/*** Keyset () iterator with map (low performance) **/ Public voidCompareMap1 () {Map<string, string> m1 =NewHashmap<string, string> ();//Smallmap<string, string> m2 =NewHashmap<string, string> ();//BigIterator<String> Iter1 =M1.keyset (). iterator (); while(Iter1.hasnext ()) {String M1key=(String) iter1.next (); if(!m1.get (M1key). Equals (M2.get (M1key))) {//if the value of the same key in the two map is not equal//...... } } } /*** Iterator with Map entryset () (High performance efficiency)*/ Public voidcompareMap2 () {Map<string, string> m1 =NewHashmap<string, string>(); Map<string, string> m2 =NewHashmap<string, string>(); Iterator<entry<string, string>> iter1 =M1.entryset (). iterator (); while(Iter1.hasnext ()) {Map.entry<string, string> entry1 = (entry<string, string>) Iter1.next (); String M1value= Entry1.getvalue () = =NULL?"": Entry1.getvalue (); String M2value= M2.get (Entry1.getkey ()) = =NULL?"": M2.get (Entry1.getkey ()); if(!m1value.equals (M2value)) {//if the value of the same key in the two map is not equal//Other operations ... } } } /*** Enhanced for loop with Map EntrySet () (High performance efficiency)*/ Public voidcompareMap3 () {Map<string, string> m1 =NewHashmap<string, string>(); Map<string, string> m2 =NewHashmap<string, string>(); for(Map.entry<string, string>Entry1:m1.entrySet ()) {String M1value= Entry1.getvalue () = =NULL?"": Entry1.getvalue (); String M2value= M2.get (Entry1.getkey ()) = =NULL?"": M2.get (Entry1.getkey ()); if(!m1value.equals (M2value)) {//if the value of the same key in the two map is not equal//Other operations ... } } }
Two map comparisons in Java