/**
* Simple implementation class for {@code Collector}.
*
* @param <T> the type of elements to be collected
* @param <R> the type of the result
*/
static class CollectorImpl<T, A, R> implements Collector<T, A, R> {
private final Supplier<A> supplier;
private final BiConsumer<A, T> accumulator;
private final BinaryOperator<A> combiner;
private final Function<A, R> finisher;
private final Set<Characteristics> characteristics;
CollectorImpl(Supplier<A> supplier,
BiConsumer<A, T> accumulator,
BinaryOperator<A> combiner,
Function<A,R> finisher,
Set<Characteristics> characteristics) {
this.supplier = supplier;
this.accumulator = accumulator;
this.combiner = combiner;
this.finisher = finisher;
this.characteristics = characteristics;
}
CollectorImpl(Supplier<A> supplier,
BiConsumer<A, T> accumulator,
BinaryOperator<A> combiner,
Set<Characteristics> characteristics) {
this(supplier, accumulator, combiner, castingIdentity(), characteristics);
}
@Override
public BiConsumer<A, T> accumulator() {
return accumulator;
}
@Override
public Supplier<A> supplier() {
return supplier;
}
@Override
public BinaryOperator<A> combiner() {
return combiner;
}
@Override
public Function<A, R> finisher() {
return finisher;
}
@Override
public Set<Characteristics> characteristics() {
return characteristics;
}
}
一些示例应用:
@Test
public void Test2(){
SomePerson somePerson1 = new SomePerson("liuliu", 21,100 );
SomePerson somePerson2 = new SomePerson("tian", 11,100 );
SomePerson somePerson3 = new SomePerson("men", 11,100 );
SomePerson somePerson4 = new SomePerson("tian", 30,110 );
List<SomePerson> somes = Arrays.asList(somePerson1,somePerson2,somePerson3,somePerson4);
somes.stream().collect(Collectors.minBy(Comparator.comparingInt(SomePerson::getAge)))
.ifPresent(System.out::println);
System.out.println(somes.stream().collect(Collectors.averagingDouble(SomePerson::getAge)));
System.out.println(somes.stream().collect(Collectors.summarizingDouble(SomePerson::getAge)));
System.out.println(somes.stream().map(SomePerson::getName).
collect(Collectors.joining(",")));
System.out.println(somes.stream().map(SomePerson::getName).
collect(Collectors.joining(",","<begin>","<end>")));
//多级分组:Height和name双重分组
Map<Integer,Map<String,List<SomePerson>>> maps = somes.stream().
collect(Collectors.groupingBy(SomePerson::getHeight,Collectors.groupingBy(SomePerson::getName)));
System.out.println(maps);
//多级分区:age>20中age>30
Map<Boolean,Map<Boolean,List<SomePerson>>> mapb = somes.stream().collect
(Collectors.partitioningBy(s->s.getAge()>20, Collectors.partitioningBy(s->s.getHeight()>30)));
System.out.println(mapb);
//年龄每个分区的人的个数
Map<Boolean,Long> mapCount = somes.stream().collect
(Collectors.partitioningBy(s->s.getAge()>20,Collectors.counting()));
System.out.println(mapCount);
//按Name分组,并只取Age较小的
Map<String,SomePerson> map2 = somes.stream().collect(groupingBy(SomePerson::getName,
collectingAndThen(minBy(Comparator.comparing(SomePerson::getAge)),Optional::get)));
System.out.println(map2);
}
class SomePerson{
private String name;
private int age;
private int height;
public SomePerson(String name, int age, int height) {
this.name = name;
this.age = age;
this.height = height;
}
@Override
public String toString() {
return "SomePerson{" +
"name='" + name + '\'' +
", age=" + age +
", height=" + height +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
}