Java java.util.IdentityHashMap类使用教程

x33g5p2x  于2021-08-21 转载在 Java  
字(2.9k)|赞(0)|评价(0)|浏览(290)

在这篇文章中,我们将讨论来自java.util package的IdentityHashMap。

我们将学到什么?

  1. IdentityHashMap类概述
  2. IdentityHashMap类的构造函数
  3. IdentityHashMap类方法
  4. IdentityHashMap类示例

1. IdentityHashMap类概述

这个类不是一个通用的Map实现! 虽然该类实现了Map接口,但是它故意违反了Map的一般契约,该契约规定在比较对象时必须使用等价方法。该类仅设计用于需要引用平等语义的少数情况。

IdentityHashMap是Map接口的基于HashTable的实现。接口。普通的HashMap使用'.equals'方法对键进行比较。但是IdentityHashMap使用'=='操作符来比较其键值。
注意,这个实现是不同步的。如果多个线程同时访问一个IdentityHashMap,并且至少有一个线程在结构上修改了该图,那么它必须在外部进行同步。 

举例来说。

Map m = Collections.synchronizedMap(new IdentityHashMap(...));

2. IdentityHashMap类构造函数

  • IdentityHashMap() - 构造一个新的、空的IdentityHashMap,默认的最大预期大小为21。
  • IdentityHashMap(int expectedMaxSize) - 构建一个新的、空的、具有指定最大预期大小的Map。
  • IdentityHashMap(Map<?extends K,?extends V> m) - 构建一个新的IdentityHashMap,包含指定Map中的键值映射。

3. IdentityHashMap类方法

  • void clear() - 移除该Map中的所有映射。
  • Object clone()- 返回这个IdentityHashMap的一个浅层拷贝:键和值本身并没有被克隆。
  • boolean containsKey(Object key) - 测试指定的对象引用是否是这个IdentityHashMap中的一个键。
  • boolean containsValue(Object value) - 测试指定的对象引用是否是这个IdentityHashMap中的一个值。
  • Set<Map.Entry<K,V>> entrySet()-返回这个Map中包含的映射的Set视图。
  • boolean equals(Object o) - 将指定的对象与这个Map进行比较,看是否相等。
  • void forEach(BiConsumer<?super K,?super V> action)- 对这个Map中的每个条目执行给定的动作,直到所有的条目都被处理或者动作抛出一个异常。 
  • V get(Object key) - 返回指定键所映射的值,如果这个Map不包含键的映射,则返回空值。
  • int hashCode() - 返回这个Map的哈希代码值。
  • boolean isEmpty() - 如果这个IdentityHashMap不包含键值映射,则返回true。
  • Set keySet() - 返回这个Map中包含的键的基于身份的集合视图。
  • V put(K key, V value) - 将指定的值与该IdentityHashMap中的指定键关联起来。
  • void putAll(Map<?extends K,?extends V> m) - 将指定Map中的所有映射复制到此Map中。
  • V remove(Object key) - 如果存在的话,从这个Map中删除这个键的映射。
  • void replaceAll(BiFunction<?super K,?super V,?extends V> function) - 用对每个条目调用给定函数的结果来替换该条目的值,直到所有条目都被处理或该函数抛出一个异常。
  • int size() -返回该IdentityHashMap中键值映射的数量。
  • Collection values() -返回该映射中包含的数值的集合视图。

4. IdentityHashMap类示例

正如我们所知,IdentityHashMap是一个基于HashTable的Map接口的实现。接口。普通的HashMap使用'.equals'方法对键进行比较。但是IdentityHashMap使用'=='操作符比较其键值。因此,'a'和new String('a')被认为是两个不同的键。IdentityHashMap的初始大小为21,而正常HashMap的初始大小为16。

import java.util.IdentityHashMap;

public class IdentityHashMapExample {
 public static void main(final String[] args) {
   final IdentityHashMap<String, String> identityHashMap = new IdentityHashMap<String, String>();

         identityHashMap.put("a", "Java");
         identityHashMap.put(new String("a"), "J2EE");
         identityHashMap.put("b", "J2SE");
         identityHashMap.put(new String("b"), "Spring");
         identityHashMap.put("c", "Hibernate");

         for (final String str : identityHashMap.keySet()) {
             System.out.println("Key : " + str + " and Value : " + identityHashMap.get(str));
         }

         System.out.println("Size of map is : " + identityHashMap.size());
         System.out.println("Here 'a' and new String('a') are considered as separate keys");
 }
}

输出:

Key : a and Value : Java
Key : b and Value : J2SE
Key : c and Value : Hibernate
Key : b and Value : Spring
Key : a and Value : J2EE
Size of map is : 5
Here 'a' and new String('a') are considered as separate keys


###参考资料

https://docs.oracle.com/javase/8/docs/api/java/util/IdentityHashMap.html

相关文章

微信公众号

最新文章

更多