如何在Java 8 groupingby函数形成的结果中添加标签? [英] How do you add labels to the result formed by Java 8 groupingby function?
问题描述
我目前正在上课,
public class Person {
private String country;
private String age;
private String name;
}
以此Person类的列表作为参数,
Taking a List of this Person class as a argument,
List<Person>
我使用 Java 8 group by(集合)函数将它们按以下数据结构进行了分组:
I managed to group them in the following data structure using Java 8 group by (collection) function :
Map<String, Map<String, Set<String>>>
示例:
USA={
21=
[
John,
Peter.
Andrew
],
22=
[
Eric,
Mark
]
]
},
France = {
etc....
下面是我的功能:
public static Map<String, Map<String, Set<String>>> getNestedMap(List<Person> persons) {
return persons.stream().collect(
groupingBy(Person::getCountry,
groupingBy(Person::getAge,
mapping(Person::getName, toSet())
)));
}
但是,我希望我的数据结构看起来像这样,每个级别都有标签. Java 8分组依据(收集)有什么方法可以帮助实现这一目标?还是有更好的方法?
However, I wanted my data structure to look like this, with labels for each level. Is there a way Java 8 group by (collection) can help achieve this? or is there better way?
Country = USA,
AgeList = {
Age = 21,
People =
[
[Name = John],
[Name = Peter],
[Name = Andrew]
],
Age = 22,
People =
[
[Name = Eric],
[Name = Mark]
]
]
},
Country = France,
AgeList = {
etc....
推荐答案
您从输入(一系列Person
实例)和工具(.groupingBy()
)开始,而不是输入和所需的输出.首先确定然后,以确定哪些工具是将输入转换为所需输出的最合适方法.
You've started with an input (a series of Person
instances) and a tool (.groupingBy()
), rather than an input and a desired output. Identify that first, then determine which tool(s) are the most appropriate way to transform the input into the desired output.
例如,您可能想要以Country
对象结尾,该对象包含名称和Age
对象列表,每个对象都包含年龄和一组People
对象.考虑到这种结果结构,您可以将单个.groupingBy()
传递给按国家/地区分组,然后将结果列表传递给Country(String name, List<Person> people)
构造函数,然后依次由另一个.groupingBy()
传递给按年龄分组,并且调用Age(int age, List<Person>)
构造函数.
For example, you might want to end up with a Country
object, that contains a name and a list of Age
objects, each of which contains an age and a set of People
objects. With that resulting structure in mind you could you could do a single .groupingBy()
pass to group by country, and pass the resulting lists into a Country(String name, List<Person> people)
constructor, which then in turn does another .groupingBy()
pass to group by age, and invokes an Age(int age, List<Person>)
constructor.
如果您的目标是然后将此结构序列化为JSON形式的字符串,则现在您已经可以在Country.toString()
中轻松地这样做了,因为您已经在所需的结构中存储了数据.
If your goal is to then serialize this structure into a JSON-ish string, you can easily do so in Country.toString()
, now that you have the data in the structure you need.
将结构性关注点(例如,从List<Foo>
转换为复杂的东西,例如Map<Bar, Map<Baz, Set<Foo>>>
)与表示性关注点(例如,然后渲染该复杂结构的字符串表示形式)分开始终是一个好主意.与一次性完成所有步骤相比,您将发现分别解决这两个步骤通常要容易得多且容易维护.
It's always a good idea to separate structural concerns (like transforming from a List<Foo>
to something complex like a Map<Bar, Map<Baz, Set<Foo>>>
) from representational concerns (like then rendering a string representation of that complex structure). You'll find solving the two steps separately is often significantly easier - and easier to maintain - than doing it all in one fell swoop.
这篇关于如何在Java 8 groupingby函数形成的结果中添加标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!