HashMap in EntrySet () use method 2016.12.28

Source: Internet
Author: User

 Packagemap;ImportJava.util.HashMap;ImportJava.util.Iterator;ImportJava.util.Map.Entry;ImportJava.util.Set;/** * @authorAdministrator * Note: HashMap does not support iterator so you want to iterate over the key and value in the map in other ways*/ Public classEntrysetdemo { Public Static voidMain (string[] args) {HashMap<String,String> hs=NewHashmap<string,string>(); //adding elementsHs.put ("Key1", "value1"); Hs.put ("Key2", "value2"); Hs.put ("Key3", "Value3"); Hs.put ("Key4", "Value4"); Hs.put ("Key5", "Value5"); /*** Get the Set view returned by HS * Very NOTE: Returns the set view of the mappings contained in this map, and the operation of this set view is reflected in the original HashMap*/Set<Entry<String,String>> set=Hs.entryset (); /*** Iterate over set, the returned entry contains key and value in the format: Key=value*/Iterator<Entry<String,String>> iter=Set.iterator (); //This approach outputs a format of Key=value//While (Iter.hasnext ()) {//System.out.println (Iter.next ());//        }                //iterate over a entry         while(Iter.hasnext ()) {Entry<String,String> Entry=iter.next ();//get a Entry objectEntry.setvalue ("123");//replaces the value of the entry of the current iteration with the 123String Key=entry.getkey ();//gets the key for the current iteration of the Entry object (entry in the returned set view)String Value=entry.getvalue ();//gets the value of the current iteration entry objectSystem.out.println (key+ "=" +value); Entry.setvalue ("1");//this replaces the value of the current iteration object, which is mapped to the hashmap<string,string>} System.out.println ("==========================="); //edits to the set view will reflect the entry         for(entry<string,string>Entry:set) {            //Each entry is a key in the map and other objects that correspond to the value being re-encapsulated             for(String key:hs.keySet ()) {System.out.println (key+"="+Hs.get (key)); }        }    }    }

The result of the operation is:

key4=123Key3=123key5=123key2=123key1 =123===========================  Key4=1=nullkey3=1=nullkey5=1=nullkey2=1=null  key1=1=null

HashMap in EntrySet () use method 2016.12.28

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.