大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺
@Test
public void r(){
List<Person> javaProgrammers = new ArrayList<Person>() {
{
add(new Person("Elsdon", "1", "Java programmer", "male", 43, 2000));
add(new Person("Elsdon", "1", "Java programmer", "female", 23, 1500));
add(new Person("Elsdon", "Jaycob", "Java programmer", "male", 33, 1800));
add(new Person("Elsdon", "Jaycob", "Java programmer", "female", 32, 1600));
add(new Person("Elsdon", "Jaycob", "Java programmer", "male", 22, 1200));
add(new Person("Maude", "2", "Java programmer", "female", 27, 1900));
add(new Person("Maude", "2", "Java programmer", "male", 30, 2300));
add(new Person("Maude", "Jaimie", "Java programmer", "female", 35, 1700));
add(new Person("Maude", "Jaimie", "Java programmer", "male", 33, 2000));
add(new Person("Maude", "Jaimie", "Java programmer", "female", 34, 1300));
}
};
List<Person> phpProgrammers = new ArrayList<Person>() {
{
add(new Person("Jarrod", "Pace", "PHP programmer", "male", 34, 1550));
add(new Person("Clarette", "Cicely", "PHP programmer", "female", 23, 1200));
add(new Person("Victor", "Channing", "PHP programmer", "male", 32, 1600));
add(new Person("Tori", "Sheryl", "PHP programmer", "female", 21, 1000));
add(new Person("Osborne", "Shad", "PHP programmer", "male", 32, 1100));
add(new Person("Rosalind", "Layla", "PHP programmer", "female", 25, 1300));
add(new Person("Fraser", "Hewie", "PHP programmer", "male", 36, 1100));
add(new Person("Quinn", "Tamara", "PHP programmer", "female", 21, 1000));
add(new Person("Alvin", "Lance", "PHP programmer", "male", 38, 1600));
add(new Person("Evonne", "Shari", "PHP programmer", "female", 40, 1800));
}
};
Map<String, Map<String, List<Person>>> z = javaProgrammers.stream().collect(Collectors.groupingBy(t > t.getFirstName(), Collectors.groupingBy(r -> r.getLastName())));
z.forEach((k, v) -> {
v.forEach((k1, v1) -> {
v1.forEach(v2 -> System.out.println(v2.getFirstName() + v2.getLastName()));
});
});
}
补充下源码的理解
首先groupBy有三个方法,前两个方法是起到一个参数补充作用(有具体知道这个的术语的吗)
第一种是我们常用的方法只需要传递一个Function用于获取key值。 public static <T, K> Collector<T, ?, Map<K, List<T>>> groupingBy(Function<? super T, ? extends K> classifier) { return groupingBy(classifier, toList()); }
这个就是嵌套写法。需要传递一个新的Collector进去。理解起来就是接收Collector类型作为参数之一,同时返回一个Collector类型,这就是嵌套调用的基础。
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); }
真正的实现是第三个方法。 downstreamAccumulator.accept(container, t);从这里我们知道,先执行的外层的 Accumulator方法,
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);
}
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/171299.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...