一、收集 Stream 流中的数据到集合中

使用Stream 流需要注意的是Stream不调用终止方法,中间的操作不会执行。

当我们对Stream 流中的数据操作完成之后,如果需要将流的结果进行保存,方便我们接下来对结果的继续操作,就得使用 Stream 流提供了的 collect() 方法,可以收集流中的数据到【集合】或者【数组】中。

代码示例:

List<String> itemList1 = new ArrayList<>();//测试数据
itemList1.add("A");
itemList1.add("B");
itemList1.add("C");
itemList1.add("D");
itemList1.add("E");
//1.收集流中的数据到 list
List<String> list = itemList1.stream().collect(Collectors.toList());
//2.收集流中的数据到 set
Set<String> collect = itemList1.stream().collect(Collectors.toSet());
//3.收集流中的数据(ArrayList)(不收集到list,set等集合中,而是)收集到指定的集合中
ArrayList<String> arrayList = itemList1.stream().collect(Collectors.toCollection(ArrayList::new));
//4.收集流中的数据到 HashSet
HashSet<String> hashSet = itemList1.stream().collect(Collectors.toCollection(HashSet::new));
二、获取list的交集、差集、并集、去重并集
注意:通过流操作完后,是返回新的集合,并不会对源list有影响
代码示例:
List<String> itemList1 = new ArrayList<>();
itemList1.add("A");
itemList1.add("B");
itemList1.add("C");
itemList1.add("D");
itemList1.add("E");
List<String> itemList2 = new ArrayList<>();
itemList2.add("A");
itemList2.add("A");
itemList2.add("C");
itemList2.add("D");
itemList2.add("D");
//收集流中的数据到集合中
//1.收集流中的数据到 list
List<String> list = itemList1.stream().collect(Collectors.toList());
System.out.println(list);
//2.收集流中的数据到 set
Set<String> collect = itemList1.stream().collect(Collectors.toSet());
System.out.println(collect);
//3.收集流中的数据(ArrayList)(不收集到list,set等集合中,而是)收集到指定的集合中
ArrayList<String> arrayList = itemList1.stream().collect(Collectors.toCollection(ArrayList::new));
System.out.println(arrayList);
//4.收集流中的数据到 HashSet
HashSet<String> hashSet = itemList1.stream().collect(Collectors.toCollection(HashSet::new));
System.out.println(hashSet);
//获取交集
List<String> test1 = itemList1.stream().filter(item -> itemList2.contains(item)).collect(Collectors.toList());
System.out.println("交集:");
test1.parallelStream().forEach(System.out :: println);
// 差集
List<String> test2 = itemList1.stream().filter(item -> !itemList2.contains(item)).collect(Collectors.toList());
System.out.println("差集:");
test2.parallelStream().forEach(System.out :: println);
// 并集
List<String> itemList1All = itemList1.parallelStream().collect(Collectors.toList());
List<String> itemList2All = itemList2.parallelStream().collect(Collectors.toList());
itemList1All.addAll(itemList2All);
System.out.println("并集:");
itemList1All.parallelStream().forEach(System.out :: println);
// 去重并集,就是在并集的基础上用distinct处理
List<String> itemAllDistinct = itemList1All.stream().distinct().collect(Collectors.toList());
System.out.println("去重并集:");
itemAllDistinct.parallelStream().forEach(System.out :: println);
System.out.println("---原来的itemList1---");
itemList1.parallelStream().forEach(System.out :: println);
System.out.println("---原来的itemList2---");
itemList2.parallelStream().forEach(System.out :: println);

三、将list中的对象的某一个字段值提取出来,返回成一个新的list

List<User> userList = new ArrayList<>();
User user1 = new User();
user1.setUserId("11");
user1.setTrueName("杨某人");
User user2 = new User();
user2.setUserId("22");
user2.setTrueName("张某人");
User user3 = new User();
user3.setTrueName("郭某人");
userList.add(user1);
userList.add(user2);
userList.add(user3);
// 将list中的对象的某一个字段值提取出来,返回成一个新的list
List<Object> newList = userList.stream().map(User::getUserId).collect(Collectors.toList());
newList.parallelStream().forEach(System.out :: println);
注意:如果对象中提取的字段是空的,会返回NULL
过滤掉null,可以这么写:
// 通过filter过滤掉null
List<Object> newList = userList.stream().map(User::getUserId).filter(x->null!=x).collect(Collectors.toList());
newList.parallelStream().forEach(System.out :: println);
四、分组操作
Collector收集器一个最常见的用户用例就是对元素进行分组
代码示例:
List<User> userList = new ArrayList<>();
User user1 = new User();
user1.setUserId("11");
user1.setTrueName("杨某人");
user1.setUserType(1);
User user2 = new User();
user2.setUserId("22");
user2.setTrueName("张某人");
user2.setUserType(1);
User user3 = new User();
user3.setTrueName("郭某人");
user3.setUserType(2);
userList.add(user1);
userList.add(user2);
userList.add(user3);
Map<Integer, List<User>> newMap = userList.stream().collect(Collectors.groupingBy(User::getUserType));
newMap.forEach((key, value) -> {
System.out.println(key + "---->" + value);
});
五、list取两个字段转map
//list取两个字段转map
Map<String, Object> newTestMap = userList.stream().collect(Collectors.toMap(User::getUserId, User::getTrueName, (k1, k2) -> k1));
newTestMap.forEach((key, value) -> {
System.out.println(key + "---->" + value);
});
// 从List中取出某个字段组成新的List
List<String> userIds = userList.stream().map(e -> e.getUserId()).collect(Collectors.toList());
userIds.forEach(x -> {
System.out.println(x);
});

六、聚合操作

我们使用 Stream 流处理数据后,可以像数据库的聚合函数一样对某个字段进行操作。比如获取最大值,获取最小值,求总和,求平均值,统计数量等操作。