JAVA learning lesson 38th (Common Object API)-set framework (6)-Map set and common methods

Source: Internet
Author: User

JAVA learning lesson 38th (Common Object API)-set framework (6)-Map set and common methods

1. Map set features

The Map set (double row set) adds one element at a time, and the Collection set (single column set) adds one element at a time.

Interface Map Actually, key-value pairs are stored. Features:

Objects that map keys to values. A ing cannot contain duplicate keys. Each key can only be mapped to one value at most.

That is to say, the uniqueness of keys must be ensured in Map.

Ii. Common Methods

1. add: value = put (key, value); returns the previous value associated with the key. If no null2. Delete: void clear (); clear all mappings in the Map value remove (key): Based on the specified key, delete the key-value pair 3. judgment: boolean containsKey (key) boolean containsValue (value) boolean isEmpty (); 4. get: value get (key): get the value through the key. If the key is not available, null is returned, which means that the int size () is not found in the Map: get the number of key-value pairs

Method demonstration

Import java. util. HashMap; import java. util. Map; public class Main {public static void main (String [] args) {Map
  
   
Map = new HashMap
   
    
(); Method (map);} public static void method (Map
    
     
Map) {// name student ID // 1. add System. out. println (map. put (1, "a"); System. out. println (map. put (1, "B"); // store the same key, and the value will be overwritten by map. put (3, "c"); map. put (4, "d"); System. out. println (map); // deletes a map. remove (3); System. out. println (map); // judge System. out. println ("containsKey:" + map. containsKey (3); System. out. println ("containsValue:" + map. containsValue ("d"); // obtain the System. out. println ("Get:" + map. get (4); System. out. println ("Get:" + map. get (8); // judge if 8 is not }}
    
   
  

There is no relationship between Map and Collection, so there is no iterator. How to obtain all key-value pairs in Map (two methods)

1. Return the keySetView.

Import java. util. hashMap; import java. util. iterator; import java. util. map; import java. util. set; public class Main {public static void main (String [] args) {Map
  
   
Map = new HashMap
   
    
(); KeySetMehod (map);} public static void KeySetMehod (Map
    
     
Map) {map. put (1, "a"); map. put (4, "B"); map. put (3, "c"); map. put (5, "d"); // retrieve all key-value pairs in the map, in three steps // 1. use keySet () to obtain the Set of all keys. The Set contains the iterator // 2. obtain the key from the iterator. // 3. get the corresponding valueSet through get () in Map
     
      
KeyIntegers = map. keySet (); Iterator
      
       
It = keyIntegers. iterator (); while (it. hasNext () {Integer key = it. next (); System. out. println ("key-value:" + key + "-" + map. get (key ));}}}
      
     
    
   
  

Three steps


2. PassEntrySet <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Authorization + 1tcQ8Y29kZT5TZXQ8L2NvZGU + authorization + CjxwPjxzdHJvbmc + PC9zdHJvbmc + PC9wPgo8cHJlIGNsYXNzPQ = "brush: java;"> import java. util. hashMap; import java. util. iterator; import java. util. map; import java. util. set; public class Main {public static void main (String [] args) {Map Map = new HashMap (); KeySetMehod (map);} public static void KeySetMehod (Map Map) {map. put (1, "a"); map. put (4, "B"); map. put (3, "c"); map. put (5, "d"); // ing type: Map. entry type // Map. entry The method in/* boolean equals (Object o) compares the equality between the specified Object and this item. K getKey () returns the key corresponding to this item. V getValue () returns the value corresponding to this item. Int hashCode () returns the hash value of this ing item. V setValue (V value) replaces the value corresponding to this item with the specified value (optional ). */Set > Entry = map. entrySet (); Iterator > It = entry. iterator (); while (it. hasNext () {// it. next is the ing relationship Map. Entry MaEntry = it. next (); System. out. println ("key-value:" + maEntry. getKey () + "-" + maEntry. getValue ());}}}


About Map. Entry Enhanced understanding

interface MMap{public static interface Entry{void get();}}class XX implements MMap.Entry{.....}

Just a nest.

3. Return the value of the value contained in the ing using the values () method.CollectionView.

import java.util.Collection;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import java.util.Set;public class Main {public static void main(String[] args) {Map
     
       map = new HashMap
      
       ();KeySetMehod(map);}public static void KeySetMehod(Map
       
         map){map.put(1, "a");map.put(4, "a");map.put(3, "c");map.put(5, "d");Collection
        
          cel = map.values();Iterator
         
           it = cel.iterator();while(it.hasNext()){String in = it.next();System.out.println("key - value : "+in);}}}
         
        
       
      
     

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.