1. 收集到集合中
public static void main(String[] args) {
// 收集到 List 集合中去
List<String> list = Stream.of("aa","bb","cc","aa")
.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<String> arrayList = Stream.of("aa","bb","cc","aa")
.collect(Collectors.toCollection(ArrayList::new));
System.out.println(arrayList);
HashSet<String> hashSet = Stream.of("aa","bb","cc","aa")
.collect(Collectors.toCollection(HashSet::new));
System.out.println(hashSet);
}
结果输出
[aa, bb, cc, aa]
[aa, bb, cc]
[aa, bb, cc, aa]
[aa, bb, cc]
2. 收集到数组中
Stream 中提供了 toArray
方法来讲结果放到一个数组中,返回值类型是 Object[]
,如果我们要指定返回的类型,那么可以使用另一个重载的 toArray(IntFunctioin f)
方法
Object[] objects = Stream.of("aa", "bb", "cc", "aa")
.toArray();
System.out.println(Arrays.toString(objects));
String[] strings = Stream.of("aa", "bb", "cc", "aa")
.toArray(String[]::new);
System.out.println(Arrays.toString(strings));
结果输出
[aa, bb, cc, aa]
[aa, bb, cc, aa]
3. 对流中的数据做聚合计算
当我们使用 Stream 流处理数据后,可以像数据库中共的聚合函数一样对某个字段进行操作,比如获得最大值,最小值,求和,平均值,统计数量
// 求年龄最大的数据
Optional<Person> maxAge = Stream.of(
new Person("aa", 77),
new Person("bb", 66),
new Person("cc", 55),
new Person("dd", 44),
new Person("eef", 33),
new Person("ff", 22)
).max(Comparator.comparingInt(Person::getAge));
System.out.println(maxAge);
// 求年龄最小的数据
Optional<Person> minAge = Stream.of(
new Person("aa", 77),
new Person("bb", 66),
new Person("cc", 55),
new Person("dd", 44),
new Person("eef", 33),
new Person("ff", 22)
).min(Comparator.comparingInt(Person::getAge));
System.out.println(minAge);
// 统计处所有年龄的和
Integer sumAge = Stream.of(
new Person("aa", 77),
new Person("bb", 66),
new Person("cc", 55),
new Person("dd", 44),
new Person("eef", 33),
new Person("ff", 22)
).mapToInt(Person::getAge).sum();
System.out.println(sumAge);
// 统计出年龄的数量
long countAge = Stream.of(
new Person("aa", 77),
new Person("bb", 66),
new Person("cc", 55),
new Person("dd", 44),
new Person("eef", 33),
new Person("ff", 22)
).mapToInt(Person::getAge).count();
System.out.println(countAge);
// 统计出年龄大于50岁的人的数量
long countAgeUp50 = Stream.of(
new Person("aa", 77),
new Person("bb", 66),
new Person("cc", 55),
new Person("dd", 44),
new Person("eef", 33),
new Person("ff", 22)
).filter(p -> p.getAge() > 50)
.mapToInt(Person::getAge).count();
System.out.println(countAgeUp50);
// 求出年龄的平均值、
OptionalDouble averageAge = Stream.of(
new Person("aa", 77),
new Person("bb", 66),
new Person("cc", 55),
new Person("dd", 44),
new Person("eef", 33),
new Person("ff", 22)
).mapToInt(Person::getAge).average();
System.out.println(averageAge);
4. 对流中的数据做分组操作
当我们使用 Stream 流处理数据后,可以根据某个属性将数据分组
// 根据姓名分组
Map<String, List<Person>> map1 = Stream.of(
new Person("aa", 77),
new Person("bb", 12),
new Person("aa", 55),
new Person("bb", 15),
new Person("bb", 33),
new Person("cc", 22)
).collect(Collectors.groupingBy(Person::getName));
map1.forEach((k, v)-> System.out.println("k = "+k+"\t"+"v = "+v));
// 根据年龄分组,超过 18 为成年,不满 18 岁是未成年人
Map<String, List<Person>> map2 = Stream.of(
new Person("aa", 77),
new Person("bb", 12),
new Person("aa", 55),
new Person("bb", 15),
new Person("bb", 33),
new Person("cc", 22)
).collect(Collectors.groupingBy(p -> p.getAge() > 18 ? "成年" : "未成年"));
map2.forEach((k, v) -> System.out.println("k = "+k+"\t"+"v = "+v));
输出结果:
k = cc v = [Person{name='cc', age=22}]
k = bb v = [Person{name='bb', age=12}, Person{name='bb', age=15}, Person{name='bb', age=33}]
k = aa v = [Person{name='aa', age=77}, Person{name='aa', age=55}]
k = 未成年 v = [Person{name='bb', age=12}, Person{name='bb', age=15}]
k = 成年 v = [Person{name='aa', age=77}, Person{name='aa', age=55}, Person{name='bb', age=33}, Person{name='cc', age=22}]
5. 多级分组
现根据姓名分组,然后再根据年龄分成年和未成年
// 根据姓名分组
Map<String, Map<String, List<Person>>> map = Stream.of(
new Person("aa", 77),
new Person("bb", 12),
new Person("aa", 55),
new Person("bb", 15),
new Person("bb", 33),
new Person("cc", 22)
).collect(Collectors.groupingBy(Person::getName, Collectors.groupingBy(p -> p.getAge() > 18 ? "成年" : "未成年")));
map.forEach((k, v) ->{
System.out.println(k);
v.forEach((k1,v1) -> {
System.out.println("\t"+k1 + "="+v1);
});
});
输出结果:
cc
成年=[Person{name='cc', age=22}]
bb
未成年=[Person{name='bb', age=12}, Person{name='bb', age=15}]
成年=[Person{name='bb', age=33}]
aa
成年=[Person{name='aa', age=77}, Person{name='aa', age=55}]