Java Map map


Map

  • Map Collection set not inherited interfaces, provided that the value of the key to the map is stored in the key - value pairs - a set of (key value) of;
  • To identify the position of the object stored in the map by the key, the key can not be repeated, the key itself can not decide to store position, but by a hashing technique process produces an integral value of a hash code;
  • Hash code used for the offset, the offset corresponding to the memory area assigned to the start position of mapping, to determine the position of the object is stored in the memory map;
  • When a call is not mapped elements are present, NoSuchElementException throws an exception, when the object is not compatible with the map elements, will lead ClassCastException exception if the map does not permit the use of null object will cause a NullPointerException when trying to change a is not allowed to modify the map, it will lead to an UnsupportedOperationException;

1. Conversion to set (Set, Collection)

  • Mapping is not cluster, but the view can be obtained cluster can be used the entrySet () method which returns a collection mapping elements (the Set), can be obtained clusters view of the keys with keySet (), with the values ​​() clusters can be worth view;
Types of method Explanation
Set entrySet() Returns a collection of all the keys Map - Set the value of the set of data elements in a set type Set is Map.Entry
Set keySet() Set to return a collection of all the keys in the corresponding Map
Collection values() Returns a set of classes worth maps contain mapping recycling get () and put (), put () specifies a value of the key will be added to the map, the key may be used as a parameter to call get () method returns the value

2. The method provided

Types of method Explanation
Object put(Object key, Object value) Map set to add key - value pair, the key returns the corresponding value before the null if none
void putAll (Map m) All incoming calls from the map is added m
Object get(Object key) Returns the value of the key mapping, if this map contains no mapping for the key return null, Object value data type
void clear() Delete all the keys - value pairs
int size() Returns map key - the value of the number of
int hashCode() Returns to the calling mapping hash code
boolean isEmpty() If the map is empty, returns true
boolean containsKey(Object k) If the map contains the key k, returns true
boolean containsValue(Object v) If the map contains the value v, returns true
Object remove(Object k) Delete key input for the k
boolean equals(Object o) If o is mapped and contains the same input, returns true
import java.util.HashMap;
import java.util.TreeSet;

public class test {
    public static void main(String[] args) {
        TreeSet ts = new TreeSet();
        ts.add(3);
        ts.add(4);
        TreeSet ts2 = new TreeSet();
        ts2.add(3);
        ts2.add(4);
        System.out.println(ts.equals(ts2));//true
        HashMap hm = new HashMap();
        hm.put(3, 3);
        hm.put(4, 4);
        HashMap hm2 = new HashMap();
        hm2.put(3, 3);
        hm2.put(4, 4);
        System.out.println(hm.equals(hm2));//true
    }
}

3. The implementation class

  • HashMap class : key and value that can be null, thread-safe;
  • TreeMap class : key can not be null, value can be null, thread-safe;
Published 59 original articles · won praise 60 · views 1575

Guess you like

Origin blog.csdn.net/Regino/article/details/104497735
Map
Map
map
map