Reputation: 55
Good morning,
Sorry if this has already been asked but i'm stuck on a java project that I'm building.
It is a program that allows the user to insert a bet (for example the predicted result of a sports match) and at a later time allows an user to insert the definitive result of said match.
This is done by 2 LinkedHashMaps. One with a key value pair of User and predicted score and one of User and definitive score.
The order of the keys in both LinkedHashMaps will be the same as they get their input for the key from another Array with the names of the users. So only the predicted and the definitive score gets set every time. (The key gets set by calling .get on the array with the names of the users).
What I want to do is compare the value in the LinkedHashMap with the predicted scores to the value in LinkedHashMap with the definitive score. This however, needs to be compared per key-value pair. So I can't just check for the equality of both LinkedHashMaps. For example, with four users only one can have predicted the right score. If I check the equality of both LinkedHashMaps to another it checks the key-value pairs as a whole. I need to get one boolean result per key-value pair.
I thought of checking the equality of the index posisition of the value but this not how LinkedHashMaps work. The LinkedHashMap Key and the LinkedHashMap value are both custom objects (the key being an instance of a class with a set string in it, the value being an enum).
How would I go about doing this?
I'm currently thinking of first calling .toArray on both LinedHashMap per " index " posisition and checking the equality of that one to a set amount.. like this; (voorspellingen is LinkedHashMap 1, uitslagen is the other LinkedHashMap)
voorspellingen.keySet().toArray()[0])
uitslagen.keySet().toArray()[0])
if (etc.)
But i'm not sure if this would be the most "effective" way of doing it...
Upvotes: 1
Views: 1163
Reputation: 106
I would have gone putting in a map instead of two with the values in the form of an object. But the trade off over here would be that we would lose the sequence of the defitive value that comes in.
import java.util.LinkedHashMap;
public class CompareTwoBets {
public static void main(String[] args) {
LinkedHashMap<String, Bet> map = new LinkedHashMap<>();
// First the users add prediction
map.put("jack", new Bet(1));
map.put("Bronwen", new Bet(2));
map.put("Cyrus", new Bet(2));
map.put("Brent", new Bet(5));
// now the users key in the definitive value
Bet bet = map.get("jack");
bet.setDefinitive(5);
map.put("jack", bet);
bet = map.get("Bronwen");
bet.setDefinitive(5);
map.put("Bronwen", bet);
bet = map.get("Cyrus");
bet.setDefinitive(5);
map.put("Cyrus", bet);
bet = map.get("Brent");
bet.setDefinitive(5);
map.put("Brent", bet);
System.out.println(map);
// process to know who put the bet accurately
map.forEach((user, x) -> {
if(x.definitive == x.predicted) {
System.out.println("User " + user +" predicted accurately." );
}
else {
System.out.println("User " + user +" predicted wrongly." );
}
});
}
}
public class User{
String firstName;
String lastName;
public User(String firstName, String lastName){
this.firstName = firstName;
this.lastName = lastName;
}
}
public class Bet {
int predicted;
int definitive;
public Bet(int predicted) {
this.predicted = predicted;
}
public void setDefinitive(int definitive) {
this.definitive = definitive;
}
}
Upvotes: 0
Reputation: 93
There are many ways to do this, but probably the most legible is to iterate over the key set of one map:
var voorspellingen = new LinkedHashMap<String, Integer>(); //ex: <user name, predicted score difference>
var uitslagen = new LinkedHashMap<String, Integer>();
//... add data to maps here
//Let's get a list of the users that gave good predictions
Predicate<String> correctPrediction = name -> (uitslagen.get(name) != null
&& uitslagen.get(name).equals(voorspellingen.get(name)));
List<String> correctUsers = uitslagen.keySet().stream()
.filter(correctPrediction)
.collect(Collectors.toList());
Note, this solution is not necessarily the fastest. I don't know why you choose LinkedHashMap, but if you have millions of users it might indeed be better to group the prediction and the result in the same object.
Upvotes: 1
Reputation: 25
I suggest to use just one HashMap, and for the value, use an object that holds both the two results.
class Results
{
int res1;
int res2;
}
Use the above class as your value class, and so you just need to iterate over the HashMap for one time to compare whether the two results in the object are equal or not.
Upvotes: 1