CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > Java > HashMap in java

Java Tutorial

  • Introduction
    • What is Java
    • History Of Java
    • Install Java
    • What is JVM
    • JDK vs JRE vs JVM
    • Java Bytecode
    • OOP vs POP
    • Compile and Run Java
  • Tokens, Expressions and Control Structures
    • Primitive data types
    • Integers
    • Floating Points
    • Characters
    • Booleans
    • User Defined Data Type
    • Declarations
    • Constants
    • Identifiers
    • Literals
    • Type Conversion and Casting
    • Variables
    • Default Variable Initialization
    • Command Line Arguments
    • Arrays of Primitive Types
    • Comment Syntax
    • Garbage Collection
    • Expressions
    • Operators
    • Arithmetic Operator
    • Bitwise and Shift Operator
    • Comparison or Relational Operators
    • Logical Operators
    • Assignment Operators
    • Ternary Operator
    • Increment and Decrement Operator
    • Control Statements
  • OOP Concepts
    • Class and Object
    • Create Class Instance
    • Method
    • Abstraction
    • Encapsulation
    • this keyword
    • Constructor
    • Pass by Value
    • Access Modifier/Control
    • Polymorphism
    • Method Overloading vs Method Overriding
    • Recursion
    • Nested and Inner Class
  • Inheritance and Packaging
    • Inheritance
    • extends Keyword
    • super Keyword
    • Object Class
    • Abstract class
    • Final Class
    • Java Package
    • Interface
  • Handling Error/ Exceptions
    • What is Exception
    • Exception Handling Keywords
    • Common Java Errors
    • User Defined Exception
    • Throwing and re-throwing Exception
    • finally Block
  • Strings
    • Java String Tutorial
  • Threads
    • Introduction
    • Create Thread
    • Thread Lifecycle
    • Thread Priority
    • Thread Synchronization
    • Inner Thread Communication
    • Thread Deadlock
  • IO and Streams
    • java.io Package
    • Files and Directories
    • Byte Stream
    • Character Stream
    • Console Input and Output
    • Serializable and Deserializable
  • Core Packages
    • java.lang Package
    • Math
    • Wrapper Classes
    • java.lang.Number
    • Double
    • Float
    • Integers
    • java.lang.Byte
    • java.lang.Short
    • java.lang.Long
    • java.lang.Character
    • java.lang.Boolean
    • java.util package
    • Vector Class
    • Stack Class
    • Dictionary Class
    • Hashtable
    • Enumeration or Enum
    • Generate Random Number
  • Holding Collection of Data
    • Arrays
    • Map
    • List
    • Set
    • Collection Interface
    • Collections Class
    • ArrayList
    • HashSet
    • TreeSet
    • Comparator
  • Java Bean
    • What is Java Bean
    • Advantages and Disadvantages of Java Bean
    • Java Beans API
    • Introspection
    • Java Bean Properties
    • Bound and Constrained Properties
    • BeanInfo Interface
    • Customizers
    • Java Beans Persistence
    • BeanDescriptor
  • Home

HashMap in java

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Last updated: July 1, 2026 ยท 4 min read ยท 0 Comments
Share: in X

HashMap is a widely used data structure in the Java programming language. It is a part of the Java Collection Framework and is an implementation of the Map interface. It provides a fast and efficient way to store and retrieve data in the form of key-value pairs. In this blog post, we will discuss what HashMap is, how it works, and how to use it in Java, including examples of the remove(), clear() and size() methods.

What is HashMap in Java?

A HashMap is a collection that stores key-value pairs, where each key is unique and is associated with a specific value. It is implemented using a hash table, which is a data structure that allows for fast and efficient data retrieval.

A hash table uses a hash function to map keys to indexes in an array. The hash function takes the key as input and returns an index in the array, where the value associated with that key is stored. When we want to retrieve a value from the HashMap, we simply provide the key and the hash function maps it to the correct index in the array, where the value is stored.

Why use HashMap in Java?

HashMap is a highly efficient data structure that is widely used in Java because of its fast and efficient data retrieval capabilities. It is particularly useful in situations where we need to store a large amount of data and need to access it quickly.

HashMap is also useful in situations where we need to store data that is frequently accessed, such as a database or a cache. It is also commonly used in situations where we need to store and retrieve data based on a key, such as a lookup table or a dictionary.

How to use HashMap in Java

Using a HashMap in Java is relatively simple. To create a HashMap, we first need to import the java.util package, which contains the HashMap class.

The commonly available methods in HashMap are:

MethodDescription
void put(Object key, Object value)This method helps to add key and values in the map
Object get(Object key)This method helps to retrieve the value associated with the given key from the map
int size()This method returns the size of the map
void clear()This method clears all the data from map and makes the map object as an empty

Add data into HashMap

Here is an example of creating a HashMap and adding data to it using put() method:

import java.util.HashMap;

HashMap<String, Integer> map = new HashMap<String, Integer>();
map.put("one", 1);
map.put("two", 2);
map.put("three", 3);

Output of this example:

{one=1, two=2, three=3}

Once we have created the HashMap, we can add data to it using the put() method. The put() method takes two arguments: the key and the value. In the example above, we are adding three key-value pairs to the HashMap.

Get data from HashMap

We can also retrieve data from a HashMap using the get() method. For example, to retrieve the value associated with the key “two”, we can use the following code:

Integer value = map.get("two");

Output of this example:

2

In addition to the put() and get() methods, HashMap also provides other useful methods such as remove(), clear(), and size().

Remove data from HashMap

Here is an example of using the remove() method:

map.remove("two");

Output of this example:

{one=1, three=3}

Check the size of HashMap

To check the size of the HashMap we can use the size() method as shown in the example below:

System.out.println(map.size());

Output of this example:

2

As you can see, the size() method returns the number of key-value pairs in the HashMap. In this example, it returns 2 because there are 2 key-value pairs in the map (one and three).

Clear data from HashMap

We may have a use case to make our HashMap object empty. In this case we can use the clear method clear() method as shown in the below example:

map.clear();

Output of this example:

{}

Related Posts:

  • The Map Interface in Java
  • Control Statements in Java
  • List in Java
  • Map in Java
  • MySQL Commands for Developers
  • Dictionary Class in Java
Tags:java
Was this article helpful?
โ† Previous ArticleThe Map Interface in Java
Next Article โ†’TreeMap in Java

Recent Posts

  • How to implement Passwordless Authentication in Spring Boot: A Step-by-Step Guide
  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Linkedin

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
ยฉ 2026 CoderSathi. All rights reserved. Privacy Policy ยท Sitemap