The difference between:
LinkedHashMap is derived from HashMap and is based on HashMap and bidirectional linked lists. A HashMap disorder; The LinkedHashMap is ordered and can be divided into insert order and access order. In access order, both put and GET operations on existing entries move the Entry to the end of the bidirectionally linked list. LinkedHashMap accesses data in the same way that HashMap uses Entry[], two-way lists only for order. LinkedHashMap is thread unsafe.
LinkedHashMap Application scenario
Hashmaps are unordered, and we use LinkedHashMap when we want to store key-values sequentially.
Map<String, String> hashMap = new HashMap<String, String>();
hashMap.put("name1", "josan1");
hashMap.put("name2", "josan2");
hashMap.put("name3", "josan3");
Set<Entry<String, String>> set = hashMap.entrySet();
Iterator<Entry<String, String>> iterator = set.iterator();
while(iterator.hasNext()) {
Entry entry = iterator.next();
String key = (String) entry.getKey();
String value = (String) entry.getValue();
System.out.println("key:" + key + ",value:" + value);
}
Copy the code
We insert in order xxx1, xxx2, xxx3, but the output is not in order.
For the same data, let’s try LinkedHashMap
Map<String, String> linkedHashMap = new LinkedHashMap<>();
linkedHashMap.put("name1", "josan1");
linkedHashMap.put("name2", "josan2");
linkedHashMap.put("name3", "josan3");
Set<Entry<String, String>> set = linkedHashMap.entrySet();
Iterator<Entry<String, String>> iterator = set.iterator();
while(iterator.hasNext()) {
Entry entry = iterator.next();
String key = (String) entry.getKey();
String value = (String) entry.getValue();
System.out.println("key:" + key + ",value:" + value);
}
Copy the code
Insert order and access order.
LinkedHashMap’s default construction parameter is insertion order by default, which means you insert in whatever order you read it in, but there’s also access order, which means you access a key and it goes to the back, right
Here accessOrder is set to false to indicate that the order stored in the LinkedHashMap is sorted by the order inserted by calling the PUT method, which is also the default. LinkedHashMap also provides a constructor that can set accessOrder. Let’s look at the order in this mode.
AccessOrder Map<String, String> linkedHashMap = new linkedHashMap <>(16, 0.75f, true); accessOrder Map<String, String> linkedHashMap = new linkedHashMap <>(16, 0.75f, true); linkedHashMap.put("name1", "josan1"); linkedHashMap.put("name2", "josan2"); linkedHashMap.put("name3", "josan3"); System.out.println(" start order: "); Set<Entry<String, String>> set = linkedHashMap.entrySet(); Iterator<Entry<String, String>> iterator = set.iterator(); while(iterator.hasNext()) { Entry entry = iterator.next(); String key = (String) entry.getKey(); String value = (String) entry.getValue(); System.out.println("key:" + key + ",value:" + value); } system.out. println(" get to end table with key name1 "); linkedHashMap.get("name1"); Set<Entry<String, String>> set2 = linkedHashMap.entrySet(); Iterator<Entry<String, String>> iterator2 = set2.iterator(); while(iterator2.hasNext()) { Entry entry = iterator2.next(); String key = (String) entry.getKey(); String value = (String) entry.getValue(); System.out.println("key:" + key + ",value:" + value); }Copy the code
The call to GET (“name1”) caused name1’s Entry to move to the end of the list
Usage of TreeMap (mainly sorting)
TreeMap’s default sort is ascending. If you want to change the sort, you can write a Comparator
import java.util.Comparator; import java.util.Iterator; import java.util.Set; import java.util.TreeMap; public class Compare { public static void main(String[] args) { TreeMap<String,Integer> map = new TreeMap<String,Integer>(new xbComparator()); map.put("key_1", 1); map.put("key_2", 2); map.put("key_3", 3); Set<String> keys = map.keySet(); Iterator<String> iter = keys.iterator(); while(iter.hasNext()) { String key = iter.next(); System.out.println(" "+key+":"+map.get(key)); } } } class xbComparator implements Comparator { public int compare(Object o1,Object o2) { String i1=(String)o1; String i2=(String)o2; return -i1.compareTo(i2); }}Copy the code