Home >Java >javaTutorial >Detailed explanation of examples of HashMap traversal and use in Java
Several traversal methods of map:
Map< String, String> map = new HashMap<>(); map.put("aa", "@sohu.com"); map.put("bb","@163.com"); map.put("cc", "@sina.com"); System.out.println("普通的遍历方法,通过Map.keySet遍历key和value");//普通使用,二次取值 for (String key : map.keySet()) { System.out.println("key= "+key+" and value= "+map.get(key)); } System.out.println("通过Map.entrySet使用iterator遍历key和value:"); Iterator<Map.Entry<String, String>> it = map.entrySet().iterator(); while(it.hasNext()){ Map.Entry<String, String> entry = it.next(); System.out.println("key= "+entry.getKey()+" and value= "+entry.getValue()); } System.out.println("通过Map.entrySet遍历key和value"); //推荐这种,特别是容量大的时候 for(Map.Entry<String, String> entry : map.entrySet()){ System.out.println("key= "+entry.getKey()+" and value= "+entry.getValue()); } System.out.println(“通过Map.values()遍历所有的value,但不能遍历key”); for(String v : map.values()){ System.out.println("value = "+v); }
The connection and difference between HashMap and Hashtable
The implementation principles are the same, the functions are the same, the bottom layer They are all hash table structures, query speed is fast, and can be interoperated in many cases. Early versions are generally safe.
HashMap and Hashtable both implement the Map interface, but before deciding which one to use, you must first understand the difference between them. The main differences are: thread safety, synchronization, and speed.
HashMap is almost equivalent to Hashtable, except that HashMap is non-synchronized and can accept null (HashMap can accept null keys and values, while Hashtable Otherwise).
HashMap is non-synchronized, while Hashtable is synchronized, which means that Hashtable is thread-safe and multiple threads can share a Hashtable; and if there is no correct synchronization, multiple threads are HashMap cannot be shared. Java 5 provides ConcurrentHashMap, which is a replacement for HashTable and has better scalability than HashTable.
Another difference is that HashMap's iterator (Iterator) is a fail-fast iterator, while Hashtable's enumerator iterator is not fail-fast. So when other threads change the structure of the HashMap (add or remove elements), a ConcurrentModificationException will be thrown, but the remove() method of the iterator itself will not throw a ConcurrentModificationException when removing elements. But this is not a guaranteed behavior, it depends on the JVM. This is also the difference between Enumeration and Iterator.
Because Hashtable is thread-safe and synchronized, it is slower than HashMap in a single-threaded environment. If you don't need synchronization and only need a single thread, then using HashMap will perform better than Hashtable.
HashMap cannot guarantee that the order of elements in the Map will remain unchanged over time.
Characteristics of hashmap
HashMap is a subclass of the map interface and is an object that maps keys to values, where keys and values are Object, not thread-safe
hashMap uses a hash table to store map keys
key is unordered and unique, one can be null
value is unordered and not unique, there can be pairs of null
linkedHashMap Use a hash table to store the keys in the map, and use a linked doubly linked list to manage the order
What we use most is HashMap. To insert, delete and locate elements in the Map, HashMap is the best s Choice. If the order of output needs to be the same as the order of input, then LinkedHashMap can be used. It can also be arranged in the order of reading.
HashMap is the most commonly used Map, which is based on the key The hashCode value stores data, and its value can be obtained directly according to the key, which has fast access speed. HashMap only allows the key of one record to be NULL, and allows the value of multiple records to be NULL. HashMap does not support thread synchronization, that is, multiple threads can write HashMap at the same time at any time, which may cause data inconsistency.
If synchronization is required, you can use the synchronizedMap method of Collections to make HashMap synchronized. LinkedHashMap saves the insertion order of records. When using Iterator to traverse LinkedHashMap, the record obtained first must be inserted first.
HashMap can be synchronized through the following statement:
Map m = Collections.synchronizeMap(hashMap);
Comparison of the efficiency of several commonly used collections
The above is the detailed content of Detailed explanation of examples of HashMap traversal and use in Java. For more information, please follow other related articles on the PHP Chinese website!