我有一个简单的课程Person
:
class Person {
String firstName;
String lastName;
//getter, setter, constructor, toString
}
以及类似的输入列表Persons
:
List<Person> myList = List.of(
new Person("Helena", "Graves"),
new Person("Jasmine", "Knight"),
new Person("Phoebe", "Reyes"),
new Person("Aysha", "Graham"),
new Person("Madeleine", "Jenkins"),
new Person("Christina", "Johnson"),
new Person("Melissa", "Carpenter"),
new Person("Marie", "Daniel"),
new Person("Robin", "French"),
new Person("Tamara", "Wyatt"),
new Person("Freya", "Montgomery"),
new Person("Lacey", "Todd"),
new Person("Heather", "Parker"),
new Person("Lauren", "Wright"),
new Person("Annie", "Bradley")
);
现在我需要按人的姓氏的第一个字符对上面的列表进行分组,然后再次对这些组进行分组,以便A-H
所有I-N
以O-Z
.
我已经可以按姓氏的第一个字符对列表进行分组:
myList.stream()
.collect(Collectors.groupingBy(p -> String.valueOf(p.getLastName().charAt(0))))
.entrySet()
.forEach(System.out::println);
这给了我:
P=[Person{Heather, Parker}]
B=[Person{Annie, Bradley}]
R=[Person{Phoebe, Reyes}]
C=[Person{Melissa, Carpenter}]
T=[Person{Lacey, Todd}]
D=[Person{Marie, Daniel}]
F=[Person{Robin, French}]
W=[Person{Tamara, Wyatt}, Person{Lauren, Wright}]
G=[Person{Helena, Graves}, Person{Aysha, Graham}]
J=[Person{Madeleine, Jenkins}, Person{Christina, Johnson}]
K=[Person{Jasmine, Knight}]
M=[Person{Freya, Montgomery}]
很难从这里开始,因为我需要进一步汇总上述内容以获得包含三个条目/键的地图。期望的输出:
Map<String, List<Person>> result = ...
A-H = [Person{Helena, Graves}, Person{Aysha, Graham}, Person{Melissa, Carpenter}, Person{Marie, Daniel}, Person{Robin, French}, Person{Annie, Bradley}]
I-N = [Person{Jasmine, Knight}, Person{Madeleine, Jenkins}, Person{Christina, Johnson}, Person{Freya, Montgomery}]
O-Z = [Person{Phoebe, Reyes}, Person{Tamara, Wyatt}, Person{Lacey, Todd}, Person{Heather, Parker}, Person{Lauren, Wright}]