Java Hashmap size() Method

What is a Hashmap?

Hashmap was first introduced in Java 1.2.The Hashmap class is included in the java.util package.The data in a hashmap is stored in two parts: the key and the value, which can be accessed using an index of another type.A hashmap have unique keys , inserting a duplicate key will replace the data of the key. Hashmap class have many inbuilt method, one of those method is Java Hashmap size() Method.

To know more about Hashmap size() Method read the complete article

Java Hashmap size() Method

The hashmap size() method checks the size of the hashmap . In other words it checks how many key/ value pairs are present in the hashmap. Below in this page you can find it’s syntax, return values, parameters with detailed examples.

Syntax :

hashmap.size();

Parameters :

This method does not take any parameters.

Return values :

It returns the size of the hashmap.
Java Hashmap size method

Hashmap size() Method Examples

Example :

Run
import java.util.*;

public class Main
{
  public static void main (String[]args)
  {

    // Creating an empty HashMap
    HashMap < Integer, String > hash_map_1 =
      new HashMap < Integer, String > ();


    // Mapping string values to int keys in a empty hashmap
    hash_map_1.put (10, "Java");
    hash_map_1.put (20, "Hashmap");
    hash_map_1.put (25, "size");
    hash_map_1.put (30, "method");


    //checking the size of the hashmap
    System.out.println (hash_map_1.size ());

  }
}



Output :

Size of hashmap is : 4

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription