Map two kinds of traversal mode

Source: Internet
Author: User

The map itself does not have iterators and thus needs to take new steps while traversing the elements, providing two methods in the JDK

KeySet
KeySet ()
Returns a view of the
keys contained in this map Set . The set is supported by mappings, so changes to the mappings can be reflected in this set, and vice versa. If the set is iterated while the mapping is modified (except through the iterator's own remove operation), the iteration result is indeterminate. Set support elements are removed via iterator.remove, set.remove, removeall, retainall , and C The Lear action removes the corresponding mapping from the map. It does not support add or addall operations.
Return:
The
set view of the keys contained in this map

1  Packagecom. Map;2 3 ImportJava.util.HashMap;4 ImportJava.util.HashSet;5 ImportJava.util.Iterator;6 ImportJava.util.Map;7 ImportJava.util.Random;8 ImportJava.util.Set;9 Ten  Public classMapkeyset { One      Public Static voidMain (string[] args) { Amap<string, string> map =NewHashmap<string, string>(); -Map.put ("The", "xsf")); -Map.put ("Up", "MDX"); theSet<string> KeySet =Map.keyset (); -         //Get iterators -Iterator<string> it =keyset.iterator (); -          while(It.hasnext ()) { +String key =It.next (); -             //first remove key and get value by Get (key) +String value =Map.get (key); ASystem.out.println ("Key" +key+ "--->value:" +value); at      -         } -     } -}

EntrySet
EntrySet ()
returns a view of the mapping relationships contained in this map Set . The set is supported by mappings, so changes to the mappings can be reflected in this set, and vice versa. If the set is iterated while the mapping is modified (with the exception of the iterator's own remove operation, or by performing a setValue operation on the mapped item returned by the iterator), the iteration result is indeterminate. Set support elements are removed via iterator.remove, set.remove, removeall, retainall , and clear operation to remove the mapping relationship from the map. It does not support add or addall operations.
Return:
set view of mapping relationships contained in this map
1  Packagecom. Map;2 3 ImportJava.util.HashMap;4 ImportJava.util.Iterator;5 ImportJava.util.Map;6 ImportJava.util.Set;7 8  Public classMapentryset {9      Public Static voidMain (string[] args) {Tenmap<string,string> map =NewHashmap<string, string>(); OneMap.put ("The", "xsf")); AMap.put ("Up", "MDX"); -         //Set EntrySet -set<map.entry<string, string>> entryset =Map.entryset (); the         //put the Enterset into the iterator -         /*iterator<map.entry<string, string>> it = Entryset.iterator (); - //Start Iteration - While (It.hasnext ()) { + //Get Map.entry Relationship Object - map.entry<string, string> me = It.next (); + String key = Me.getkey (); A String value = Me.getvalue (); at System.out.println ("Key:" +key+ "-->value:" +value); -         }*/ -          for(Map.entry<string, string>Entry:map.entrySet ()) { -String key =Entry.getkey (); -String value =Entry.getvalue (); -System.out.println ("Key:" +key+ "-->value:" +value); in         } -      to     }  +}

Map two kinds of traversal mode

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.