Java learning day30-Map collection

A, Map

1.Map for storing data having a mapping relationship, and therefore the collection Map holds two sets of values, one for storage in the Map Key, another group of the Map for holding Value.

The 2.Map key and value can be any type of reference data.

The Key 3.Map allowed to repeat, i.e., with any two of a Key Map objects by comparing equals method returns false.

There is a one-way one to one relationship between 4.Key and Vaiue, that is, always find unique, Value determined by the specified Key.

 

Two, Map interfaces and HashMap classes

package day16;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class Test6 {
    public static void main(String[] args){
        Map<String,Integer> map = new HashMap<String,Integer>();
        
        map.put("b", 1);//添加数据
        map.put("c", 2);
        map.put("e", 2);
        System.out.println(map);
        
        System.out.println(map.get("b"));//The key value 
        
        map.remove ( "C"); // The key value pairs are removed 
        System.out.println (Map); 
        
        ; System.out.println (map.size ()) // Map set length 
        
        System.out.println (map.containsKey ( "B")); // determines whether the current map set contains the specified Key; 
        
        System.out.println (map.containsValue ( . 1)); // determine the current map specified collection contains the Value 
        
//         map.clear (); // Clear set 
        
        the set <String> = map.keySet Keys (); // Get a collection of key map set 
        
        map.values (); // Get map set the values set 
        
        
        // iterate map set by map.keySet ();
        for(String key : keys){
            System.out.println("key:" + key + ",value:" + map.get(key));
        }
        
        //通过map.entrySet();遍历map集合
        Set<Entry<String,Integer>> entrys = map.entrySet();
        for(Entry<String,Integer>en : entrys){
            System.out.println("key:" + en.getKey() + ",value:" + en.getValue());
        }
    }
}

Print results:

  

三、HashMap & Hashtable

1.HashMap and Hashtable are two typical Map interface implementation class, except that:

  ①Hashtable is an old Map implementation class is not recommended.

  ②Hashtable is a thread-safe Map implementation, but HashMap is thread safe.

  ③Hashtable not permit null as a key and value, and can HashMap.

2. HashSet set and can not guarantee that the same arrangement order of the elements, HashMap Hashtable and can not guarantee the order in which the key-value pairs.

3.HashMap, Hashtable determination is equal to two key criteria: by two Key equals method returns true, hashCode values ​​are equal.

 

Four, TreeMap

When the Key-Value for storing 1.TreeMap required Key key-value according to sort. TreeMap can guarantee that all of the Key-Value in an orderly state.

Key 2.TreeMap sort of:

  ① natural ordering: All Key TreeMap must implement the Comparable interface, and all of Key should be the object of the same class, otherwise it will throw ClassCastException.

  ② custom sorting (to know): When you create a TreeMap, pass a Comparable object, which is responsible for all key TreeMap is sorted. At this time, it does not require Map of Key implement the Comparable interface.

3. General Use map collection, do not use overly complex objects do key.

package day16;

import java.util.Map;
import java.util.TreeMap;

public class Test6 {
    public static void main(String[] args){
        
        //TreeMap的自然排序是字典排序
        Map<Integer,String> map = new TreeMap<Integer,String>();
        map.put(4, "a");
        map.put(2, "a");
        map.put(3, "a");
        map.put(1, "a");
        
        System.out.println(map);
        
        Map<String,String> map1 = new TreeMap<String,String>();
        map1.put("b", "b");
        map1.put("c", "c");
        map1.put("d", "d");
        map1.put("a", "a");
        map1.put("ab", "ab");
        map1.put("1", "ab");
        map1.put("10", "ab");
        
        System.out.println(map1);
    }
}

Print result:

 

Guess you like

Origin www.cnblogs.com/su-peng/p/12569974.html