📜  Java.util.HashMap类

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


介绍

java.util.HashMap类是基于哈希表的Map接口实现。以下是有关HashMap的要点-

  • 此类无法保证地图的迭代顺序;特别是,它不能保证顺序会随着时间的推移保持恒定。

  • 此类允许空值和空键。

类声明

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

public class HashMap
   extends AbstractMap
   implements Map, Cloneable, Serializable

参量

以下是java.util.HashMap类的参数-

  • K-这是此映射维护的键的类型。

  • V-这是映射值的类型。

类的构造函数

Sr.No. Constructor & Description
1

HashMap()

This constructs an empty HashMap with the default initial capacity (16) and the default load factor (0.75).

2

HashMap(Collection c)

This constructs an empty HashMap with the specified initial capacity and the default load factor (0.75).

3

HashMap(int initialCapacity, float loadFactor)

This constructs an empty HashMap with the specified initial capacity and load factor.

4

HashMap(Map m)

This constructs a new HashMap 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 Object clone()

This method returns a shallow copy of this HashMap instance, the keys and values themselves are not cloned.

3 boolean containsKey(Object key)

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

4 boolean containsValue(Object value)

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

5 Set> entrySet()

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

6 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.

7 boolean isEmpty()

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

8 Set keySet()

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

9 V put(K key, V value)

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

10 void putAll(Map m)

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

11 V remove(Object key)

This method removes the mapping for the specified key from this map if present.

12 int size()

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

13 Collection values()

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

方法继承

此类从以下类继承方法-

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