Reputation:
I have a HashMap
in Java (named queue) which with Integer
as the key and List
of messages as a value related to each key. I want to visit all values of List<messages>
, and compare each entry with the particular value that I feed it to the function foo(Message m)
. If the entered value equal the value of HashMap, then get the key of this value and store it in a list. Here is my function:
public static List<Integer> getKey(Message m,MessageList m1)
{
List<Integer> l = new ArrayList<Integer>();
for(Map.Entry<Integer, List<Message>> entry:m1.queue.entrySet()) {
if(m.equals(entry.getValue())) {
l.add(entry.getKey());
}
}
return l;
}
But this function doesn't work. Is there any wrong?
Upvotes: 0
Views: 112
Reputation: 12633
The value is of type List<Message>
but you compare it to a single Message
. Use List::contains
:
public static List<Integer> getKey(Message m,MessageList m1)
{
List<Integer> l = new ArrayList<Integer>();
for(Map.Entry<Integer, List<Message>> entry:m1.queue.entrySet()) {
if(entry.getValue != null && entry.getValue().contains(m)) {
l.add(entry.getKey());
}
}
return l;
}
Upvotes: 1