1. HashMap and linkedHashMap and treeMap

 

1 * LinkedHashMap is derived from HashMap and is based on HashMap and bidirectional linked lists.

2 * HashMap unordered; The LinkedHashMap is ordered and can be divided into insert order and access order.

3 * In access order, both put and GET operations on existing entries move the Entry to the end of the bidirectional list.

4 * LinkedHashMap accesses data in the same way as HashMap uses Entry[], two-way lists are only for order.

5 * LinkedHashMap is thread-unsafe.

 

A hashMap sample

/ / disordered

Map<String,String> hashMap = new HashMap<>();

HashMap. Put (” No1 “, “xiao Ming 1”);

HashMap. Put (” No2 “, “xiao Ming 2”);

HashMap. Put (” No3 – “, “xiao Ming 3”);

System.out.println(“hashMap=>” + hashMap);

 

LinkedHashMap sample

 

1 // Ordered, thread unsafe, bidirectional linked list

2 // LinkedHashMap’s default constructor parameters are inserted in order by default, that is, in what order do you insert, 3, 3 read in what order, but there is also access order, that is, if you access a key, the key goes after the last 4

5 // accessOrder is set to false, meaning that it is stored not in accessOrder but in insert order, which is also the default,

6 // indicates that the order stored in the LinkedHashMap is sorted in the order inserted by calling the PUT method.

7 LinkedHashMap also provides a constructor that can set accessOrder

8 Map<String,String> linkedHashMap = new linkedHashMap <>(16,0.75f,true);

9 linkedHashMap. Put (“No1″,” xiaoming1 “);

10 linkedHashMap. Put (“No2″,” xiaoming2 “);

11 linkedhashmap. put(“No3″,” xiaoming3 “);

12 system.out.println (” start =>linkedHashMap=>” + linkedHashMap);

13 / / set the iteration

14 // Set<Map.Entry<String,String>> set = hashMap.entrySet();

15 // System.out.println(set);

16 // Iterator<Map.Entry<String,String>> iterator = set.iterator();

17 // while(iterator.hasNext()){

18 // Map.Entry<String,String> entry = iterator.next();

19 // System.out.println(“key=>” + (String) entry.getKey());

20 // System.out.println(“value=>” + (String) entry.getValue());

21 // }

22 Set<Map.Entry<String,String>> set2 = linkedHashMap.entrySet();

23 Iterator<Map.Entry<String,String>> iterator2 = set2.iterator();

24 while(iterator2.hasNext()){

25 Map.Entry entry = iterator2.next();

26 String key = (String) entry.getKey();

27 String value = (String) entry.getValue();

28 System.out.println(“key:” + key + “,value:” + value);

29}

30 String s = linkedHashMap.get(“No2”);

31 system.out.println (” end =>linkedHashMap=>” + linkedHashMap);

32 Set<Map.Entry<String,String>> set3 = linkedHashMap.entrySet();

33 Iterator<Map.Entry<String,String>> iterator3 = set3.iterator();

34 while(iterator3.hasNext()){

35 Map.Entry entry = iterator3.next();

37 String key = (String) entry.getKey();

38 String value = (String) entry.getValue();

39 System.out.println(“key:” + key + “,value:” + value);

40}

treeMap

 

1 // TreeMap’s default sort is ascending. If you want to change the sort, you can write a Comparator

2 TreeMap<String,Integer> treeMap = new TreeMap<String,Integer>();

3 treeMap.put(“No1”,1);

4 treeMap.put(“No3”,3);

5 treeMap.put(“No2”,2);

6 System.out.println(“treeMap=>” + treeMap);

Please keep smiling eyes, in order to see more beautiful scenery, the rest of your life is expensive, do not waste