In this tutorial, we will learn about the Java HashMap class and its methods with the help of examples.
The HashMap
class of the Java collections framework provides the hash table implementation of the Map interface.
Java集合框架的HashMap类提供Map接口的哈希表实现。
Create a HashMap
In order to create a hash map, we must import the java.util.HashMap
package first. Once we import the package, here is how we can create hashmaps in Java.
为了创建哈希映射,我们必须首先导入java.util.HashMap包。导入程序包后,可以使用以下方法在Java中创建HashMap
// HashMap creation with 8 capacity and 0.6 load factor
HashMap<Key, Value> numbers = new HashMap<>(8, 0.6f);
In the above code, we have created a hashmap named numbers.
Here,
- Key - a unique identifier used to associate each element (value) in a map
- Value - elements associated by keys in a map
Notice the part new HashMap<>(8, 0.6)
. Here, the first parameter is capacity and the second parameter is loadFactor.
- capacity - The capacity of this hash map is 8. Meaning, it can store 8 entries.
- loadFactor - The load factor of this hashmap is 0.6. This means, whenever our hash table is filled by 60%, the entries are moved to a new hash table of double the size of the original hash table.
Default capacity and load factor
It’s possible to create a hashmap without defining its capacity and load factor. For example,
// HashMap with default capacity and load factor
HashMap<Key, Value> numbers1 = new HashMap<>();
By default,
- the capacity of the hash map will be 16
- the load factor will be 0.75
Creating HashMap from Other Maps
Here is how we can create a hashmap containing all the elements of other maps.
package com.programiz.hashmap;
import java.util.HashMap;
public class CreateHashMap {
public static void main(String[] args) {
// Creating a hashmap of even numbers
HashMap<String, Integer> evenNumbers = new HashMap<>();
evenNumbers.put("Two", 2);
evenNumbers.put("Four", 4);
System.out.println("HashMap1: " + evenNumbers);
// Creating a hash map from other hashmap
HashMap<String, Integer> numbers = new HashMap<>(evenNumbers);
numbers.put("Three", 3);
System.out.println("HashMap2: " + numbers);
}
}
Output
HashMap1: {
Four=4, Two=2}
HashMap2: {
Two=2, Three=3, Four=4}
Methods of HashMap
The HashMap
class provides various methods that allow us to perform various operations on the map.
HashMap类提供了各种方法,可让其在map上执行各种操作
Insert Elements to HashMap
put()
- inserts the specified key/value mapping to the map- 将指定的键/值映射插入到map中
putAll()
- inserts all the entries from specified map to this map- 将指定映射中的所有条目插入到map中
putIfAbsent()
- inserts the specified key/value mapping to the map if the specified key is not present in the map- 如果map中不存在指定的键,则将指定的键/值映射插入到map中
For example,
package com.programiz.hashmap;
import java.util.HashMap;
public class InsertElement {
public static void main(String[] args) {
// Creating HashMap of even numbers
HashMap<String, Integer> evenNumbers = new HashMap<>();
// Using put()
evenNumbers.put("Two", 2);
evenNumbers.put("Four", 4);
// Using putIfAbsent()
evenNumbers.putIfAbsent("Six", 6);
System.out.println("HashMap of even numbers: " + evenNumbers);
//Creating HashMap of numbers
HashMap<String, Integer> numbers = new HashMap<>();
numbers.put("One", 1);
// Using putAll()
numbers.putAll(evenNumbers);
System.out.println("HashMap of numbers: " + numbers);
}
}
Output
HashMap of even numbers: {
Six=6, Four=4, Two=2}
HashMap of numbers: {
Six=6, One=1, Four=4, Two=2}
Access HashMap Elements
1. Using entrySet(), keySet() and values()
entrySet()
- returns a set of all the key/value mapping of the map- 返回map中所有键/值的集合
keySet()
- returns a set of all the keys of the map- 返回map中所有键的集合
values()
- returns a set of all the values of the map- 返回map中所有值的集合
For example,
package com.programiz.hashmap;
import java