【问题标题】:How to sum up similar values of Map together如何将Map的相似值汇总在一起
【发布时间】:2023-03-12 19:23:01
【问题描述】:

我在下面有这个输入,它显示了一个人在什么年龄有什么分数。 这存储在像这样Map<Person, Information> 的HashMap 中,Person 类只有double: getScore(),它返回分数,而类Information 有int:getAge(),它返回年龄。类中没有属性名。

   {Person has at Age: 12 (Score: 50)
=alex,
 Person has at Age: 16 (Score: 50)
=miki, 
Person has at Age: 5 (Score: 100)
=shi, 
Person has at Age: 4 (Score: 50)
=rafi, 
Person has at Age: 1 (Score: 50). (Score: 50)
=sharbel, 
Person has at Age: 5 (Score: 0). (Score: 0)
=thomas, 
Person has at Age: 14 (Score: 60). (Score: 60)
=thomy, 
Person has at Age: 14 (Score: 50). (Score: 50)
=angelos,
 Person has at Age: 11 (Score: 50). (Score: 50)
=musti, 
Person has at Age: 11 (Score: 100). (Score: 100)
=aloo,
 Person has at Age: 2 (Score: 50). (Score: 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}

所以请注意[thomy, angelos][musti, aloo] 在一起,这是因为他们的年龄相同,我们认为他们之间的得分最大。

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

【问题讨论】:

  • 请更正此语句“需要什么才能获得具有相同边缘但名称相同的人的最高分,因此预期的输出应该是这样的”。
  • 您可以遍历每个年龄的地图,为地图中的每个人调用getAge(),然后使用getScore() 获取分数。计算得分最高的人并将其放入List<Name>
  • @Kshitij 你能用java代码发表你的想法吗?
  • 为什么 5 岁的人不在您的输出部分中的组中?
  • @jnrdn0011 更新了,谢谢我忘记了

标签: java arrays algorithm hashmap


【解决方案1】:

你可以像这样使用流:

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()))
        );

但我建议使用另一种方式,使用简单的类而不是复杂的 Map。

输出

Person(score=50.0, age=1) - [sharbel]
Person(score=100.0, age=11) - [aloo, musti]
Person(score=50.0, age=12) - [alex]
Person(score=60.0, age=14) - [thomy, angelos]
Person(score=50.0, age=2) - [evi]
Person(score=100.0, age=5) - [shi, thomas]
Person(score=50.0, age=4) - [rafi]
Person(score=50.0, age=16) - [miki]

【讨论】:

  • 我正在检查这个解决方案,你能看到更新的帖子吗?我发布了 getAge() 在一个名为信息Map&lt;Person, Information&gt; 的对象中。您能否相应地更新您的答案?
  • @Catalina 请提出一个新问题,我相信我们会回答你,因为它在 stackoverflow 中知道,如果你想要另一个问题,这个问题应该有我的主题,然后你可以创建一个新的发布您应该描述问题的所有细节的地方
  • 请问这个问题确实与这篇文章有关。我该如何改进您的解决方案,以便我将其得分命名为另外一个名称?像这样[thomy=60, angelos=50]
  • @Catalina 您想将thomy=60 存储为对象还是字符串?在这两种情况下,您都可以将.map(Map.Entry::getValue) 更改为.map(v -&gt; /*Fill object with v.getValue() and e.getKey().getScore()*/)
猜你喜欢
  • 2021-12-11
  • 1970-01-01
  • 2010-12-16
  • 1970-01-01
  • 1970-01-01
  • 2021-10-22
  • 2015-11-16
  • 2019-08-15
相关资源
最近更新 更多