Collectors.groupingBy
Collectors类 groupingBy 方法
package java.util.stream;
public final class Collectors {
public static <T, K> Collector<T, ?, Map<K, List<T>>>
groupingBy(Function<? super T, ? extends K> classifier) {
return groupingBy(classifier, toList());
}
public static <T, K, A, D>
Collector<T, ?, Map<K, D>> groupingBy(Function<? super T, ? extends K> classifier,
Collector<? super T, A, D> downstream) {
return groupingBy(classifier, HashMap::new, downstream);
}
public static <T, K, D, A, M extends Map<K, D>>
Collector<T, ?, M> groupingBy(Function<? super T, ? extends K> classifier,
Supplier<M> mapFactory,
Collector<? super T, A, D> downstream) {
Supplier<A> downstreamSupplier = downstream.supplier();
BiConsumer<A, ? super T> downstreamAccumulator = downstream.accumulator();
BiConsumer<Map<K, A>, T> accumulator = (m, t) -> {
K key = Objects.requireNonNull(classifier.apply(t), "element cannot be mapped to a null key");
A container = m.computeIfAbsent(key, k -> downstreamSupplier.get());
downstreamAccumulator.accept(container, t);
};
BinaryOperator<Map<K, A>> merger = Collectors.<K, A, Map<K, A>>mapMerger(downstream.combiner());
@SuppressWarnings("unchecked")
Supplier<Map<K, A>> mangledFactory = (Supplier<Map<K, A>>) mapFactory;
if (downstream.characteristics().contains(Collector.Characteristics.IDENTITY_FINISH)) {
return new CollectorImpl<>(mangledFactory, accumulator, merger, CH_ID);
}
else {
@SuppressWarnings("unchecked")
Function<A, A> downstreamFinisher = (Function<A, A>) downstream.finisher();
Function<Map<K, A>, M> finisher = intermediate -> {
intermediate.replaceAll((k, v) -> downstreamFinisher.apply(v));
@SuppressWarnings("unchecked")
M castResult = (M) intermediate;
return castResult;
};
return new CollectorImpl<>(mangledFactory, accumulator, merger, finisher, CH_NOID);
}
}
}
-
一个参数的方法
一个参数的方法,还是调用的两个参数的重载方法,第二个参数默认调用
toList()
方法public static <T, K> Collector<T, ?, Map<K, List<T>>> groupingBy(Function<? super T, ? extends K> classifier) { return groupingBy(classifier, toList());
}
示例:
```java
public class Student {
private Long id;
private String name;
private Integer age;
private String address;
}
public class GroupingByTest {
public static void main(String[] args) {
List<Student> students = new ArrayList<>();
Student student = new Student();
student.setId(1L);
student.setName("小明");
students.add(student);
Student student2 = new Student();
student2.setId(2L);
student2.setName("小红");
students.add(student2);
Map<Long, List<Student>> collect = students.stream().collect(Collectors.groupingBy(s -> {
return s.getId();
}));
System.out.println(JSON.toJSONString(collect));
}
}
结果
{1:[{"id":1,"name":"小明"}],2:[{"id":2,"name":"小红"}]}
groupingBy 方法参数Function<? super T, ? extends K> classifier
@FunctionalInterface
public interface Function<T, R> {
R apply(T t);
}
Function
是函数式接口,接收一个参数T,返回一个结果R,示例中可以表示为下面这样的,先创建一个Function接口,再将接口当作参数传进去.
Function<Student, Long> groupingByFun = s -> {return s.getId()};
Map<Long, List<Student>> collect = students.stream().collect(Collectors.groupingBy(groupingByFun
));
优化s -> {return s.getId()}
可以简化写法
Function<Student, Long> groupingByFun = s -> {return s.getId()};
// 可以简化成
Function<Student, Long> groupingByFun = s -> s.getId();
// 再一次简化
Function<Student, Long> groupingByFun = Student::getId;
-
两个参数的方法
public static <T, K, A, D> Collector<T, ?, Map<K, D>> groupingBy(Function<? super T, ? extends K> classifier, Collector<? super T, A, D> downstream) { return groupingBy(classifier, HashMap::new, downstream); }
示例
Map<Long, Long> countMap = students.stream().collect(Collectors.groupingBy(Student::getId, Collectors.counting())); System.out.println(JSON.toJSONString(countMap)); // {1:1,2:1}
-
三个参数的方法
public static <T, K, D, A, M extends Map<K, D>> Collector<T, ?, M> groupingBy(Function<? super T, ? extends K> classifier, Supplier<M> mapFactory, Collector<? super T, A, D> downstream) { }
示例
TreeMap<Long, Set<Student>> treeMapSet = students.stream().collect(Collectors.groupingBy(Student::getId, TreeMap::new, Collectors.toSet())); System.out.println(JSON.toJSONString(treeMapSet)); // {1:[{"id":1,"name":"小明"}],2:[{"id":2,"name":"小红"}]}
本篇文章由一文多发平台ArtiPub自动发布
网友评论