一、收集Stream流到集合和指定集和中
Stream 流提供了一个 collect() 方法,可以收集流中的数据到【集合】或者【数组】中去。
stream.collect(Collectors.toList())
stream.collect(Collectors.toSet())
stream.collect(Collectors.toCollection(Supplier<C> collectionFactory))
1、示例
public void test2() {
Stream<String> stream = Stream.of("aaa", "bbb", "ccc", "bbb");
List<String> list = stream.collect(Collectors.toList());
System.out.println(list);
stream = Stream.of("aaa", "bbb", "ccc", "bbb");
Set<String> collect = stream.collect(Collectors.toSet());
System.out.println(collect);
stream = Stream.of("aaa", "bbb", "ccc", "bbb");
ArrayList<String> arrayList = stream.collect(Collectors.toCollection(ArrayList::new));
System.out.println(arrayList);
stream = Stream.of("aaa", "bbb", "ccc", "bbb");
HashSet<String> hashSet = stream.collect(Collectors.toCollection(HashSet::new));
System.out.println(hashSet);
stream = Stream.of("aaa", "bbb", "ccc", "bbb");
List<String> distinctCollect = stream.distinct().collect(Collectors.toList());
System.out.println(distinctCollect);
}
2、结果

public void test3() {
List<People> peopleList = new ArrayList<People>();
People people1 = new People("张三", 18);
People people2 = new People("王五", 19);
peopleList.add(people1);
peopleList.add(people2);
List<People> collect = peopleList.stream().collect(Collectors.toCollection(ArrayList<People>::new));
System.out.println(collect);
}

二、收集 Stream 流中的数据到数组中
1、示例
void test2() {
List<People> peopleList = new ArrayList<People>();
People people1 = new People(1, "小王", 1);
People people2 = new People(1, "小李", 2);
peopleList.add(people1);
peopleList.add(people2);
Object[] objects = peopleList.stream().toArray();
for (Object o : objects) {
System.out.println("data:" + o);
}
People[] people = peopleList.stream().toArray(People[]::new);
for (People str : people) {
System.out.println(str.getName());
}
}
2、结果

三、Stream流中数据聚合/分组/分区/拼接操作
Collectors.maxBy();
Collectors.minBy();
Collectors.summingInt();/Collectors.summingDouble();/Collectors.summingLong();
Collectors.averagingInt();/Collectors.averagingDouble();/Collectors.averagingLong();
Collectors.counting();
1、聚合操作
@Test
void test3() {
List<People> peopleList = new ArrayList<>();
peopleList.add(new People(1, "小王", 1));
peopleList.add(new People(3, "小李", 3));
peopleList.add(new People(2, "小张", 2));
peopleList.add(new People(4, "小皇", 4));
Optional<People> maxCollect1 = peopleList.stream().collect(Collectors.maxBy(
new Comparator<People>() {
@Override
public int compare(People o1, People o2) {
if (o1.getJgid() > o2.getJgid()) {
return 0;
} else {
return -1;
}
}
}
));
Optional<People> maxCollect2 = peopleList.stream().collect(Collectors.maxBy((s1, s2) -> s1.getJgid() - s2.getJgid()));
Optional<People> minCollect = peopleList.stream().collect(Collectors.maxBy((s1, s2) -> s1.getJgid() - s2.getJgid()));
Integer sumCollect1 = peopleList.stream().collect(Collectors.summingInt(new ToIntFunction<People>() {
@Override
public int applyAsInt(People value) {
return value.getJgid();
}
}));
Integer sumCollect2 = peopleList.stream().collect(Collectors.summingInt(People::getJgid));
Double avgScore1 = peopleList.stream().collect(Collectors.averagingInt(new ToIntFunction<People>() {
@Override
public int applyAsInt(People value) {
return value.getJgid();
}
}));
Double avgScore2 = peopleList.stream().collect(Collectors.averagingInt(People::getJgid));
Long count = peopleList.stream().collect(Collectors.counting());
System.out.println("最大值:" + maxCollect1.get());
System.out.println("最大值:" + maxCollect2.get());
System.out.println("最小值:" + minCollect.get());
System.out.println("jgid总和:" + sumCollect1);
System.out.println("jgid总和:" + sumCollect2);
System.out.println("分数平均值:"+avgScore1);
System.out.println("分数平均值:"+avgScore2);
System.out.println("数量为:"+count);
}

2、分组操作
当我们使用 Stream 流处理数据后,可以根据某个属性来将数据进行分组。
groupingBy(Function<? super T, ? extends K> classifier)
@Test
void test4() {
List<People> peopleList = new ArrayList<>();
peopleList.add(new People(1, "小王", 1));
peopleList.add(new People(3, "小李", 2));
peopleList.add(new People(2, "小张", 2));
peopleList.add(new People(4, "小皇", 1));
Map<Integer, List<People>> jgidMap1 = peopleList.stream().collect(Collectors.groupingBy(new Function<People, Integer>() {
@Override
public Integer apply(People people) {
return people.getJgid();
}
}));
Map<Integer, List<People>> jgidMap2 = peopleList.stream().collect(Collectors.groupingBy(People::getJgid));
System.out.println("根据年龄分组");
jgidMap1.forEach((key, value) -> {
System.out.println(key + "---->" + value);
});
Map<String, List<People>> jgidMap3 = peopleList.stream().collect(Collectors.groupingBy(s -> {
if (s.getJgid() % 2 == 0) {
return "jgid为偶数";
} else {
return "jgid为奇数";
}
}));
System.out.println("jgid奇数一组偶数一组");
jgidMap3.forEach((key, value) -> {
System.out.println(key + "---->" + value);
});
Map<Integer, Optional<People>> jgidMap4 = peopleList.stream().collect(Collectors.groupingBy(People::getJgid,
Collectors.reducing(BinaryOperator.maxBy(Comparator.comparing(People::getJgid)))
));
System.out.println("jgid分组后最大值");
jgidMap4.forEach((key, value) -> {
System.out.println(key + "---->" + value);
});
}
}

3、多级分组操作
当我们使用 Stream 流处理数据后,可以根据某个属性来将数据进行分组。
groupingBy(Function<? super T, ? extends K> classifier,Collector<? super T, A, D> downstream)
@Test
void test5() {
List<People> peopleList = new ArrayList<>();
peopleList.add(new People(1, "小王", 1));
peopleList.add(new People(2, "小李", 2));
peopleList.add(new People(3, "小张", 2));
peopleList.add(new People(4, "小皇", 1));
Map<Integer, Map<String, List<People>>> collect = peopleList.stream().collect(Collectors.groupingBy(People::getJgid,
Collectors.groupingBy(people -> {
if (people.getId() % 2 == 0) {
return "id是偶数";
} else {
return "id是奇数";
}
})));
collect.forEach((key1, value1) -> {
System.out.println("机构id" + "---->" + key1);
value1.forEach((key2, value2) -> {
System.out.println("\t" + key2 + "---->" + value2);
});
});
}

4、分区操作
分区操作,通过使用 Collectors.partitioningBy() ,根据返回值是否为 true,把集合分为两个列表,一个 true 列表,一个 false 列表。 分组和分区的区别就在:分组可以有多个组。分区只会有两个区( true 和 false)
partitioningBy(Predicate<? super T> predicate)
partitioningBy(Predicate<? super T> predicate, Collector<? super T, A, D> downstream)
@Test
void test6() {
List<People> peopleList = new ArrayList<>();
peopleList.add(new People(1, "小王", 1));
peopleList.add(new People(2, "小李", 2));
peopleList.add(new People(3, "小张", 2));
peopleList.add(new People(4, "小皇", 1));
Map<Boolean, List<People>> collect = peopleList.stream().collect(Collectors.partitioningBy(s -> {
return s.getJgid() % 2 == 0;
}));
collect.forEach((key, value) -> {
System.out.println(key + "---->" + value);
});
}

5、拼接操作
Collectors.joining() 会根据指定的连接符,将所有元素连接成一个字符串。
joining()
joining(CharSequence delimiter)
joining(CharSequence delimiter, CharSequence prefix,CharSequence suffix)
@Test
void test7() {
List<People> peopleList = new ArrayList<>();
peopleList.add(new People(1, "小王", 1));
peopleList.add(new People(2, "小李", 2));
peopleList.add(new People(3, "小张", 2));
peopleList.add(new People(4, "小皇", 1));
String collect1 = peopleList.stream().map(People::getName).collect(Collectors.joining());
System.out.println(collect1);
String collect2 = peopleList.stream().map(People::getName).collect(Collectors.joining(","));
System.out.println(collect2);
String collect3 = peopleList.stream().map(People::getName).collect(Collectors.joining("_", "开始<", ">结束"));
System.out.println(collect3);
}

|