Two map comparisons in Java

Source: Internet
Author: User

One
    /*** Keyset () iterator with map (low performance) **/     Public  voidCompareMap1 () {Map<string, string> m1 =NewHashmap<string, string> (); map<string, string> m2 =NewHashmap<string, string> ();Iterator<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//......            }        }     }

Two
   /*** 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 ...            }        }    }

Three
    /*** 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.