📜  Java.util.WeakHashMap类

📅  最后修改于: 2020-11-14 06:40:24             🧑  作者: Mango


介绍

java.util.WeakHashMap类是具有弱键的基于哈希表的Map实现。当不再使用其键时,WeakHashMap中的条目将由垃圾收集器自动删除。以下是有关WeakHashMap的要点-

  • 空值和空键均受支持。

  • 像大多数集合类一样,此类也不同步。

  • 此类主要用于与其equals方法使用==运算符测试对象标识的关键对象一起使用。

  • WeakHashMap中的每个关键对象都间接存储为弱引用的引用。

  • 此类是Java Collections Framework的成员。

类声明

以下是java.util.WeakHashMap类的声明-

public class WeakHashMap 
   extends AbstractMap
   implements Map

这里是此映射维护的键的类型,而是映射值的类型。

类的构造函数

Sr.No. Constructor & Description
1

WeakHashMap()

This constructor is used to create an empty WeakHashMap with the default initial capacity (16) and load factor (0.75).

2

WeakHashMap(int initialCapacity)

This constructor is used to create an empty WeakHashMap with the given initial capacity and the default load factor (0.75).

3

WeakHashMap(int initialCapacity, float loadFactor)

This constructor is used to create an empty WeakHashMap with the given initial capacity and the given load factor.

4

WeakHashMap(Map m)

This constructor is used to create a new WeakHashMap with the same mappings as the specified map.

类方法

Sr.No. Method & Description
1 void clear()

This method removes all of the mappings from this map.

2 boolean containsKey(Object key)

This method returns true if this map contains a mapping for the specified key.

3 boolean containsValue(Object value)

This method returns true if this map maps one or more keys to the specified value.

4 SetK,V>> entrySet()

This method returns a Set view of the mappings contained in this map.

5 v get(Object key)

This method returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.

6 boolean isEmpty()

This method returns true if this map contains no key-value mappings.

7 Set keySet()

This method returns a Set view of the keys contained in this map.

8 v put(K key, V value)

This method associates the specified value with the specified key in this map.

9 void putAll(Map m)

This method copies all of the mappings from the specified map to this map.

10 v remove(Object key)

This method removes the mapping for a key from this weak hash map if it is present.

11 int size()

This method returns the number of key-value mappings in this map.

12 Collection values()

This method returns a Collection view of the values contained in this map.

方法继承

此类从以下类继承方法-

  • java.util.AbstractMap
  • java.lang.Object
  • java.util.Map