JDK1.8新特性: Collectors

一:简介
Stream中有两个个方法collect和collectingAndThen用于对流中的数据进行处理,可以对流中的数据进行聚合操作,如:

将流中的数据转成集合类型: toList、toSet、toMap、toCollection
将流中的数据(字符串)使用分隔符拼接在一起:joining
对流中的数据求最大值maxBy、最小值minBy、求和summingInt、求平均值averagingDouble
对流中的数据进行映射处理 mapping
对流中的数据分组:groupingBy、partitioningBy
对流中的数据累计计算:reducing
<R, A> R collect(Collector<? super T, A, R> collector); 

// collectingAndThen : 将流中的数据通过Collector计算,最终的结果在通过Function再最终处理一下
public static<T,A,R,RR> Collector<T,A,RR> collectingAndThen(Collector<T,A,R> downstream,
                                                                Function<R,RR> finisher);

Collectors


public final class Collectors {

    // 转换成集合
    public static <T> Collector<T, ?, List<T>> toList();
    public static <T> Collector<T, ?, Set<T>> toSet();
    public static <T, K, U> Collector<T, ?, Map<K,U>> toMap(Function<? super T, ? extends K> keyMapper,
                                    Function<? super T, ? extends U> valueMapper);
    public static <T, C extends Collection<T>> Collector<T, ?, C> toCollection(Supplier<C> collectionFactory);
                                     
   // 拼接字符串,有多个重载方法                                  
   public static Collector<CharSequence, ?, String> joining(CharSequence delimiter);   
   public static Collector<CharSequence, ?, String> joining(CharSequence delimiter,
                                                             CharSequence prefix,
                                                             CharSequence suffix);      
    // 最大值、最小值、求和、平均值                                                         
    public static <T> Collector<T, ?, Optional<T>> maxBy(Comparator<? super T> comparator);
    public static <T> Collector<T, ?, Optional<T>> minBy(Comparator<? super T> comparator);
    public static <T> Collector<T, ?, Integer> summingInt(ToIntFunction<? super T> mapper);      
    public static <T> Collector<T, ?, Double> averagingDouble(ToDoubleFunction<? super T> mapper);                   
    
    // 分组:可以分成true和false两组,也可以根据字段分成多组                                 
    public static <T, K> Collector<T, ?, Map<K, List<T>>> groupingBy(Function<? super T, ? extends K> classifier);
    // 只能分成true和false两组
    public static <T> Collector<T, ?, Map<Boolean, List<T>>> partitioningBy(Predicate<? super T> predicate);
    
     // 映射
     public static <T, U, A, R> Collector<T, ?, R> mapping(Function<? super T, ? extends U> mapper,
                               Collector<? super U, A, R> downstream);
                               
     public static <T, U> Collector<T, ?, U> reducing(U identity,
                                Function<? super T, ? extends U> mapper,
                                BinaryOperator<U> op);
}


二:示例
流转换成集合

@Test
public void testToCollection(){
    List<Integer> list = Arrays.asList(1, 2, 3);

    // [10, 20, 30]
    List<Integer> collect = list.stream().map(i -> i * 10).collect(Collectors.toList());
    
    // [20, 10, 30]
    Set<Integer> collect1 = list.stream().map(i -> i * 10).collect(Collectors.toSet());
    
    // {key1=value:10, key2=value:20, key3=value:30}
    Map<String, String> collect2 = list.stream().map(i -> i * 10).collect(Collectors.toMap(key -> "key" + key/10, value -> "value:" + value));
    
    // [1, 3, 4]
    TreeSet<Integer> collect3= Stream.of(1, 3, 4).collect(Collectors.toCollection(TreeSet::new));
}


 

@Data
@ToString
@AllArgsConstructor
@RequiredArgsConstructor
public class User {
    private Long id;
    private String username;
}

@Test
public void testToMap() {
    List<User> userList = Arrays.asList(
         new User(1L, "mengday"),
         new User(2L, "mengdee"),
         new User(3L, "mengdy")
    );
    
    // toMap 可用于将List转为Map,便于通过key快速查找到某个value
    Map<Long, User> userIdAndModelMap = userList.stream().collect(Collectors.toMap(User::getId, Function.identity()));
    User user = userIdAndModelMap.get(1L);
    // User(id=1, username=mengday)
    System.out.println(user);
    
    Map<Long, String> userIdAndUsernameMap = userList.stream().collect(Collectors.toMap(User::getId, User::getUsername));
    String username = userIdAndUsernameMap.get(1L);
    // mengday
    System.out.println(username);
}


集合元素拼接

@Test
public void testJoining(){
    // a,b,c
    List<String> list2 = Arrays.asList("a", "b", "c");
    String result = list2.stream().collect(Collectors.joining(","));

    // Collectors.joining(",")的结果是:a,b,c  然后再将结果 x + "d"操作, 最终返回a,b,cd
    String str= Stream.of("a", "b", "c").collect(Collectors.collectingAndThen(Collectors.joining(","), x -> x + "d"));
}


元素聚合

@Test
public void test(){
    // 求最值 3
    List<Integer> list = Arrays.asList(1, 2, 3);
    Integer maxValue = list.stream().collect(Collectors.collectingAndThen(Collectors.maxBy((a, b) -> a - b), Optional::get));


    // 最小值 1
    Integer minValue = list.stream().collect(Collectors.collectingAndThen(Collectors.minBy((a, b) -> a - b), Optional::get));

    // 求和 6
    Integer sumValue = list.stream().collect(Collectors.summingInt(item -> item));

    // 平均值 2.0
    Double avg = list.stream().collect(Collectors.averagingDouble(x -> x));
}

@Test
public void test(){
    // 映射:先对集合中的元素进行映射,然后再对映射的结果使用Collectors操作
    // A,B,C
    Stream.of("a", "b", "c").collect(Collectors.mapping(x -> x.toUpperCase(), Collectors.joining(",")));
}


分组

public class User {
    private Long id;
    private String username;
    private Integer type;

    // Getter & Setter & toString
}

@Test
public void testGroupBy(){
    List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
    // 奇偶数分组:奇数分一组,偶数分一组
    // groupingBy(Function<? super T, ? extends K> classifier) 参数是Function类型,Function返回值可以是要分组的条件,也可以是要分组的字段
    // 返回的结果是Map,其中key的数据类型为Function体中计算类型,value是List<T>类型,为分组的结果
    Map<Boolean, List<Integer>> result = list.stream().collect(Collectors.groupingBy(item -> item % 2 == 0));
    // {false=[1, 3, 5, 7, 9], true=[2, 4, 6, 8, 10]}
    System.out.println(result);


     // partitioningBy 用于分成两组的情况
    Map<Boolean, List<Integer>> twoPartiton = list.stream().collect(Collectors.partitioningBy(item -> item % 2 == 0));
    System.out.println(twoPartiton);
    
    
    User user = new User(1L, "zhangsan", 1);
    User user2 = new User(2L, "lisi", 2);
    User user3 = new User(3L, "wangwu", 3);
    User user4 = new User(4L, "fengliu", 1);
    List<User> users = Arrays.asList(user, user2, user3, user4);
    // 根据某个字段进行分组
    Map<Integer, List<User>> userGroup = users.stream().collect(Collectors.groupingBy(item -> item.type));

    /**
     * key 为要分组的字段
     * value 分组的结果
     * {
     *  1=[User{id=1, username='zhangsan', type=1}, User{id=4, username='fengliu', type=1}],
     *  2=[User{id=2, username='lisi', type=2}],
     *  3=[User{id=3, username='wangwu', type=3}]
     * }
     */
    System.out.println(userGroup);
}    


累计操作

@Test
public void testReducing(){
        
    // sum: 是每次累计计算的结果,b是Function的结果
    System.out.println(Stream.of(1, 3, 4).collect(Collectors.reducing(0, x -> x + 1, (sum, b) -> {
        System.out.println(sum + "-" + b);
        return sum + b;
    })));

    
     // 下面代码是对reducing函数功能实现的描述,用于理解reducing的功能
    int sum = 0;
    List<Integer> list3 = Arrays.asList(1, 3, 4);
    for (Integer item : list3) {
        int b = item + 1;
        System.out.println(sum + "-" + b);
        sum = sum + b;
    }
    System.out.println(sum);

        
    // 注意reducing可以用于更复杂的累计计算,加减乘除或者更复杂的操作
    // result = 2 * 4 * 5 = 40
    System.out.println(Stream.of(1, 3, 4).collect(Collectors.reducing(1, x -> x + 1, (result, b) -> {
        System.out.println(result + "-" + b);
        return result * b;
    })));
}


--------------------- 
作者:vbirdbest 
来源:CSDN 
原文:https://blog.csdn.net/vbirdbest/article/details/80216713 
版权声明:本文为博主原创文章,转载请附上博文链接!

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值