Weird equals() result with Map/Set object graph - java

Investigating a special case where some objects didn't equal as they should and came to this simple test case that simplifies my issue.
When running this with JUnit in Eclipse with jdk8u152 the last assertEquals fails, can anyone explain why?
It's something with Set/HashSet because if I change as,bs to be ArrayList's instead the final assertEquals goes through.
#Test
public void test()
{
String list = "list";
String object = "object";
String value = "value";
Map<String, Object> a = new HashMap<>();
Map<String, Object> b = new HashMap<>();
assertEquals(a, b);
Set<Object> as = new HashSet<>();
Set<Object> bs = new HashSet<>();
a.put(list, as);
b.put(list, bs);
assertEquals(a, b);
Map<String, Object> ao = new HashMap<>();
as.add(ao);
Map<String, Object> bo = new HashMap<>();
bs.add(bo);
assertEquals(a, b);
ao.put(object, value);
bo.put(object, value);
assertEquals(a, b);
}

You're mutating the elements of the sets. That leads to unspecified behaviour.
From the JavaDoc:
Great care must be exercised if mutable objects are used as set elements. The behavior of a set is not specified if the value of an object is changed in a manner that affects equals comparisons while the object is an element in the set.

You are adding ao and bo HashMaps to the HashSets as and bs.
Later you mutate ao and bo by putting a new entry in each of them.
This means that the hashCode that was used to place ao in as is no longer the current hashCode of ao, and the hashCode that was used to place bo in bs is no longer the current hashCode of bo.
As a result, AbstractSet's equals cannot locate the element of one Set in the other Set, so it concludes that as is not equal to bs. As a result a is not equal to b.
Here's the implementation of AbstractSet's equals. You can see that it uses containsAll, which in turns calls contains(), which relies on the hashCode of the searched element. Since that hashCode has changed after the element was added to the Set, contains() doesn't find the element.
public boolean equals(Object o) {
if (o == this)
return true;
if (!(o instanceof Set))
return false;
Collection<?> c = (Collection<?>) o;
if (c.size() != size())
return false;
try {
return containsAll(c);
} catch (ClassCastException unused) {
return false;
} catch (NullPointerException unused) {
return false;
}
}
If you mutate an element of a HashSet in a way that affects the result of equals or hashCode, you must remove the element from the HashSet prior to the update and add it again after the update.
Adding the following remove and add calls will cause a to be equal to b in the end:
....
assertEquals(a, b);
bs.remove (bo); // added
as.remove (ao); // added
ao.put(object, value);
bo.put(object, value);
as.add (ao); // added
bs.add (bo); // added
assertEquals(a, b);

That is because of the hascode implementation of HashMap which is basically x-or of key and value. If key or value is null then hascode will be zero. Hence all empty hashmaps will have hashcode as zero.
/*hashcode of HashMap*/
public final int hashCode() {
return Objects.hashCode(key) ^ Objects.hashCode(value);
}
/*hashcode of object*/
public static int hashCode(Object o) {
return o != null ? o.hashCode() : 0;
}
Upon adding key value pairs the hashcode value changes.

Related

Java TreeMap put vs HashMap put, custom Object as key

My objective was to use the TreeMap to make Box key objects sorted by Box.volume attribute while able to put keys distinct by the Box.code. Is it not possible in TreeMap?
As per below test 1, HashMap put works as expected, HashMap keeps both A, B key objects, but in test 2, TreeMap put doesn't treat D as a distinct key, it replaces C's value, note that i used the TreeMap comparator as Box.volume, because i want keys to be sorted by volume in TreeMap.
import java.util.*;
public class MapExample {
public static void main(String[] args) {
//test 1
Box b1 = new Box("A");
Box b2 = new Box("B");
Map<Box, String> hashMap = new HashMap<>();
hashMap.put(b1, "test1");
hashMap.put(b2, "test2");
hashMap.entrySet().stream().forEach(o-> System.out.println(o.getKey().code+":"+o.getValue()));
//output
A:test1
B:test2
//test 2
Box b3 = new Box("C");
Box b4 = new Box("D");
Map<Box, String> treeMap = new TreeMap<>((a,b)-> Integer.compare(a.volume, b.volume));
treeMap.put(b3, "test3");
treeMap.put(b4, "test4");
treeMap.entrySet().stream().forEach(o-> System.out.println(o.getKey().code+":"+o.getValue()));
//output
C:test4
}
}
class Box {
String code;
int volume;
public Box(String code) {
this.code = code;
}
#Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Box box = (Box) o;
return code.equals(box.code);
}
#Override
public int hashCode() {
return Objects.hash(code);
}
}
Thank you
TreeMap considers 2 keys for which the comparison method returns 0 to be identical, even if they are not equal to each other, so your current TreeMap cannot contain two keys with the same volume.
If you want to keep the ordering by volume and still have multiple keys with the same volume in your Map, change your Comparator's comparison method to compare the Box's codes when the volumes are equal. This way it will only return 0 if the keys are equal.
Map<Box, String> treeMap = new TreeMap<>((a,b)-> a.volume != b.volume ? Integer.compare(a.volume, b.volume) : a.code.compareTo(b.code));
Now the output is:
C:test3
D:test4
b3 and b4 has the same volume, that is 0 (int default value).
For it work, assign a value to the Box volume variables before comparing.

Java's HashMap key replacement when storing existing value

According to Java HashMap documentation, put method replaces the previously contained value (if any): https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html#put-K-V-
Associates the specified value with the specified key in this map. If
the map previously contained a mapping for the key, the old value is
replaced.
The documentation however does not say what happens to the (existing) key when a new value is stored. Does the existing key get replaced or not? Or is the result undefined?
Consider the following example:
public class HashMapTest
{
private static class Key {
private String value;
private Boolean b;
private Key(String value, Boolean b) {
this.value = value;
this.b = b;
}
#Override
public int hashCode()
{
return value.hashCode();
}
#Override
public boolean equals(Object obj)
{
if (obj instanceof Key)
{
return value.equals(((Key)obj).value);
}
return false;
}
#Override
public String toString()
{
return "(" + value.toString() + "-" + b + ")";
}
}
public static void main(String[] arg) {
Key key1 = new Key("foo", true);
Key key2 = new Key("foo", false);
HashMap<Key, Object> map = new HashMap<Key, Object>();
map.put(key1, 1L);
System.out.println("Print content of original map:");
for (Entry<Key, Object> entry : map.entrySet()) {
System.out.println("> " + entry.getKey() + " -> " + entry.getValue());
}
map.put(key2, 2L);
System.out.println();
System.out.println("Print content of updated map:");
for (Entry<Key, Object> entry : map.entrySet()) {
System.out.println("> " + entry.getKey() + " -> " + entry.getValue());
}
}
}
When I execute the following code using Oracle jdk1.8.0_121, the following output is produced:
Print content of original map:
> (foo-true) -> 1
Print content of updated map:
> (foo-true) -> 2
Evidence says that (at least on my PC) the existing key does not get replaced.
Is this the expected/defined behaviour (where is it defined?) or is it just one among all the possible outcomes? Can I count on this behaviour to be consistent across all Java platforms/versions?
Edit: this question is not a duplicate of What happens when a duplicate key is put into a HashMap?. I am asking about the key (i.e. when you use multiple key instances that refer to the same logical key), not about the values.
From looking at the source, it doesn't get replaced, I'm not sure if it's guaranteed by the contract.
if (e != null) { // existing mapping for key
V oldValue = e.value;
if (!onlyIfAbsent || oldValue == null)
e.value = value;
afterNodeAccess(e);
return oldValue;
}
It finds the existing mapping and replaces the value, nothing is done with the new key, they should be the same and immutable, so even if a different implementation can replace the key it shouldn't matter.
You can't count on this behavior but you should write your code in a way that it won't matter.
When a new pair is added, the map uses hasCode,equals to check if the key already present in the map. If the key already exists the old value is replaced with a new one. The key itself remains unmodified.
Map<Integer,String> map = new HashMap<>();
map.put(1,"two");
System.out.println(map); // {1=two}
map.put(1,"one");
System.out.println(map); // {1=one}
map.put(2,"two");
System.out.println(map); // {1=one, 2=two}
There is an issue with your equals and hashCode contract. ke1 and key2 are identical according to your implementation:
#Override
public boolean equals(Object obj)
{
if (obj instanceof Key)
{
return value.equals(((Key)obj).value);
}
return false;
}
you need to compare Boolean b as well
Key other = (Key) obj;
return value.equals(other.value) && b.equals(other.b);
The same rule apples to hasCode
#Override
public int hashCode()
{
return value.hashCode();
}
return value.hashCode() + b.hashCode();
with these changes key1 and key2 are different
System.out.println(key1.equals(key2));
and the output for your map will be
> (foo-true) -> 1
> (foo-false) -> 2
It is not replaced - neither it should. If you know how a HashMap works and what hashCode and equals is (or more precisely how they are used) - the decision of not touching the Key is obvious.
When you put the other Key/Entry in the map for the second time, that key is first look-up in the map - according to hashCode/equals, so according to the map IFF keys have the same hashCode and are equal according to equals they are the same. If so, why replace it? Especially since if it would have been replaced, that might rigger additional operations or at least additional code to not trigger anything else if keys are equal.
Apparently the current HashSet implementation relies on this HashMap behaviour in order to be compliant to the HashSet documentation.
With that i mean that when you add a new element in an HashSet the documentation says that if you try to add an element in an HasSet that already contains the element, the HashSet is not changed and so the element is not substituted,
In the openjdk8 implementation the HashSet uses an HashMap keys to hold the values and in the HashSet.add method it calls the HashMap.put method to add the value, thus relying on the fact that the put method will not substitute the object
Although this still not a direct specification in the documentation and it's subject to variations in the JRE implementation, it probably provides a stronger
assurance that this will probably not change in the future

How to remove array list records from other array list which is holding pojo class object

Hi i want to remove values from one array list to another array list where both are using model class of students and which are not in a sorted order.
Example:
public class Test {
public static void main(String[] args) {
model obj1 = new model();
model obj2 = new model();
model obj3 = new model();
model obj6 = new model();
model obj5 = new model();
model obj4 = new model();
obj1.setCity("pune");
obj1.setName("aakshi");
obj1.setSalary("22");
obj1.setState("MH");
obj2.setCity("pune");
obj2.setName("aakshi");
obj2.setSalary("23");
obj2.setState("MH");
obj3.setCity("pune");
obj3.setName("aakshi");
obj3.setSalary("24");
obj3.setState("MH");
obj4.setCity("pune");
obj4.setName("aakshi");
obj4.setSalary("22");
obj4.setState("MH");
obj5.setCity("pune");
obj5.setName("aakshi");
obj5.setSalary("23");
obj5.setState("MH");
obj6.setCity("pune");
obj6.setName("aakshi");
obj6.setSalary("24");
obj6.setState("MH");
ArrayList<model> List1 =new ArrayList<model>();
ArrayList<model> list2 = new ArrayList<model>();
List1.add(obj1);
List1.add(obj2);
List1.add(obj3);
list2.add(obj4);
list2.add(obj5);
list2.add(obj6);
for (model tableRow: List1) {
System.out.println("record to be deleted "+tableRow);
list2.remove(tableRow);
System.out.println("records in list"+list2);
}
}
}
Here it is not removing the element from list 1 to list 2, can anyone provide me the solution.
Sorry about my previous answer. I did not understand your question. I think I understand now. You want to take all the elements in List1 and remove their equivalencies in list2.
First and foremost, List1 should be list1 and the class model should be Model in order to follow proper Java naming conventions.
Now, as for the answer, read the documentation on the remove() method:
Removes the first occurrence of the specified element from this list, if it is present. If the list does not contain the element, it is unchanged. More formally, removes the element with the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))) (if such an element exists). Returns true if this list contained the specified element (or equivalently, if this list changed as a result of the call).
This means that, for instance, object4 will only be removed from list2 if object4.equals(object1) (at least in the case of this application). In order to ensure this works, you'll have to override the .equals() method in the model class (which should be named Model - again, proper naming conventions):
public class Model{
private String city, name, salary, state;
//...setters and getters
#Override
public int hashCode(){
int result = (city != null ? city.hashCode() : 0);
result = 31 * result + (name != null ? name.hashCode() : 0);
result = 31 * result + (salary != null ? salary.hashCode() : 0);
result = 31 * result + (state != null ? state.hashCode() : 0);
return result;
}
#Override
public boolean equals(Object o){
if(!(o instanceOf Model))
return false;
Model oModel = (Model) o;
if(oModel.getCity().equals(city) &&
oModel.getName().equals(name) &&
oModel.getSalary().equals(salary) &&
oModel.getState().equals(state))
return true;
return false;
}
}
This override method will ensure that .remove() and .removeAll() will remove an object o or collection of objects from the specified list such that said list contains(o) (or each object in the collection to be removed). As you had it:
for(Model m : list1){
list2.remove(m);
}
Or, a more elegant approach:
list2.removeAll(list1);
To reiterate, remove(Object o) and removeAll(Collection<?> c) remove an object or objects from a list so long as said list .contains(o) (or, when using .removeAll, .contains((c.get(i)) in a common iteration.
Then, it is important to know that .contains(Object o) returns true if and only if the specified list has an object whose .equals(o) returns true, as shown in the documentation for the contains() method:
Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that (o==null ? e==null : o.equals(e))
In order for this to be the case, you must define (override) .equals in the Model class.
EDIT:
As Vladimir Vagaytsev reminded me, it is crucial that you override hashCode whenever overriding equals to ensure that hashCode is also a derivative of the object's relevant values for hash collections. I included said method in the above source code.

How to override equals for two Maps of <String, Object>?

I have a class that has a Map<String, Object> field (the keys are Strings, the values are Objects that have correctly implemented the "equals" method for comparison).
I would like to override equals for this class in a way that only returns true if the Maps have equal mappings between keys and values.
Here is my attempt:
// Assumes that the Object values in maps have correctly implemented the equals method.
private boolean mapsEqual(Map<String, Object> attributes)
{
if (this.attributes_.keySet().size() != attributes.keySet().size() ||
this.attributes_.values().size() != attributes.values().size())
return false;
for (String key : attributes.keySet()) {
if (!this.attributes_.keySet().contains(key))
return false;
if (!this.attributes_.get(key).equals(attributes.get(key)))
return false;
}
return true;
}
However, this implementation fails when the same key is added more than once or when a key is removed from the map (the size tests fail for the values, as they count the duplicates and do not resize when values are removed.)
It seems that my situation should be common enough to find information that is relevant to my case, but I could not find any. Is there any legacy code or widely accepted solution to this situation? Any help or working solution is appreciated.
I am going to put this as an answer even though I am not 100% sure it solves your problem (but it's simply not gonna fit in a comment).
First off, to repeat my comments: The Map interface forbides that a map has duplicate keys or multiple values per key. Any proper implementation (e.g. java.util.HashMap) will therefore not allow this. Typically they will just replace the value if this happens.
Furthermore, the specification for equals, to me, seems to be doing what you want. Again, a proper implementation must live up to that specification.
So, what's the point here: If you are writing your own class that is implementing Map, then it simply cannot allow duplicate keys (methods like get wouldn't make sense anymore). If you are using a built-in implementation such as HashMap, it replaces the values anyway.
Now you are saying that you're experiencing size issues with keySet() and values(). I think you should add example code that will cause this behavior. The following works just fine for me:
Map<String, String> map = new HashMap<String, String>();
map.put("Foo", "Bar");
System.out.println(map.keySet().size()); // 1
System.out.println(map.values().size()); // 1
map.put("Foo", "Baz"); // the HashMap will merely replace the old value
System.out.println(map.keySet().size()); // still 1
System.out.println(map.values().size()); // still 1
Removing a key will, of course, change the size. I don't see how you consider this a problem based on your explanations so far.
As for equals, you may just want to look at the implementation for HashMap, which can be found here:
public boolean equals(Object o) {
if (o == this)
return true;
if (!(o instanceof Map))
return false;
Map<K,V> m = (Map<K,V>) o;
if (m.size() != size())
return false;
try {
Iterator<Entry<K,V>> i = entrySet().iterator();
while (i.hasNext()) {
Entry<K,V> e = i.next();
K key = e.getKey();
V value = e.getValue();
if (value == null) {
if (!(m.get(key)==null && m.containsKey(key)))
return false;
} else {
if (!value.equals(m.get(key)))
return false;
}
}
} catch (ClassCastException unused) {
return false;
} catch (NullPointerException unused) {
return false;
}
return true;
}
Consider the following example:
Map<String, String> map1 = new HashMap<String, String>();
map1.put("Foo", "Bar");
Map<String, String> map2 = new HashMap<String, String>();
map2.put("Foo", "Bar");
System.out.println(map1.equals(map2)); // true
Firstly, you complain about your maps having duplicate keys... not possible (unless you're using a badly broken implementation).
This should do it:
public boolean equals(Object o) {
if (!(o instanceof MyClass))
return false;
MyClass that = (MyClass)o;
if (map.size() != that.map.size())
return false;
for (Map.Entry<String, Object> entry : map) {
Object a = entry.getValue();
Object b = that.map.get(entry.getKey());
if ((a == null ^ b == null) || (a == null && !a.equals(b)))
return false;
}
return true;
}

Iterate through two TreeMaps to compare in Java

At first I had something like this:
public static boolean equals(TreeMap<?, Boolean> a, TreeMap<?, Boolean> b) {
boolean isEqual = false;
int count = 0;
if (a.size() == b.size()) {
for (boolean value1 : a.values()) {
for (boolean value2 : b.values()) {
if (value2 == value1) {
count++;
isEqual = true;
continue;
} else {
isEqual = false;
return isEqual;
}
}
}
if (count == a.size()) {
return true;
}
}
}
Then found that nope it didn't work. I'm checking to see if every element in Object a is the same as in Object b without using Iterate or Collection. and in the same place... any suggestions? Would implementing a for-each loop over the keySet() work?
So, something along these lines? Needing to take in account BOTH keys and values: (Not an answer - test code for suggestions)
This should work as values() are backed up by the TreeMap, so are sorted according to the key values.
List<Boolean> aList = new ArrayList<>(a.values());
List<Boolean> bList = new ArrayList<>(b.values());
boolean equal = aList.equals(bList);
This should be a bit faster than a HashSet version.
And this won't work as #AdrianPronk noticed:
a.values().equals(b.values())
How about this :
Set values1 = new HashSet(map1.values());
Set values2 = new HashSet(map2.values());
boolean equal = values1.equals(value2);
For Comparing two Map Objects in java, you can add the keys of a map to list and with those 2 lists you can use the methods retainAll() and removeAll() and add them to another common keys list and different keys list.
The correct way to compare maps is to:
Check that the maps are the same size(!)
Get the set of keys from one map
For each key from that set you retrieved, check that the value retrieved from each map for that key is the same

Categories

Resources