I have a List of Foo Objects. If a name appears multiple times, I want to do something to the first Item with that name.
HashMap<String, Foo> map = new HashMap<String, Foo>();
for (Foo bar: this.FooList)
{
if (!map.containsKey(bar.getName()))
{
map.put(bar.getName(), bar);
}
else
{
map.get(bar.getName()).doSomeThing();
}
}
But this is not working, because every name (unique or not) gets thrown in that map. Does the HashMap check only for reference equality and not equality on key objects?
This is the code you need:
Here’s a testbed for it:
Output is: