Java 如何将地图的相似值汇总在一起

Java 如何将地图的相似值汇总在一起,java,arrays,algorithm,hashmap,Java,Arrays,Algorithm,Hashmap,我在下面有一个输入,表示一个人在什么年龄有什么分数。 这被存储在一个HashMap中,像这样MapPerson类只有double:getScore()返回分数和,类信息有int:getAge()返回年龄。类中没有属性名称 {一个人在12岁时(分数:50)有 =亚历克斯, 个人在16岁时的成绩(分数:50) =miki, 个人在5岁时的成绩(分数:100) =施, 个人在4岁时的成绩(分数:50) =拉菲, 该人在1岁时(分数:50)已被解雇。(分数:50) =沙贝尔, 该人在5岁时(得分:0)已

我在下面有一个输入,表示一个人在什么年龄有什么分数。 这被存储在一个HashMap中,像这样
Map
Person类只有
double:getScore()
返回分数和,类信息有
int:getAge()
返回年龄。类中没有属性名称

{一个人在12岁时(分数:50)有
=亚历克斯,
个人在16岁时的成绩(分数:50)
=miki,
个人在5岁时的成绩(分数:100)
=施,
个人在4岁时的成绩(分数:50)
=拉菲,
该人在1岁时(分数:50)已被解雇。(分数:50)
=沙贝尔,
该人在5岁时(得分:0)已被解雇。(得分:0)
=托马斯,
人在14岁时(分数:60)有过类似经历。(分数:60)
=汤米,
个人在14岁时(得分:50)有经验。(得分:50)
=angelos,
该人在11岁时(分数:50)有过类似经历。(分数:50)
=musti,
该人在11岁时(分数:100)有过类似经历。(分数:100)
=你好,
该人在2岁时(分数:50)有过类似经历。(分数:50)
=evi}
我需要的是,将年龄相同、得分最高的用户分组。预期输出应如下所示:

{Person has at Age: 12 (Score: 50)
=alex,
 Person has at Age: 16 (Score: 50)
=miki, 
Person has at Age: 5 (Score: 100)
=[shi,thomas], // those are together
Person has at Age: 4 (Score: 50)
=rafi, 
Person has at Age: 1 (Score: 50)
=sharbel, 

Person has at Age: 14 (Score: 60).
=[thomy , angelos], // those are together and we consider the biggest score 60

 Person has at Age: 11 (Score: 100)
=[musti, aloo],  // those are together and we consider the biggest score 100
 Person has at Age: 2 (Score: 50)
=evi}
Map<Person, List<String>> result = origin.entrySet().stream()
        .collect(Collectors.groupingBy(e -> e.getKey().getAge())).entrySet().stream()
        .collect(Collectors.toMap(
                e -> e.getValue().stream()
                        .map(Map.Entry::getKey)
                        .max(Comparator.comparing(Person::getScore))
                        .get(),
                e -> e.getValue().stream()
                        .map(Map.Entry::getValue)
                        .collect(Collectors.toList()))
        );
<> P> > <代码> [ THOMYS,ANGORS] 是一起的,<代码> [Muthi,AlOO] 这是因为它们有相同的年龄,我们认为它们之间的最大得分。


我尝试了许多不同的方法,但没有成功,因此我没有尝试任何实现。

您可以像这样使用流:

{Person has at Age: 12 (Score: 50)
=alex,
 Person has at Age: 16 (Score: 50)
=miki, 
Person has at Age: 5 (Score: 100)
=[shi,thomas], // those are together
Person has at Age: 4 (Score: 50)
=rafi, 
Person has at Age: 1 (Score: 50)
=sharbel, 

Person has at Age: 14 (Score: 60).
=[thomy , angelos], // those are together and we consider the biggest score 60

 Person has at Age: 11 (Score: 100)
=[musti, aloo],  // those are together and we consider the biggest score 100
 Person has at Age: 2 (Score: 50)
=evi}
Map<Person, List<String>> result = origin.entrySet().stream()
        .collect(Collectors.groupingBy(e -> e.getKey().getAge())).entrySet().stream()
        .collect(Collectors.toMap(
                e -> e.getValue().stream()
                        .map(Map.Entry::getKey)
                        .max(Comparator.comparing(Person::getScore))
                        .get(),
                e -> e.getValue().stream()
                        .map(Map.Entry::getValue)
                        .collect(Collectors.toList()))
        );

请更正此语句“需要什么才能获得具有相同边缘但其名称相同的人的最高分,因此预期输出应如下”。您可以针对每个年龄在地图上迭代,为地图中的每个人调用
getAge()
,然后使用
getScore()
获得分数。数一数得分最高的人,并将其放入
列表中
@Kshitij你能用java代码发布你的想法吗?为什么5岁的人不在你的输出部分的一个组中?@jnrdn0011更新,谢谢,我忘了我正在检查这个解决方案,你能看到更新的帖子吗?我发布了getAge()位于名为information
Map
的对象中。你能相应地更新你的答案吗?@Catalina请提出一个新问题,我相信我们会回答你的,正如stackoverflow中所知,如果你想要另一个问题,这个问题应该有我的主题,然后你可以创建一个新的帖子,在那里你应该描述问题的所有细节。请我有一个问题,这个问题与这个帖子相关。我如何改进您的解决方案,以便将名称和分数相加?像这样
[thomy=60,angelos=50]
@Catalina您想将
thomy=60
存储为对象还是字符串?在这两种情况下,您都可以将
.map(map.Entry::getValue)
更改为
.map(v->/*使用v.getValue()和e.getKey().getScore()*/)填充对象。
请参阅: