Java HashMap

Java 中的 HashMap 是一个集合类,它实现了 Map 接口。
它用于存储键值对。
每个键都映射到映射中的单个值。

键是唯一的。
这意味着我们只能在Map中插入一次键“K”。
不允许使用重复的key。
尽管值“V”可以映射到多个键。

  • HashMap 不能包含重复的键。
  • HashMap 允许多个 null值但只允许一个 null键。
  • HashMap 是一个无序集合。它不保证元素的任何特定顺序。
  • HashMap 不是线程安全的。我们必须显式同步对 HashMap 的并发修改。或者我们可以使用 Collections.synchronizedMap(hashMap) 来获取 HashMap 的同步版本。
  • 只能使用关联的键检索值。
  • HashMap 只存储对象引用。所以原语必须与其对应的包装类一起使用。例如int将存储为 Integer
  • HashMap 实现了 Cloneable 和 Serializable 接口。

HashMap 方法

HashMap 类中的方法

  • void clear() :从 HashMap 中删除所有键值对。
  • Object clone() :返回指定 HashMap 的浅拷贝。
  • boolean containsKey(Object key) :根据是否在地图中找到指定的键,返回 true或者 false
  • boolean containsValue(Object Value) :类似于 containsKey() 方法,它查找指定的值而不是键。
  • Object get(Object key) :返回 HashMap 中指定键的值。
  • boolean isEmpty() :检查地图是否为空。
  • Set keySet() :返回存储在 HashMap 中的所有键的 Set。
  • Object put(Key k, Value v) :将键值对插入到 HashMap 中。
  • int size() :返回映射的大小,它等于存储在 HashMap 中的键值对的数量。
  • Collection values() :返回地图中所有值的集合。
  • Value remove(Object key) :删除指定键的键值对。
  • void putAll(Map m) :将地图的所有元素复制到另一个指定的地图。

java.util.HashMap 类

HashMap 类声明

HashMap 声明如下:

public class HashMap<K,V> extends AbstractMap<K,V> 
				implements Map<K,V>, Cloneable, Serializable

HashMap 实现了Map接口并扩展了AbstractMap类。

更多: zhilu jiaocheng

Java HashMap 示例

添加key - HashMap.put()

import java.util.HashMap;
public class HashMapExample 
{
    public static void main(String[] args) throws CloneNotSupportedException 
    {
        HashMap<Integer, String> map = new HashMap<>();

        map.put(1,  'A');
        map.put(2,  'B');
        map.put(3,  'C');

        System.out.println(map);
    }
}

程序输出。

{1=A, 2=B, 3=C}

根据key获取值 - HashMap.get()

HashMap<Integer, String> map = new HashMap<>();

map.put(1,  'A');
map.put(2,  'B');
map.put(3,  'C');
String value = map.get(2);
System.out.println('The value is :: '+  value );

程序输出。

The value is :: B

删除键值对 HashMap.remove()

HashMap<Integer, String> map = new HashMap<>();
map.put(1, 'A');
map.put(2, 'B');
map.put(3, 'C');
System.out.println(map);
map.remove(3);
System.out.println(map);

程序输出。

{1=A, 2=B, 3=C}
{1=A, 2=B}

迭代HashMap (遍历HashMap)

请注意,此类的迭代器是快速失败的,如果在创建迭代器后进行任何结构修改,它将抛出 ConcurrentModificationException

HashMap<Integer, String> map = new HashMap<>();
map.put(1, 'A');
map.put(2, 'B');
map.put(3, 'C');
System.out.println('//Iterate over keys');
Iterator<Integer> itr = map.keySet().iterator();
while (itr.hasNext()) 
{
    Integer key = itr.next();
    String value = map.get(key);

    System.out.println('The key is :: ' + key + ', and value is :: ' + value );
}
System.out.println('//Iterate over entries set');
Iterator<Entry<Integer, String>> entryIterator = map.entrySet().iterator();
while (entryIterator.hasNext()) 
{
    Entry<Integer, String> entry = entryIterator.next();

    System.out.println('The key is :: ' + entry.getKey() + ', and value is :: ' + entry.getValue() );
}

程序输出。

//Iterate over keys
The key is :: 1, and value is :: A
The key is :: 2, and value is :: B
The key is :: 3, and value is :: C
//Iterate over entries set
The key is :: 1, and value is :: A
The key is :: 2, and value is :: B
The key is :: 3, and value is :: C
日期:2020-09-17 00:10:17 来源:oir作者:oir