LinkedHashMap 概述
java.util.LinkedHashMap<k,v>集合 extends HashMap<k,v>集合
LinkedHashMap的特点:
1. LinkedHashMap集合底层是哈希表+链表(保证迭代的顺序):查询速度快
2. LinkedHashMap集合是一个有序的集合,存储元素和取出元素的顺序是一致的
Demo:
1 HashMap<String,String> map = new HashMap<>();
2 map.put("a","a");
3 map.put("c","c");
4 map.put("b","b");
5 map.put("a","d");
6 System.out.println(map);// key不允许重复,无序 {a=d, b=b, c=c}
7
8 LinkedHashMap<String,String> linked = new LinkedHashMap<>();
9 linked.put("a","a");
10 linked.put("c","c");
11 linked.put("b","b");
12 linked.put("a","d");
13 System.out.println(linked);// key不允许重复,有序 {a=d, c=c, b=b}