Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing maps with lists in java

Ive come across a nice problem recently, I have a Map

 LinkedHashMap<String, List<MyCustomObject>>

I need to compare this to another Map, which will be populated in the exact same method is the data in the DB is the same.

What really is happening is , that I am populating map1 at time 0, and populating map2 at time t, using the same java method(when I say this, i mean using the same DB/tables etc) So if the data on the DB side is same, then these two maps should have the same content, in the same order(hopefully!!)

Now I want to compare these two maps and if they are not equal (i.e some new data was fetched from the DB), then I want to do some action.If they are the same, I just want to leave it like that.

Can someone point me out to tutes that will help me understand how to do that??

Is serialization an answer to this??

Thanks, Neeraj

like image 674
Neeraj Avatar asked Jul 09 '26 11:07

Neeraj


1 Answers

just use equals:

if (!map1.equals(map2)) {
    //they differ
}

you will have to implement equals (and hashCode!) on MyCustomObject, since (if I understand correctly) the objects will have the same content but different identity (the default equals just compares the pointers - i.e., the memory address in which the object live in).

like image 93
Amir Rachum Avatar answered Jul 12 '26 02:07

Amir Rachum



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!