Comparative use of LinkedHashMap and HashMap, linkedhashmap
I made a question yesterday, And hashmap always timed out. Later I looked at other people's shares and found that I could change HashMap to LinkedHashMap.
At the same time, it has the advantages of the histogram list and HashMap, and saves time for processing. Good data structure!
The following is a description of the comparison between LinkedHashMap and HashMap found on the Internet. It is recorded here to deepen your memory!
import java.util.HashMap; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Map; public class TestLinkedHashMap { public static void main(String args[]) { System.out.println( "*************************LinkedHashMap*************" ); Map<Integer,String> map = new LinkedHashMap<Integer,String>(); map.put( 6 , "apple" ); map.put( 3 , "banana" ); map.put( 2 , "pear" ); for (Iterator it = map.keySet().iterator();it.hasNext();) { Object key = it.next(); System.out.println( key+ "=" + map.get(key)); } System.out.println( "*************************HashMap*************" ); Map<Integer,String> map1 = new HashMap<Integer,String>(); map1.put( 6 , "apple" ); map1.put( 3 , "banana" ); map1.put( 2 , "pear" ); for (Iterator it = map1.keySet().iterator();it.hasNext();) { Object key = it.next(); System.out.println( key+ "=" + map1.get(key)); } } } |
The running result is as follows:
*************
6 = apple
3 = banana
2 = pear
* ************************ HashMap ************** ************
2 = pear
6 = apple
3 = banana
Analysis: LinkedHashmap is characterized by the position of the put object not changing, but HashMap changes.
Further popularity:
Java defines an interface java. util. Map for ing in the data structure. It has four implementation classes:HashMap Hashtable LinkedHashMap and TreeMap.
Map is mainly used to store the key-value pairs and obtain values based on the keys. Therefore, duplicate keys are not allowed (repeated overwrites), but repeated values are allowed.
Hashmap is the most commonly used Map. It stores data based on the HashCode value of the key, and can directly obtain its value based on the key, with fast access speed and time, the order in which data is obtained is completely random. HashMap allows a maximum of Null keys for one record, and Null values for multiple records. HashMap does not support thread synchronization, that is, multiple threads can write HashMap simultaneously at any time; data inconsistency may occur. If synchronization is required, you can use the synchronizedMap method of Collections to synchronize HashMap or use ConcurrentHashMap.
Like HashMap, Hashtable inherits fromDictionaryClass, the difference is: it does not allow the record key or the value is null; it supports thread synchronization, that is, at any time, only one thread can write Hashtable, as a result, Hashtable is slow in writing.
LinkedHashMap is a subclass of HashMap.The record insertion sequence is saved. When Iterator is used to traverse the LinkedHashMap, the first record must be inserted first. You can also use a parameter to sort the records by the number of times of application. It is slower than HashMap during traversal, but in some cases, when the HashMap capacity is large and the actual data is small, the traversal may be slower than LinkedHashMap, because the traversal speed of LinkedHashMap is only related to the actual data and has nothing to do with the capacity, and the traversal speed of HashMap is related to its capacity.
TreeMap implementation SortMap InterfaceThe record stored in the table is sorted by key. By default, the record is sorted in ascending order of key values. You can also specify a sort comparator. When you use Iterator to traverse the TreeMap, the obtained records are sorted in ascending order.
Summary:
In general, HashMap is the most used. It is the best choice to insert, delete, and locate elements in a Map. However, if you want to traverse keys in the natural or custom order, it is better to use TreeMap. If the output order is the same as that of the input, you can use LinkedHashMap to implement it. It can also be sorted by read order.
To learn more about map knowledge, you can see this URL: http://www.oracle.com/technetwork/cn/articles/maps1-100947-zhs.html
Refer:
Http://www.cnblogs.com/hubingxu/archive/2012/02/21/2361281.html
Http://www.oracle.com/technetwork/cn/articles/maps1-100947-zhs.html
How to traverse hashmap in java
You can use LinkedHashMap to solve the problem that the iteration sequence is consistent with the insertion sequence.
In your code, replace HashMap with LinkedHashMap.
See:
Comparative use of LinkedHashMap and HashMap
Www.cnblogs.com/...html.
What is the difference between LinkedHashMap and TreeMap?
The first two are map, so there is no difference in the key value. The difference is that when Iterator is used to traverse
LinkedHashMap stores the insert sequence of records, first inserts and first traverses
TreeMap is sorted in ascending order by default. You can also specify a comparator for sorting. In ascending order.
For example, a is a LinkedHashMap, and B is a TreeMap.
A. put ("2", "AB ");
A. put ("1", "bc ");
B. put ("2", "AB ");
B. put ("1", "bc ");
Then, when traversing a, it first traverses the key to 2, because 2 is first put in.
When traversing B, first traverse to "1", because the order is first 1 after 2