Java 集合框架Hashmap
原文:https://www.studytonight.com/java/hashmap-in-collection-framework.php
Java HashMap 类是基于HashMap
的 Map 接口的实现。它存储键和值对中的元素,表示为HashMap
HashMap
扩展了 AbstractMap 类,实现了 Map 接口,可以通过导入 java.util 包进行访问。下面给出了这个类的声明。
HashMap
声明
public class HashMap<K,V>extends AbstractMap<K,V>implements Map<K,V>,Cloneable, Serializable
要点:
- 它是 Java 集合框架的成员。
- 它使用
HashMap
来存储映射。这使得get()
和put()
的执行时间保持不变。 - HashMap 不维护其元素的顺序。
- 它包含基于键的值。
- 它只允许唯一的键。
- 它是不同步的。
- 它的初始默认容量是 16。
- 它允许空值和空键
- 它是不同步的。
HashMap
构造器
HashMap 类提供了以下四种构造器。
HashMap()
HashMap(Map< ? extends k, ? extends V> m)
HashMap(int capacity)
HashMap(int capacity, float loadfactor)
示例:创建HashMap
让我们举一个例子来创建一个可以存储整型键和字符串值的 hashmap。最初它是空的,因为我们没有向它添加元素。它的元素用花括号括起来。
import java.util.*;
class Demo
{
public static void main(String args[])
{
// Creating HashMap
HashMap<Integer,String> hashMap = new HashMap<Integer,String>();
// Displaying HashMap
System.out.println(hashMap);
}
}
{}
向HashMap
添加元素
创建HashMap
后,现在让我们向其中添加元素。HashMap 提供了put()
方法,该方法接受两个参数,第一个是键,第二个是值。见下面的例子。
import java.util.*;
class Demo
{
public static void main(String args[])
{
// Creating HashMap
HashMap<Integer,String> hashMap = new HashMap<Integer,String>();
// Adding elements
hashMap.put(1, "One");
hashMap.put(2, "Two");
hashMap.put(3, "Three");
hashMap.put(4, "Four");
// Displaying HashMap
System.out.println(hashMap);
}
}
{ 1 =一,2 =二,3 =三,4 =四}
从HashMap
中移除元素
在这种情况下,我们需要从 hashmap 中移除任何元素。我们可以使用 remove()方法,该方法将 key 作为参数。它有一个重载的 remove()方法,该方法接受两个参数:第一个是键,第二个是值。
import java.util.*;
class Demo
{
public static void main(String args[])
{
// Creating HashMap
HashMap<Integer,String> hashMap = new HashMap<Integer,String>();
// Adding elements
hashMap.put(1, "One");
hashMap.put(2, "Two");
hashMap.put(3, "Three");
hashMap.put(4, "Four");
// Displaying HashMap
System.out.println(hashMap);
// Remove element by key
hashMap.remove(2);
System.out.println("After Removing 2 :\n"+hashMap);
// Remove by key and value
hashMap.remove(3, "Three");
System.out.println("After Removing 3 :\n"+hashMap);
}
}
{ 1 =一,2 =二,3 =三,4 =四}移除 2 后:{ 1 =一,3 =三,4 =四}移除 3 后:{ 1 =一,4 =四}
遍历元素
为了访问 hashmap 的元素,我们可以使用循环遍历它们。在这个例子中,我们使用 for 循环来迭代元素。
import java.util.*;
class Demo
{
public static void main(String args[])
{
// Creating HashMap
HashMap<Integer,String> hashMap = new HashMap<Integer,String>();
// Adding elements
hashMap.put(1, "One");
hashMap.put(2, "Two");
hashMap.put(3, "Three");
hashMap.put(4, "Four");
// Traversing HashMap
for(Map.Entry<Integer, String> entry : hashMap.entrySet()) {
System.out.println(entry.getKey()+" : "+entry.getValue());
}
}
}
1:1 2:2 3:3 4:4
替换HashMap
元素
HashMap 提供了替换元素的内置方法。有两种重载替换方法:第一种方法接受两个参数,一个用于键,第二个用于要替换的值。第二个方法接受三个参数第一个是键,第二个是与键相关联的值,第三个是我们要用键值替换的值。
import java.util.*;
class Demo
{
public static void main(String args[])
{
// Creating HashMap
HashMap<Integer,String> hashMap = new HashMap<Integer,String>();
// Adding Elements
hashMap.put(1, "One");
hashMap.put(2, "Two");
hashMap.put(3, "Three");
hashMap.put(4, "Four");
// Traversing HashMap
for(Map.Entry<Integer, String> entry : hashMap.entrySet()) {
System.out.println(entry.getKey()+" : "+entry.getValue());
}
// Replacing Elements of HashMap
hashMap.replace(1, "One-1");
System.out.println(hashMap);
hashMap.replace(1, "One-1", "First");
System.out.println(hashMap);
}
}
1:1 2:2 3:3 4:4 { 1 = 1-1,2 = 2,3 = 3,4 = 4 } { 1 =第一,2 = 2,3 = 3,4 = 4 }