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