php中做购物网站的教程,seo关键词排名优化推荐,wordpress插件dx seo下载,网站跨省备案一、Collectors类#xff1a;
1.1、Collectors介绍
Collectors类#xff0c;是JDK1.8开始提供的一个的工具类#xff0c;它专门用于对Stream操作流中的元素各种处理操作#xff0c;Collectors类中提供了一些常用的方法#xff0c;例如#xff1a;toList()、toSet()、to…一、Collectors类
1.1、Collectors介绍
Collectors类是JDK1.8开始提供的一个的工具类它专门用于对Stream操作流中的元素各种处理操作Collectors类中提供了一些常用的方法例如toList()、toSet()、toCollection()、toMap()、toConcurrentMap()方法以及一些分组聚合的方法。
Stream操作流
1.2、常用方法 注意Collectors类中的方法都是需要和Stream类中的collect()方法结合使用的。 1toList、toSet方法
toList、toSet方法是将Stream流中的数据提取出来转换成集合返回。
public static ListUser getUserList() {ListUser list new ArrayList();for (int i 0; i 10; i) {list.add(new User(i11, name_00 i, test_0 i));}return list;
}
public static void main(String[] args) {ListUser userList getUserList();// 转换 List 集合ListString list userList.stream().map(User::getUname).collect(Collectors.toList());System.out.println(list);// 转换 Set 集合SetString list2 userList.stream().map(User::getPassword).collect(Collectors.toSet());System.out.println(list2);
}2toMap方法
toMap、toConcurrentMap两个方法的作用是一样的只不过toConcurrentMap方法是线程安全的。这两个方法在使用的时候至少需要传递两个参数分别是
第一个参数Function类型的参数作为keyMapper这是用于指定Map中的key的必须传递。第二个参数Function类型的参数作为valuesMapper这是用于指定Map中的value的必须传递。第三个参数BinaryOperator类型的参数这是用于当Map集合中的key重复的时候执行的解决办法可选只有当key重复时候才会执行这个操作。
public static void main(String[] args) {ListUser userList getUserList();// 转换为map集合MapInteger, User userMap userList.stream().collect(Collectors.toMap(User::getId, item - item, (x, y) - {if (Objects.equals(x.getId(), y.getId())) {System.out.println(key发生重复啦);return x;}return y;}));System.out.println(userMap);
}3joining方法
joining方法的作用就是将Stream流中的元素按照指定的格式拼接起来该方法有哪个重载类型分别如下所示
第一个方法joining()无参数方法直接将所有元素拼接起来。第二个方法joining(delimiter)一个参数方法按照指定的分隔符delimiter拼接元素。第三个方法joining(delimiterprefixsuffix)三个参数方法按照指定的分隔符delimiter拼接元素并且在最终拼接结果的前后指定prefix前缀和suffix后缀。
public static void main(String[] args) {ListUser userList getUserList();// joining 连接String join1 userList.stream().map(User::getUname).collect(Collectors.joining());String join2 userList.stream().map(User::getUname).collect(Collectors.joining(,));String join3 userList.stream().map(User::getUname).collect(Collectors.joining(,, [, ]));System.out.println(join1);System.out.println(join2);System.out.println(join3);
}4counting方法
counting方法用于统计元素个数一般不会单独使用会和groupingBy结合使用。
// counting 统计
Long count userList.stream().collect(Collectors.counting());
System.out.println(count);5groupingBy方法
groupingBy方法用于将Stream流中的元素按照某个分组规则将其分组。groupingBy方法返回值是一个Map集合集合中的key表示分组的唯一标识value则表示分组后的所有元素默认是List集合。
groupingBy方法有多个参数的重载如下所示
一个参数的方法groupingBy(Function key)接收一个Function类型的参数主要是作为Map集合中的key默认是返回List集合作为value。两个参数的方法groupingBy(Function keyCollector downstream)key是Map的key值第二个则表示一个收集器对分组中的所有元素进行收集然后作为Map对象的value。*groupingBy可以实现多个字段分组也就是先按照某个字段分组之后在对组内的集合按照某个字段继续分组* 案例代码如下
public static void main(String[] args) {ListUser userList new ArrayList();userList.add(new User(1001, java, 123));userList.add(new User(1002, html, 123));userList.add(new User(1003, css, 123));userList.add(new User(1004, js, 123));userList.add(new User(1005, java, 123));userList.add(new User(1006, css, 123));// 按照 name 分组MapString, ListUser map userList.stream().collect(Collectors.groupingBy(User::getUname));System.out.println(map);// 按照 name 分组并且计算每一组中 id 平均值MapString, Double map1 userList.stream().collect(Collectors.groupingBy(User::getUname, Collectors.averagingDouble(User::getId)));System.out.println(map1);
}6partitioningBy方法
partitioningBy方法是分区的它是一种特殊的分组groupingBy前面介绍了groupingBy是将数组按照某个key分组成多个集合最终得到一个MapkeyList结果。
而这里的partitioningBy意味分区它是将数据分为两组一组是满足条件的分区另外一组则是不满足条件的分组最终返回的结果是一个MapbooleanList的结果格式其中boolean只有两个值true和false。
public static void main(String[] args) {// 分区Integer[] nums new Integer[] { 1, 2, 3, 4, 5 };// 将大于 3 的作为一组MapBoolean, ListInteger map Arrays.stream(nums).collect(Collectors.partitioningBy(item - item 3));System.out.println(map);
}运行结果如下所示