I want to compare 2 HashMap Objects, and i want to iterate the values Collection, somthing like this answer.
But can I be sure that the 2 object Iterator will go in the same order?
If not, i thought about the next one, but it not so good looking:
public class MyObject {
HashMap<Integer, String> myMap = new HashMap<Integer, String>();
@Override
public boolean equals(Object obj) {
if(!(obj instanceof MyObject))
return false;
MyObject other = (MyObject)obj;
if(this.myMap.size() != other.myMap.size())
return false;
for (Iterator<Entry<Integer, String>> it = myMap.entrySet().iterator(); it.hasNext(); ) {
Entry<Integer, String> entry = it.next();
String otherValue = other.myMap.get(entry.getKey());
if(otherValue == null || !entry.getValue().equals(otherValue))
return false;
}
return true;
}
}