Array deduplication and record repetition (and four ways to traverse the map)

Source: Internet
Author: User

Private Static voidcheck (string[] array) {//what are the string arrays that contain non-repeating strings? The number of each repetitionmap<string,integer> Map = new hashmap<> (); for (int i=0;i<array.length;i++) {if (Map.get (array[i]) = null) {Map.put (Array[i], Map.get ( Array[i]) + 1);//value + 1} else {map.put (array[i],1); } } //① Traversal MapSystem.out.println ("Traverse key and Value by Map.keyset ():");  for(String key:map.keySet ()) {System.out.println ("The array is:" + key + "=" +Map.get (key)); }                //②mapset (). Iterator ()SYSTEM.OUT.PRINTLN ("Traversal of key and value through Map.entryset () iterator ()"); Iterator<entry<string, integer>> it =Map.entryset (). iterator ();  while(It.hasnext ()) {Entry<string, integer> entry =It.next (); System.out.println ("The array is:" + entry.getkey () + "=" +Entry.getvalue ()); }        //③mapset () recommended when capacity is largeSystem.out.println ("Traverse key and value through Map.entryset ()");  for(Entry<string, integer>Entry:map.entrySet ()) {System.out.println ("The array is:" + entry.getkey () + "=" +Entry.getvalue ()); }                //④System.out.println ("Traverse all value through Map.values (), but cannot traverse key");  for(Integer value:map.values ()) {System.out.println ("= " +value); }          }

Array deduplication and record repetition (and four ways to traverse the map)

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.