我设法写了Collector.groupingBy(Function, groupingBy(classifier, toList())
Map<String, Map<Person.Sex, List<Person>>> groupByYearThenSex = personList.stream().collect(
groupingBy(p ->{
if(p.getYear() > 2015) return "New members";
else if(p.getYear() < 2009) return "Senior members";
else return "Normal members";
},
groupingBy(Person::getSex)
)
);
groupByYearThenSex.forEach((String y, Map<Person.Sex, List<Person>> m) ->{
System.out.println("\n" + y);
m.forEach((Person.Sex s, List<Person> p) -> {
System.out.println(" " + s);
p.forEach((Person prsn) -> {
System.out.println(" " + prsn.toString());
});
});
});
相应的结果是:
New members
MALE
Person{name: Gawel, age: 23, sex: MALE}
Normal members
MALE
Person{name: Patryk, age: 34, sex: MALE}
Person{name: Pawel, age: 21, sex: MALE}
Person{name: Bolek, age: 12, sex: MALE}
Person{name: Lolek, age: 12, sex: MALE}
FEMALE
Person{name: Jola, age: 70, sex: FEMALE}
Person{name: Ala, age: 25, sex: FEMALE}
Senior members
FEMALE
Person{name: Iwona, age: 34, sex: FEMALE}
Person{name: Oliwia, age: 17, sex: FEMALE}
现在我正在尝试制作多级地图,其中仅列出最老的成员,如下所示:
New members
MALE
Person{name: Gawel, age: 23, sex: MALE}
Normal members
MALE
Person{name: Patryk, age: 34, sex: MALE}
FEMALE
Person{name: Jola, age: 70, sex: FEMALE}
Senior members
FEMALE
Person{name: Iwona, age: 34, sex: FEMALE}
以下代码不起作用。我得到&#34;推理变量RR有不兼容的边界错误,我认为Function<R,RR>
=&gt; collectingAndThen(reducing(), Optional)
。有什么方法可以减少人流以获得最老的并获得上面的输出?
Map<String, Map<Person.Sex, List<Person>>> groupByYearThenSexOldest = personList.stream().collect(
groupingBy(p ->{
if(p.getYear() > 2015) return "New members";
else if(p.getYear() < 2009) return "Senior members";
else return "Normal members";
},
groupingBy(Person::getSex, collectingAndThen(
reducing((p1, p2) -> p1.getAge() > p2.getAge() ? p1 : p2),
Optional::get)
)
)
);
答案 0 :(得分:0)
问题是地图的类型参数。它应该为您提供Map<String, Map<Person.Sex, Person>>
而不是Map<String, Map<Person.Sex, List<Person>>>
,因为您正在减少列表。
但您可以将第二个groupingBy
收藏家与maxBy
和collectingAndThen
合并:
groupingBy(Person::getSex, collectingAndThen(maxBy(Comparator.comparingInt(Person::getAge)), Optional::get)
有时,如果需要按多个字段进行分组,则可以使用对字段进行分组的列表(假设它们具有正确的equals和hashcode方法)。例如:
Function<Person, String> groupingYear = p -> {
if (p.getYear() > 2015) return "New members";
else if (p.getYear() < 2009) return "Senior members";
else return "Normal members";
};
Map<Object, Optional<Person>> map2 =
personList.stream()
.collect(groupingBy(p -> Arrays.asList(groupingYear.apply(p), p.getSex()), maxBy(Comparator.comparingInt(Person::getAge))));
给出:
[Normal members, MALE] => Optional[Person{name='Patryk', age=34, sex=MALE}]
[Senior members, FEMALE] => Optional[Person{name='Iwona', age=34, sex=FEMALE}]
[Normal members, FEMALE] => Optional[Person{name='Jola', age=70, sex=FEMALE}]
[New members, MALE] => Optional[Person{name='Gawel', age=23, sex=MALE}]
但是,您可以先使用toMap
收集器,并比较两个值组合在一起时合并函数的年龄:
Map<Object, Person> map =
personList.stream()
.collect(toMap(p -> Arrays.asList(groupingYear.apply(p), p.getSex()),
p -> p,
(p1, p2) -> {
if(p1.getAge() > p2.getAge()) return p1;
else return p2;
},
HashMap::new));
Object
中有一个Map
作为关键字(实际类型很糟糕且写得很长),但你可以做的是创建一个专用的类MemberStatusAndSex
属性(而不是List
- 不要忘记为此类实现equals和hashcode),并在结尾处获得Map<MemberStatusAndSex, Person>
。