I am getting unexpected results from a Set of Maps (Set<Map<String,String>>), where s.contains(s.iterator().next()) is false.
The set in question contains only one map that is [{=262.666666666666667}] (the empty String mapped to the String 262.666666666666667).
I have been unable to put together a minimal working example that replicates the problem as the following outputs true:
Set s = new HashSet<Map<String,String>>();
Map<String,String> m = new HashMap<>();
m.put("", "262.666666666666667");
s.add(m);
System.out.println(s.contains(s.iterator().next()));
HashMap does not override hashCode but Abstract map does (see below) so I don't see the problem of putting a HashMap in a HashSet.
public int hashCode()
{
int h = 0;
Iterator<Entry<K,V>> i = entrySet().iterator();
while (i.hasNext())
h += i.next().hashCode();
return h;
}
What is the reason for this behaviour and how can I fix it?
Edit: Thanks at doublep and morgano, I indeed modified the map after adding it and resolved the issue by adding after the modification instead of before.
truewhen executed on a single thread.