jdk1.8在Collection中新增了一个默认方法Stream,Collection接口的所有实现类都可以继承这个方法,如List、Set、Vector等,对于Map、数组 等集合没有实现Collection接口,也可以通过其他方式转换成Stream;
Stream常用方法:
count:统计元素个数
如:int count = Stream.of("a1", "a2", "a3").count();
forEach:遍历集合,见上图
filter:筛选集合中的元素,如筛选姓张 并且 名字是三个字的
List<String> list = Arrays.asList("张三","张三丰","成龙","周星驰");
list.stream()
.filter(s->s.startsWith("张"))
.filter(s->s.length() == 3)
.forEach(s->{ System.out.println(s); });
limit:取前几个数据,如:
Stream.of("a1", "a2", "a3","bb","cc","aa","dd")
.limit(3)
.forEach(a->{System.out.println(a);});
skip:和limit相反,跳过前面几个数据取后面的;
map:将流中的元素映射到另一个流中,如 将Stream<String>映射成Stream<Integer>
Stream.of("1", "2", "3","4","5","6","7")
.map(msg->Integer.parseInt(msg))
.forEach(a->{System.out.println(a);});
sorted:排序,:: 是方法引用,1.8新的语法,效果和上面map一样
Stream.of("1", "3", "2","4","0","9","7")
.map(Integer::parseInt)
//.sorted() // 根据数据的自然顺序排序
.sorted((o1,o2)->o2-o1) // 根据比较 强指定排序规则
.forEach(System.out::println);
distinct:去重:
Stream.of(new Person("张三",18)
,new Person("李四",22)
,new Person("张三",18))
.distinct()
.forEach(System.out::println);
match:判断集合中的元素是否满足某个条件,返回true或false,有三个方法:
anyMatch,任意一个元素满足即为true;
allMatch,所有元素都满足才为true;
noneMatch:都不满足才为true;
用法: boolean b = Stream.of(1, 3, 4).noneMatch(s -> s >3);
System.out.println(b);
max和min:返回最大或最小值:
Optional<Integer> max = Stream.of(2,9,1, 3, 7).max((o1,o2)->o1-o2);
System.out.println(max.get());
reduce:将所有数据归纳得到一个数据
/* identity默认值,第一次的时候会将默认值赋值给x,之后每次会将 上一次的操作结果赋值给x
y就是每次从数据中获取的元素
*/
Integer sum = Stream.of(4, 5, 3, 9)
.reduce(0, (x, y) -> {
System.out.println("x="+x+",y="+y);
return x + y;
});
System.out.println(sum);
// 获取 最大值
Integer max = Stream.of(4, 5, 3, 9)
.reduce(0, (x, y) -> {
return x > y ? x : y;
});
System.out.println(max);
常和map配合使用,如:
Integer maxAge = Stream.of(new Person("张三", 18)
, new Person("李四", 22)
, new Person("王五", 15))
.map(Person::getAge)
.reduce(0, Math::max);
System.out.println(maxAge);
mapToInt:将Stream中的Integer转换成int,类似的还有mapToLong、mapToDouble如:
Integer arr[] = {1,2,3,5,6,8};
IntStream intStream = Stream.of(arr)
.mapToInt(Integer::intValue);
concat:将两个Stream合并成一个Stream:
Stream<String> stream1 = Stream.of("a","b","c");
Stream<String> stream2 = Stream.of("x", "y", "z");
Stream.concat(stream1,stream2).forEach(System.out::println);
Stream中的元素转换成集合,也叫Stream结果收集:
//收集到list集合中
List<String> list = Stream.of("aa", "bb", "cc").collect(Collectors.toList());
System.out.println(list);
// 收集到 Set集合中
Set<String> set = Stream.of("aa", "bb", "cc", "aa").collect(Collectors.toSet());
System.out.println(set);
//收集到ArrayList
ArrayList<String> arrayList = Stream.of("aa", "bb", "cc")
.collect(Collectors.toCollection(() -> new ArrayList<>()));
System.out.println(arrayList);
//收集到hashSet
HashSet<String> hashSet = Stream.of("aa", "bb", "cc")
.collect(Collectors.toCollection(HashSet::new));
System.out.println(hashSet);
//收集到数组中
String[] strings = Stream.of("aa", "bb", "cc").toArray(String[]::new);
System.out.println(Arrays.toString(strings));
//将list中的数据收集到map中,常用于批量查询后匹配结果,list是一个List<Map>
Map re = list.stream().collect(Collectors.toMap(t->t.get("user_id"),t->t));
并行Stream,上面说的都是串行的Stream,并行Stream获取:
1、通过List 接口 直接获取并行流
List<Integer> list = new ArrayList<>();
Stream<Integer> integerStream = list.parallelStream();
2、将已有的串行流转换为并行流
Stream<Integer> parallel = Stream.of(1, 2, 3).parallel();
并行Stream是采用的多线程处理数据,提升效率;