HashMap collection sorting methods

First, let's look at three common methods (1) Map Gets a collection of elements entrySet (), (2) keySet (), (3) values ​​()


1. entrySet () :( 1) the collection of all the first return map "map" Set the set, where each type specification "map" is of Map.Entry <K, V>
        (2) Remove all of the re-iterative " mapping ", reuse getKey (), getValue () method to obtain a corresponding key value.

 Import Classes in java.util *. ; 
  
  public  class the Main {
       public  static  void main (String [] args) { 
          the Map <String, String> Map = new new the HashMap <String, String> ();     // build key-value pair <String, String> the Map set 
          map.put ( "a", "AAA" ); 
          map.put ( "B", "BBB" ); 
          map.put ( "C", "CCC" ); 
      
         the set <of Map.Entry < String, String >> EnumMap.entrySet the entrySet = ();     // to get all "map" the map set collection set, there is a specification for each type of mapping of Map.Entry <K, V> 
         the Iterator <    Map.Entry<String, String>> iter = entrySet.iterator();    //再得到entrySet集合的迭代器,Map.Entry<K, V>为迭代元素的类型
         while(iter.hasNext()){
             Map.Entry<String, String> item = iter.next();
             String key = item.getKey();
             String value = item.getValue();
             System.out.println("key:" + key + "-->value:" + value);
         }
        /*
         for(Map.Entry<String, String> item: entrySet){
             String key = item.getKey();
             String value = item.getValue();
             System.out.println("key:" + key + "-->value:" + value);
            }
         */
    }
 }
 

 运行结果
 key:b-->value:bbb
 key:c-->value:ccc
 key:a-->value:aaa

2. keySet () :( 1) a first step, a collection of all return Set key map set
         (2) a second step, and then remove all key iteratively reuse get () method to get the value, for (type element : the essence of the collection) is to get the collection's iterator iterate

Import Classes in java.util *. ; 
   
  public  class the Main {
       public  static  void main (String [] args) { 
          the Map <String, String> Map = new new the HashMap <String, String> ();     // build key-value pair <String, String> the Map set 
         map.put ( "a", "AAA" ); 
         map.put ( "B", "BBB" ); 
          map.put ( "C", "CCC" ); 
         
        the set <String> = keySet map.keySet ();     // to get all of the keys of the map set set set 
         Iterator <String> iter = keySet.iterator (     );// then get iterator keySet collection 
         while(iter.hasNext()){
             String key = iter.next();
             String value = map.get(key);
             System.out.println("key:" + key + "-->value:" + value);
         }
         /*
        for(String key: keySet){
                     String value = map.get(key);
                    System.out.println("key:" + key + "-->value:" + value);
                 }
                 */
       }
 }

 运行结果
 key:b-->value:bbb
 key:c-->value:ccc
 key:a-->value:aaa
```

3. values ​​(): returns a collection of all value map Collection set (stored in the unordered collection)

Import Classes in java.util *. ; 
  
  public  class the Main `` {
       public  static  void main (String [] args) { 
          the Map <String, String> Map = new new the HashMap <String, String> ();     // first key pair is configured <String, String> set the Map 
          map.put ( "a", "AAA" ); 
          map.put ( "B", "BBB" ); 
          map.put ( "C", "CCC" ); 
      
         collection <String > = map.values collection ();     // then get all the set value map collection set 
         System.out.println (collection);
     } 
 } 
 
 
  // run results
  // [bbb, ccc, aaa]

Welcome to browse, I hope all of you can help.

Guess you like

Origin www.cnblogs.com/saomoumou/p/11333041.html