Java HashMap

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值