**java
@param <T>
*/
static class GroupToList<T> implements Collector<T, List<List<T>>, List<List<T>>> {
/**sql
集合中對象兩兩比較,知足自定義的條件(operation結果返回true),便將這兩個元素分爲一組
*/
private BiFunction<T, T, Boolean> operation;架構
public GroupToList(BiFunction<T, T, Boolean> operation) {
super();
this.operation = operation;
}併發
@Override
public BiConsumer<List<List<T>>, T> accumulator() {
return (c, t) -> {
boolean added = false;
for (List<T> cc : c) {
for (T ccc : cc) {
if (operation.apply(ccc, t)){
cc.add(t);
added = true;
}
break;
}
if (added)
break;
continue;
}
if (!added) {
ArrayList<T> list = new ArrayList<T>();
list.add(t);
c.add(list);
}
};
}app
@Override
public Set<java.util.stream.Collector.Characteristics> characteristics() {
/
Set<java.util.stream.Collector.Characteristics> emptySet = Collections.emptySet();
/
return Collections.unmodifiableSet(EnumSet.of(Characteristics.IDENTITY_FINISH));
}分佈式
@Override
public BinaryOperator<List<List<T>>> combiner() {
return (l1,l2) -> {l2.addAll(l1); return l2;};
}ide
@Override
public Function<List<List<T>>, List<List<T>>> finisher() {
return p -> p;
}高併發
@Override
public Supplier<List<List<T>>> supplier() {
return () -> new ArrayList<List<T>>(); 歡迎工做一到五年的Java工程師朋友們加入Java羣: 891219277
羣內提供免費的Java架構學習資料(裏面有高可用、高併發、高性能及分佈式、Jvm性能調優、Spring源碼,MyBatis,Netty,Redis,Kafka,Mysql,Zookeeper,Tomcat,Docker,Dubbo,Nginx等多個知識點的架構資料)合理利用本身每一分每一秒的時間來學習提高本身,不要再用"沒有時間「來掩飾本身思想上的懶惰!趁年輕,使勁拼,給將來的本身一個交代!性能
}
}
複製代碼學習