Two ways to remove a map collection (KeySet, EntrySet)

Source: Internet
Author: User
Tags set set

/*
* Two ways to remove a map collection: (Map without iterator method)
* 1, set<k> KeySet: All the keys in the map are deposited into the set set because the set has iterators.
* So you can iterate through all the keys, and then according to the Get method, get each key corresponding value.
* The extraction principle of the Map collection: The Map collection is transferred to the set set and then removed by the iterator
* 2, Set<map.entry<k,v>> EntrySet: Stores the mappings in the map collection into the set set,
* and the data type of the relationship is: map.entry

*/

Package Map overview; import java.util.hashmap;import java.util.iterator;import java.util.map;import java.util.set;/* * Map.entry actually Entry is also an interface, it is an internal interface in the Map interface *//*interface map{public static interface Entry{public abstract Object GetKey () ;p ublic abstract Object getValue ();}} Class HashMap implements Map.entry{class HaHa implements map.entry{@Overridepublic Object GetKey () {return null;} @Overridepublic Object GetValue () {return null;}} @Override//public Object GetKey () {//return null;//}////@Override//public Object GetValue () {//return null;//}}*/ public class Test {public static void main (string[] args) {map<string, string> Map = new hashmap<string, STRING&G t; (); Map.put ("zhangsan2"), Map.put ("The", "Zhangsan3"), Map.put ("", "Zhangsan1"), Map.put ("A", "Zhangsan4"); /Method 1: Take out the mappings in the Map collection, deposit into the set set set<map.entry<string, string>> entryset = Map.entryset ();iterator< Map.entry<string, string>> it = Entryset.iterator (); while (It.hasnext ()) {map.entry<string, String> me = It.next ();//After the relationship object Map.entry is acquired, the key and value in the relationship can be obtained by Map.entry//getkey and GetValue methods string key = Me.getkey (); String value = Me.getvalue (); System.out.println ("Key:" +key+ "--" + "value:" +value);} Method 2: Get the set set of all keys for the map collection first, KeySet (). set<string> KeySet = Map.keyset ();//The iterator can be obtained with the set set. Iterator<string> it2 = Keyset.iterator (); while (It2.hasnext ()) {String key = It2.next ();//With Key Its corresponding value can be obtained through the Get method of the map collection. String value = Map.get (key); System.out.println ("key:" + key + "-+" + value);}}}


Two ways to remove a map collection (KeySet, EntrySet)

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.