Map
Map常用子类
- HashMap:HashMap 是最常用的 Map 实现之一,它基于哈希表实现,提供了 O(1) 时间复杂度的插入、删除和查找操作。
- Hashtable:Hashtable 是较早期的 Java 集合类,它是线程安全的,但性能通常比 HashMap 差,因为它的方法是同步的。
- TreeMap:TreeMap 是基于红黑树实现的有序 Map,它根据键的自然顺序或自定义顺序进行排序。TreeMap 提供了一些附加的方法来处理有序映射。
- LinkedHashMap:LinkedHashMap 继承自 HashMap,它使用双向链表维护插入顺序或者访问顺序,因此迭代顺序可以预测。
- ConcurrentHashMap:ConcurrentHashMap是Hashtable替代,相比性能较好,线程安全,无序。
- ConcurrentSkipListMap:线程安全,有序。
扩展:还有其他子类比如ConcurrentHashMap、WeakHashMap、IdentityHashMap、EnumMap...
EnumMap 是针对枚举类型的特定实现,它的键必须是枚举类型的值。
HashMap
/**
* hashMap_demo
* 实现方式:数组 + 链表 + (1.8加的红黑树)
* 遍历顺序:无序
* 是否线程安全:线程不安全
* 扩容机制: 当HashMap容量大小(默认16)达到容量的加载因子的大小(默认0.75)自动扩容两倍
* null值操作:允许使用一个 null 键,多个 null 值。
*/
@Test
public void hashMap_demo() {
HashMap<String,String> hashMap = new HashMap<>(16, 0.75f);
for (int i = 1; i <= 3; i++) {
hashMap.put("test"+i,"test");
}
hashMap.put(null,"test");
for (Map.Entry<String, String> entry : hashMap.entrySet()) {
System.out.print(" Key: " + entry.getKey());
}
}
执行打印
Key: null Key: test2 Key: test3 Key: test1
Hashtable
/**
* hashtable_demo
* 实现方式:数组 + 链表
* 遍历顺序:无序
* 是否线程安全:线程安全,所有公共方法都是同步的
* 扩容机制: 当HashMap容量大小(默认11)达到容量的加载因子的大小(默认0.75)自动扩容两倍
* 设置扩容参数方法:Map<String,String> map = new HashMap<>(11,0.75f);
* null值操作:不允许使用 null 键或 null 值
*/
@Test
public void hashtable_demo() {
Hashtable<String,String> hashtable = new Hashtable<>(11, 0.75f);
for (int i = 1; i <= 3; i++) {
hashtable.put("test"+i,"test");
}
//hashtable.put(null,"test");//键设置为null 抛出java.lang.NullPointerException
for (Map.Entry<String, String> entry : hashtable.entrySet()) {
System.out.print(" Key: " + entry.getKey());
}
}
执行打印
Key: test2 Key: test3 Key: test1
LinkedHashMap
/**
* linkedHashMap_demo linkedHashMap demo
* LinkedHashMap:LinkedHashMap 继承自 HashMap,它使用双向链表维护插入顺序或者访问顺序,因此迭代顺序可以预测。
* 实现方式:哈希表 + 双向链表
* 是否线程安全:线程不安全
* 遍历顺序:当元素被添加到 LinkedHashMap 中时,添加到链表末尾,遍历时,按照插入的顺序返回
* 设置访问顺序参数策略: 默认是false,设置为true的是如果有get或put操作,将被操作的元素追加到链表的末尾,顺序排后
* 扩容机制:与 HashMap 相同
* null值操作:允许使用一个 null 键,多个 null 值。
*/
@Test
public void linkedHashMap_demo(){
//设置顺序方式 默认是false
LinkedHashMap<String,String> linkedHashMap = new LinkedHashMap<>(16, 0.75f, false);
//LinkedHashMap<String,String> linkedHashMap = new LinkedHashMap<>(16, 0.75f, true);
for (int i = 1; i <= 3; i++) {
linkedHashMap.put("test"+i,"test");
}
// 默认按照插入顺序,遍历所有的键和值
for (Map.Entry<String, String> entry : linkedHashMap.entrySet()) {
System.out.print(" Key: " + entry.getKey());
}
System.out.println("\n --------------");
linkedHashMap.put("test0","test");
linkedHashMap.get("test1");
for (Map.Entry<String, String> entry : linkedHashMap.entrySet()) {
System.out.print(" Key: " + entry.getKey());
}
}
执行打印
1.设置为false的结果:
Key: test1 Key: test2 Key: test3
//--------------
Key: test1 Key: test2 Key: test3 Key: test0
2.设置为true的结果:
Key: test1 Key: test2 Key: test3
//--------------
Key: test2 Key: test3 Key: test0 Key: test1
ConcurrentHashMap
/**
* concurrentHashMap_demo
* 实现方式:数组 + 链表 + 红黑树
* 是否线程安全:线程安全 1.8开始使用 CAS 操作和循环来保证对数据的修改是线程安全的,之前使用分段锁。
* 遍历顺序:无序
*/
@Test
public void concurrentHashMap_demo(){
ConcurrentHashMap<String,String> concurrentHashMap = new ConcurrentHashMap<>(16, 0.75f);
for (int i = 1; i <= 3; i++) {
concurrentHashMap.put("test"+i,"test");
}
for (Map.Entry<String, String> entry : concurrentHashMap.entrySet()) {
System.out.print(" Key: " + entry.getKey());
}
}
执行打印
Key: test2 Key: test3 Key: test1
ConcurrentSkipListMap
/**
* concurrentSkipListMap_demo
* 实现方式:数组 + 链表 + 红黑树
* 是否线程安全:线程安全
* 遍历顺序:有序
* 顺序机制:默认按照key的第一个数字或字母顺序正序排序,也可以创建Comparator类重写compare方法排序逻辑
*/
@Test
public void concurrentSkipListMap_demo() throws InterruptedException {
// ConcurrentSkipListMap<String,String> concurrentSkipListMap = new ConcurrentSkipListMap<>();
ConcurrentSkipListMap<Integer, String> concurrentSkipListMap = new ConcurrentSkipListMap<>(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
System.out.print("o1:"+o1 +" o2:"+o2+"\n");
// 定义比较逻辑
// return o1.compareTo(o2);
return Integer.compare(o2,o1);//倒序
}
});
for (int i = 1; i <= 3; i++) {
concurrentSkipListMap.put( i,"test");
}
for (Map.Entry<Integer, String> entry : concurrentSkipListMap.entrySet()) {
System.out.print(" Key: " + entry.getKey());
}
}
执行打印
标签:map,顺序,java,HashMap,demo,线程,Key,entry,collections From: https://www.cnblogs.com/yu-si/p/18143516o1:2 o2:1
o1:3 o2:2
Key: 3 Key: 2 Key: 1