Java HashMap hash map


HashMap

  • HashMap also known as hash or hash map mapping, its implementation Map and extended AbstractMap, itself does not add any new approach;
  • HashMap is disordered, a hash map order of the elements are not added in the order they are read iterator;
  • By calculating the hash table HashMap key - the value of the storage location, and up to a null, thread synchronization is not supported;
  • HashMap in the face of a large collection, you can make the get () and put () run-time remains constant;

1. Constructor

Construction method Explanation
HashMap() Constructs a default hash map
HashMap(Map m) Initializing the hash map elemental m
HashMap(int capacity) Capacity initialization hash map as capacity
HashMap(int capacity, float fillRatio) And filling capacity compared with the hash map of initialization parameters

2. The use of class

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

public class test {
    public static void main(String[] args) {
        HashMap hm = new HashMap();
        hm.put("001", "apple");
        hm.put("002", "banana");
        //返回映射中包含的项的集合,以便使用迭代器
        Set s = hm.entrySet();
        Iterator it = s.iterator();
        while (it.hasNext()) {
            //用 Map.Entry 来操作映射的输入
            Map.Entry m = (Map.Entry) it.next();
            System.out.print(m.getKey() + ": ");
            System.out.println(m.getValue());
        }
        //put 旧键可以自动替换相应的值
        hm.put("001", new String("dog"));
        System.out.println(hm.get("001"));
        //返回映射的 键 的集合
        s = hm.keySet();
        it = s.iterator();
        String key = "";
        while (it.hasNext()) {
            //将 k 的集合强转为 String 类型
            key = (String) it.next();
            System.out.println(key + ":" + hm.get(key));
        }
    }
}
Published 59 original articles · won praise 60 · views 1572

Guess you like

Origin blog.csdn.net/Regino/article/details/104549629
Recommended