1.初识流
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
public class Start1 {
public static void main(String [] args){
List<String> list = new ArrayList<>();
list.add("王xx");
list.add("王x");
list.add("奉x");
list.add("钱xx");
Stream<String> str = list.stream();
str.filter(s->s.startsWith("王"))
.filter(name->name.length()==3)
.filter(name->name.length()==3)
.forEach(name-> System.out.println(name));
}
}
2.获取流
import java.util.*;
import java.util.stream.Stream;
public class CollectionStream {
public static void main(String[] args) {
List<String>list=new ArrayList<>();
Stream<String> stream = list.stream();
Set<String> set=new HashSet<>();
Stream<String> stream1 = set.stream();
Set<String> set1=new LinkedHashSet<>();
Stream<String> stream2 = set1.stream();
Map<String,String>map=new HashMap<>();
Set<String> strings = map.keySet();
Stream<String> stream3 = strings.stream();
Collection<String> values = map.values();
Stream<String> stream4 = values.stream();
Set<Map.Entry<String,String>>set2=map.entrySet();
Stream<Map.Entry<String, String>> stream5 = set2.stream();
Integer []arr=new Integer[10];
Stream<Integer>stream6=Stream.of(1,2,3,4,5);
Stream<Integer>stream7=Stream.of(arr);
}
}
forEach
forEach方法,用来遍历流是一个终结方法,遍历之后不能继续调用Stream流的其他方法
import java.util.stream.Stream;
public class StreamForeach {
public static void main(String[] args) {
Stream<Integer>stream=Stream.of(1,2,3,4,5);
stream.forEach(i->System.out.println(i));
}
}
Stream filter的使用
延迟方法
import java.util.stream.Stream;
/*
Stream流中常用的方法filter:用与对Stream流中数据过滤
filter
*/
public class StreamFilter {
public static void main(String[] args) {
Stream<Integer>stream=Stream.of(1,3,8,5,1,4,2,7,3,3);
stream.filter(i->i%2==0)
.forEach(i-> System.out.println(i));
}
}
Stream map方法的使用
延迟方法
import java.util.stream.Stream;
public class StreamMap {
public static void main(String[] args) {
Stream<Integer>stream=Stream.of(1,2,3,4,5,6);
stream.map((i)->{
return (char)(i+64);
}).forEach((i)-> System.out.println(i));
}
}
Stream count方法的使用
终结方法
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
public class StreamCount {
public static void main(String[] args) {
Stream<Integer>stream=Stream.of(1,3,1,4,5,2,0);
long count = stream.count();
System.out.println(count);
List <Integer>list=new ArrayList<>();
list.add(1);
list.add(3);
list.add(1);
list.add(4);
list.add(5);
list.add(2);
list.add(0);
System.out.println(list.stream().count());
}
}
Stream Limit方法的使用
延时方法
import java.util.stream.Stream;
public class StreamLimit {
public static void main(String[] args) {
Stream<String>stream=Stream.of("D","j","k","s","t","r","a");
stream.limit(3)
.forEach((i)-> System.out.print(i));
}
}
Stream skip方法的使用
延时方法
import java.util.stream.Stream;
public class StreamSkip {
public static void main(String[] args) {
Stream<String> stream=Stream.of("D","j","k","s","t","r","a");
stream.skip(3)
.forEach((i)-> System.out.print(i));
}
}
Stream concat方法的使用
import java.util.stream.Stream;
public class StreamConcat {
public static void main(String[] args) {
Stream<String>stream=Stream.of("D","j","k","s","t","r","a");
Stream<String>stream1=Stream.of("s","p","f","a");
Stream<String> stream2 = Stream.concat(stream, stream1);
stream2.forEach((i)-> System.out.print(i));
}
}
样例
package Demo;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) {
Person []asd=new Person[10];
int k=0;
Stream<String> stream = Stream.of("张三","谢五","李四","蓝孔雀","阿达尔","韩佳人");
Stream<String> limit = stream.filter((i) -> i.length() == 3)
.limit(3);
Stream<String> stream1=Stream.of("daidioja","dadadafsrfsdq","weASQQEDQD","adad","sadasd");
Stream<String> skip = stream1.filter((i) ->i.length()> 6)
.skip(2);
Stream.concat(limit, skip).map((i)->new Person(i)).forEach(i-> System.out.println(i));
}
}