Imagine I have two maps:
HashMap<TestClass, Integer> map1, map2;
with TestClass being defined as:
class TestClass {
int i;
public TestClass(int i){
this.i = i;
}
}
And the following code to fill the maps:
map1.put(new TestClass(1), 1);
map1.put(new TestClass(2), 2);
map2.put(new TestClass(1), 1);
map2.put(new TestClass(2), 2);
I want to write a method that returns true
if both maps contain the "same" keys and values. Yes, in above example I could just create a local variable to store the result of the first constructor call and pass it to the second map, but I can't do this in my real application as I want to compare similar objects that do not equal each other per se at least as far as java's implementation of the equals
method goes.
My previous implementation (simplified):
if(map1.size() == map2.size()){
for(String key1 : map1.keySet()){
if(!map1.get(key1).equals(map2.get(key1))
return false;
}
return true;
} else
return false;
This works fine for String
s, as they equal each other even if instantiated twice in different positions.
As executing the constructor of TestClass twice returns two different objects though, map2.get(key1)
will return null (or throw an exception, not entirely sure) as key1
is not in map2
.
To compare both maps I have written the following code (simplified):
if(map1.size() == map2.size()){
for(TestClass key1 : map1.keySet()){
boolean foundEqualing = false;
for (TestClass key2 : map2.keySet()) {
// Search equaling
if (key1.i == key2.i) {
// Check if corresponding values equal as well
if (map1.get(key1).equals(map2.get(key2))
// Store that a equaling key value pair was found
foundEqualing = true;
// Break if keys equaled each other
break;
}
}
// Return false if no equaling key was found or if keys equal each other but the corresponding values don't
if (!foundEqualing)
return false;
}
return true;
} else
return false;
The issue I have with this code is that it loops through both maps, which seems really inefficient to me. I'm not familiar with the correct notations, but the time the operation takes quadruples if the size of the map doubles, if I'm not mistaken.
Is there a way to more efficiently loop through or filter these maps in another way than writing for loops?
My real world code uses reflection, therefore do not focus too hard on the provided example. The types of the map could be from each and every type (the only thing I know is that they have to implement a certain interface, otherwise they are just ignored).
Edit:
I'm currently thinking about using the stream filter collect syntax, but I've never used that. Is that more efficient anyway or does it just loop over the map internally as well?
question from:
https://stackoverflow.com/questions/65859132/efficient-way-to-compare-two-similar-maps-in-java