As the name suggests, linkedhashmap has a linked list structure more than hashmap. Compared with hashmap, linkedhashmap maintains a hashmap with a double linked list. linkedhashmap supports sorting in 2, insert sorting, and sort, recently used products will be moved to the end, for example, M1 m2 M4 and M1 m2 M4 m3 after m3 are used. The elements in linkedhashmap output are ordered, while those in hashmap output are random, if map ing is complex and requires high efficiency, it is best to use linkedhashmap, but multi-threaded access may cause non-synchronization, so collections is used. synchronizedmap is encapsulated to implement synchronization. Its implementation is generally:
Map <string> map = collections. synchronizedmap (New linkedhashmap (<string ));
Hashmap, linkedhashmap, and treemap all belong to map.
MapIt is mainly used to store key-value pairs and obtain values based on keys. Therefore, duplicate keys are not allowed, but repeated values are allowed.
HashmapIs 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. 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.
LinkedhashmapLinkedhashmap is also a hashmap, but a two-way linked list is maintained internally to maintain the order.
TreemapNot only can order be maintained, but also can be used to sort hashmap examples:
public static void main(String[] args) { Map<String, String> map = new HashMap<String, String>(); map.put("a3", "aa"); map.put("a2", "bb"); map.put("b1", "cc"); for (Iterator iterator = map.values().iterator(); iterator.hasNext();) { String name = (String) iterator.next(); System.out.println(name); } }
Linkedhashmap example:
public static void main(String[] args) { Map<String, String> map = new LinkedHashMap<String, String>(); map.put("a3", "aa"); map.put("a2", "bb"); map.put("b1", "cc"); for (Iterator iterator = map.values().iterator(); iterator.hasNext();) { String name = (String) iterator.next(); System.out.println(name); } }
Treemap example:
public static void main(String[] args) { Map<String, String> map = new TreeMap<String, String>(new Comparator<Object>(){ Collator collator = Collator.getInstance(); public int compare(Object o1, Object o2) { CollationKey key1 = collator.getCollationKey(o1.toString()); CollationKey key2 = collator.getCollationKey(o2.toString()); return key1.compareTo(key2); //return collator.compare(o1, o2); }}); map.put("a3", "aa"); map.put("a2", "bb"); map.put("b1", "cc"); for (Iterator iterator = map.values().iterator(); iterator.hasNext();) { String name = (String) iterator.next(); System.out.println(name); } }
Run these three examples to see the differences between them.