java8中stream().collect(Supplier<R> supplier, BiConsumer<R, ? super T> accumulator)说明

java8中提供了Lambda表达式,常用的操作如下:
list.stream().collect()来进行操作,collect中传入的参数如下:

<R> R collect(Supplier<R> supplier,
                  BiConsumer<R, ? super T> accumulator,
                  BiConsumer<R, R> combiner);

Supplier 提供容器,供后续accumulatorcombiner使用

public interface Supplier<T> {
    
    
    T get();
}

Supplier只有一个方法,get返回一个容器,accumulator和combiner方法都会传入

accumulator 处理Supplier提供的T容器和stream中的元素U

@FunctionalInterface
public interface BiConsumer<T, U> {
    
    

    void accept(T t, U u);
    default BiConsumer<T, U> andThen(BiConsumer<? super T, ? super U> after) {
    
    
        Objects.requireNonNull(after);

        return (l, r) -> {
    
    
            accept(l, r);
            after.accept(l, r);
        };
    }
}

accumulator 中的accept的方法接收两个参数,Supplier提供的容器T以及stream中的元素U,在accept方法中对其进行处理,常见的比如:T是一个ArrayList 将U类型元素添加到list中

combiner 用来将所有accumulator处理后的结果T进行处理

如果是多线程处理情况下,每个线程都会有一个supplier,最后需要将这些结果合并。

常见用法:

  • 实现类实现
List<JSONObject> jsons =  data.stream().collect(
                   new Supplier<List<JSONObject>>() {
    
    
                       @Override
                       public List<JSONObject> get() {
    
    
                           return new ArrayList<>();
                       }
                   },
                   new BiConsumer<List<JSONObject>, DisplayNetworkDto>() {
    
    
                       @Override
                       public void accept(List<JSONObject> jsonObjects, DisplayNetworkDto dto) {
    
    
                            JSONObject json = new JSONObject();
                            json.put("id",dto.getAd_id());
                            json.put("city",dto.getCity());
                            jsonObjects.add(json);

                       }
                   },
                   new BiConsumer<List<JSONObject>, List<JSONObject>>() {
    
    
                       @Override
                       public void accept(List<JSONObject> jsonObjects, List<JSONObject> jsonObjects2) {
    
    
                            jsonObjects.addAll(jsonObjects2);
                       }
                   }
            );
  • lambda表达式实现:
   List<JSONObject> jsons =  data.stream().collect(
                   ()->{
    
    return new ArrayList<>();},
                   (list,dto)->{
    
    
                       JSONObject json = new JSONObject();
                       json.put("id",dto.getAd_id());
                       json.put("city",dto.getCity());
                       list.add(json);
                   },
                   (list,list2)->{
    
    
                       list.addAll(list2);
                   }
            );
  • 方法引用实现:
 List<JSONObject> jsons =  data.stream().collect(
                  ArrayList::new,
                   (list,dto)->{
    
    
                       JSONObject json = new JSONObject();
                       json.put("id",dto.getAd_id());
                       json.put("city",dto.getCity());
                       list.add(json);
                   },
                   List::addAll
            );

猜你喜欢

转载自blog.csdn.net/LeoHan163/article/details/116716871
今日推荐