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 m = (Map.Entry) it.next();
System.out.print(m.getKey() + ": ");
System.out.println(m.getValue());
}
hm.put("001", new String("dog"));
System.out.println(hm.get("001"));
s = hm.keySet();
it = s.iterator();
String key = "";
while (it.hasNext()) {
key = (String) it.next();
System.out.println(key + ":" + hm.get(key));
}
}
}