HashMap、TreeMap以及LinkedHashMap

HashMap

HashMap the elements are unordered.

New:

HashMap<String, String> hashmap= new HashMap<String, String>();

Common methods:

Reference java learning Summary 1.

Traversal:

for(String str : hashmap.keySet()){
    int value = hashmap.get(str);
    System.out.println(value);
}

TreeMap

TreeMap the elements are arranged in a default natural ordering of keys.

New:

TreeMap<Integer, String> treeMap = new TreeMap<>();

Common methods:

Add elements:

V put (K key, V value): the specified mapping into the TreeMap.

V putAll (Map map): specifies the map into a TreeMap.

Removing elements:

void clear (): Empty all the elements in the TreeMap.

V remove (Object key): Removes the specified key corresponding mapping from the TreeMap.

Modify elements:

V replace (K key, V value): Replace value corresponding to the specified key value.

boolean replace (K key, V oldValue, V newValue): when the specified key value corresponding to the specified value, the new value replaces the value.

Find elements:

boolean containsKey (Object key): determining whether the TreeMap contains the specified key mapping.

boolean containsValue (Object value): determining whether the TreeMap map contains information about the specified value.

Map.Entry <K, V> firstEntry (): returns the TreeMap the first (lowest) mapping.

K firstKey (): returns the first (smallest) Key mapping of the TreeMap.

Map.Entry <K, V> lastEntry (): returns the TreeMap the last (maximum) mapping.

K lastKey (): Returns the last of the TreeMap (the largest) mapping key.

v get (K key): Returns the key corresponding to the specified value.

SortedMap <K, V> headMap (K toKey): Returns the TreeMap mapping set strictly less than the specified key.

SortedMap <K, V> subMap (K fromKey, K toKey): Returns the TreeMap mapping set in the specified range (greater than or equal fromKey, less than toKey).

Traversal interfaces:

Set <Map <K, V >> entrySet (): Set Object Returns the mapping of all the TreeMap composition.

void forEach (BiConsumer action <super K ,? super V?>): perform the specified operation on a mapping of each of TreeMap.

Collection values ​​(): returns the set composed of the TreeMap all values.

Other methods:

Object clone (): returns TreeMap instance shallow copy.

Comparator comparator () <super K?>: Return to the sort keys of the TreeMap's comparator, as if the natural order null is returned.

int size (): returns the number contained in the TreepMap mapping.

Traversal:

for loop

for (Map.Entry entry : treeMap.entrySet()) {
      System.out.println(entry);
}

Iterator loop

Iterator iterator = treeMap.entrySet().iterator();
while (iterator.hasNext()) {
      System.out.println(iterator.next());
}

Laidakedःashanap

LinkedHashMap elements are arranged in order according to the insertion element.

New:

 LinkedHashMap<String, String> linkedHashMap = new LinkedHashMap<String, String>();

Common methods:

clear (): Removes all mappings from this map.

containsValue (Object value): If this mapping one or more keys to the specified value, it returns true. (Present value if the value of Returns true)

get (Object key): back to the specified value of the key mapped, if this map contains a mapping key, null is returned.

Other methods:

Other methods such as put, remove LinkedHashMap method does not override, it can be done by calling the parent class HashMap put, remove method. (Reference java learning Summary 1)

Traversal:

for loop

 for(Map.Entry<String, String> entry : linkedHashMap.entrySet()) {
     System.out.println("key:" + entry.getKey() + "   value:" + entry.getValue());
 }

Iterator loop:

while (it.hasNext()) {
    Map.Entry entry = (Map.Entry) it.next();
    System.out.println("key:" + entry.getKey() + "   value:" + entry.getValue());
}
Published 36 original articles · won praise 2 · Views 986

Guess you like

Origin blog.csdn.net/y18771025420/article/details/102527875