如何将一个哈希图中的值与另一个哈希图中的相同值进行比较?

早上好,


对不起,如果已经有人问过这个问题,但我被困在我正在构建的 Java 项目上。


它是一个程序,允许用户插入赌注(例如体育比赛的预测结果),然后允许用户插入所述比赛的最终结果。


这是由 2 个 LinkedHashMap 完成的。一个具有用户和预测分数的键值对以及用户和确定分数之一。


两个 LinkedHashMap 中键的顺序将与它们从另一个具有用户姓名的 Array 中获取键的输入相同。因此,每次都只设置预测的和最终的分数。(通过在具有用户名称的数组上调用 .get 来设置密钥)。


我想要做的是将 LinkedHashMap 中的值与预测分数进行比较,并将 LinkedHashMap 中的值与最终分数进行比较。然而,这需要按键值对进行比较。所以我不能只检查两个 LinkedHashMaps 的相等性。例如,对于四个用户,只有一个用户可以预测正确的分数。如果我检查两个 LinkedHashMap 与另一个的相等性,它会检查整个键值对。我需要为每个键值对获得一个布尔结果。


我想检查值的索引位置是否相等,但这不是 LinkedHashMaps 的工作方式。LinkedHashMap Key 和 LinkedHashMap 值都是自定义对象(键是一个类的实例,其中有一个设置字符串,值是一个枚举)。


我该怎么做呢?


我目前正在考虑首先在每个“索引”位置的 LinedHashMap 上调用 .toArray 并检查该位置与设定数量的相等性..像这样;(voorspellingen 是 LinkedHashMap 1,uitslagen 是另一个 LinkedHashMap)


voorspellingen.keySet().toArray()[0])

uitslagen.keySet().toArray()[0])

if (etc.)

但我不确定这是否是最“有效”的方式......


月关宝盒
浏览 184回答 3
3回答

慕田峪7331174

有很多方法可以做到这一点,但最清晰的方法可能是迭代一张地图的键集:&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;voorspellingen&nbsp;=&nbsp;new&nbsp;LinkedHashMap<String,&nbsp;Integer>();&nbsp;//ex:&nbsp;<user&nbsp;name,&nbsp;predicted&nbsp;score&nbsp;difference> &nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;uitslagen&nbsp;=&nbsp;new&nbsp;LinkedHashMap<String,&nbsp;Integer>(); &nbsp;&nbsp;&nbsp;&nbsp;//...&nbsp;add&nbsp;data&nbsp;to&nbsp;maps&nbsp;here &nbsp;&nbsp;&nbsp;&nbsp;//Let's&nbsp;get&nbsp;a&nbsp;list&nbsp;of&nbsp;the&nbsp;users&nbsp;that&nbsp;gave&nbsp;good&nbsp;predictions &nbsp;&nbsp;&nbsp;&nbsp;Predicate<String>&nbsp;correctPrediction&nbsp;=&nbsp;name&nbsp;->&nbsp;(uitslagen.get(name)&nbsp;!=&nbsp;null&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&&&nbsp;uitslagen.get(name).equals(voorspellingen.get(name))); &nbsp;&nbsp;&nbsp;&nbsp;List<String>&nbsp;correctUsers&nbsp;=&nbsp;uitslagen.keySet().stream() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.filter(correctPrediction) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.collect(Collectors.toList());请注意,此解决方案不一定是最快的。我不知道你为什么选择 LinkedHashMap,但如果你有数百万用户,将预测和结果分组在同一个对象中确实可能更好。

精慕HU

我建议只使用一个 HashMap,并且对于值,使用一个包含两个结果的对象。class Results{&nbsp; &nbsp; int res1;&nbsp; &nbsp; int res2;}使用上面的类作为你的值类,所以你只需要遍历HashMap一次来比较对象中的两个结果是否相等。

慕田峪4524236

我会用对象形式的值放入一张地图而不是两张地图。但是这里的权衡是我们将失去进入的最终值的序列。import java.util.LinkedHashMap;public class CompareTwoBets {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; LinkedHashMap<String, Bet> map = new LinkedHashMap<>();&nbsp; &nbsp; &nbsp; &nbsp; // First the users add prediction&nbsp; &nbsp; &nbsp; &nbsp; map.put("jack", new Bet(1));&nbsp; &nbsp; &nbsp; &nbsp; map.put("Bronwen", new Bet(2));&nbsp; &nbsp; &nbsp; &nbsp; map.put("Cyrus", new Bet(2));&nbsp; &nbsp; &nbsp; &nbsp; map.put("Brent", new Bet(5));&nbsp; &nbsp; &nbsp; &nbsp; // now the users key in the definitive value&nbsp; &nbsp; &nbsp; &nbsp; Bet bet = map.get("jack");&nbsp; &nbsp; &nbsp; &nbsp; bet.setDefinitive(5);&nbsp; &nbsp; &nbsp; &nbsp; map.put("jack", bet);&nbsp; &nbsp; &nbsp; &nbsp; bet = map.get("Bronwen");&nbsp; &nbsp; &nbsp; &nbsp; bet.setDefinitive(5);&nbsp; &nbsp; &nbsp; &nbsp; map.put("Bronwen", bet);&nbsp; &nbsp; &nbsp; &nbsp; bet = map.get("Cyrus");&nbsp; &nbsp; &nbsp; &nbsp; bet.setDefinitive(5);&nbsp; &nbsp; &nbsp; &nbsp; map.put("Cyrus", bet);&nbsp; &nbsp; &nbsp; &nbsp; bet = map.get("Brent");&nbsp; &nbsp; &nbsp; &nbsp; bet.setDefinitive(5);&nbsp; &nbsp; &nbsp; &nbsp; map.put("Brent", bet);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(map);&nbsp; &nbsp; &nbsp; &nbsp; // process to know who put the bet accurately&nbsp; &nbsp; &nbsp; &nbsp; map.forEach((user, x) -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(x.definitive == x.predicted) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("User&nbsp; " + user +" predicted accurately." );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("User&nbsp; " + user +" predicted wrongly." );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }}public class User{&nbsp; &nbsp; String firstName;&nbsp; &nbsp; String lastName;&nbsp; &nbsp; public User(String firstName, String lastName){&nbsp; &nbsp; &nbsp; &nbsp; this.firstName = firstName;&nbsp; &nbsp; &nbsp; &nbsp; this.lastName = lastName;&nbsp; &nbsp; }&nbsp; &nbsp;}public class Bet {&nbsp; &nbsp; int predicted;&nbsp; &nbsp; int definitive;&nbsp; &nbsp; public Bet(int predicted) {&nbsp; &nbsp; &nbsp; &nbsp; this.predicted = predicted;&nbsp; &nbsp; }&nbsp; &nbsp; public void setDefinitive(int definitive) {&nbsp; &nbsp; &nbsp; &nbsp; this.definitive = definitive;&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java