JAVA HASHMAP

--help cse
4 min readApr 6, 2022

Java Hashmap basic programs and important method.

INTRODUCTION:

In the ArrayList chapter, you learned that Arrays store items as an ordered collection, and you have to access them with an index number (int type). A HashMap however, store items in “key/value” pairs, and you can access them by an index of another type (e.g. a String).

One object is used as a key (index) to another object (value). It can store different types: String keys and Integer values, or the same type, like: String keys and String values:

Syntax:

Create a HashMap object called capitalCities that will store String keys and String values:

import java.util.HashMap; // import the HashMap class

HashMap<String, String> capitalCities = new HashMap<String, String>();

Add Items

The HashMap class has many useful methods. For example, to add items to it, use the put() method:

Program 1:Add item in hashmap.

Here,you have need to add key-value pair.

CODE:

// Import the HashMap class

import java.util.HashMap;

public class Main {

public static void main(String[] args) {

// Create a HashMap object called capitalCities

HashMap<String, String> capitalCities = new HashMap<String, String>();

// Add keys and values (Country, City)

capitalCities.put(“Virat”, “India”);

capitalCities.put(“AB”, “Africa”);

capitalCities.put(“Kane”, “NZ”);

capitalCities.put(“SMITH”, “AUS”);

System.out.println(capitalCities);

}

}

OUTPUT:

{AB=Africa, SMITH=AUS, Kane=NZ, Virat=India}

Note*:IT is stored in random order in map

Access an Item

To access a value in the HashMap, use the get() method and refer to its key:

Program 2:Access item using key

CODE:

import java.util.HashMap;

public class Main {

public static void main(String[] args) {

HashMap<String, String> capitalCities = new HashMap<String, String>();

capitalCities.put(“England”, “London”);

capitalCities.put(“Germany”, “Berlin”);

capitalCities.put(“Norway”, “Oslo”);

capitalCities.put(“USA”, “Washington DC”);

System.out.println(capitalCities.get(“England”));

}

}

OUTPUT:

London

Note*:Key we have passed England so it will printead london value.

Remove an Item

To remove an item, use the remove() method and refer to the key:

Program 3:Remove key values.

If we remove key then its value also automatically remove.

CODE:

import java.util.HashMap;

public class Main {

public static void main(String[] args) {

HashMap<String, String> capitalCities = new HashMap<String, String>();

capitalCities.put(“England”, “London”);

capitalCities.put(“Germany”, “Berlin”);

capitalCities.put(“Norway”, “Oslo”);

capitalCities.put(“USA”, “Washington DC”);

capitalCities.remove(“England”);

System.out.println(capitalCities);

}

}

OUTPUT:

{USA=Washington DC, Norway=Oslo, Germany=Berlin}

Clear Method.

To remove all items, use the clear() method:

Program 4: To remove all items, use the clear() method:

CODE:

import java.util.HashMap;

public class Main {

public static void main(String[] args) {

HashMap<String, String> capitalCities = new HashMap<String, String>();

capitalCities.put(“England”, “London”);

capitalCities.put(“Germany”, “Berlin”);

capitalCities.put(“Norway”, “Oslo”);

capitalCities.put(“USA”, “Washington DC”);

capitalCities.clear();

System.out.println(capitalCities);

}

}

OUTPUT:

{}

HashMap Size

To find out how many items there are, use the size() method:

Program 5:Calculating hashmap size.

CODE:

import java.util.HashMap;

public class Main {

public static void main(String[] args) {

HashMap<String, String> capitalCities = new HashMap<String, String>();

capitalCities.put(“England”, “London”);

capitalCities.put(“Germany”, “Berlin”);

capitalCities.put(“Norway”, “Oslo”);

capitalCities.put(“USA”, “Washington DC”);

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

}

}

OUTPUT:

4

Loop Through a HashMap

Loop through the items of a HashMap with a for-each loop.

Note: Use the keySet() method if you only want the keys, and use the values() method if you only want the values:

Program 6:Printing hashmaplist using for loop

Note:Here we have used Keyset() so it will print all keys.

CODE:

import java.util.HashMap;

public class Main {

public static void main(String[] args) {

HashMap<String, String> capitalCities = new HashMap<String, String>();

capitalCities.put(“England”, “London”);

capitalCities.put(“Germany”, “Berlin”);

capitalCities.put(“Norway”, “Oslo”);

capitalCities.put(“USA”, “Washington DC”);

for (String i : capitalCities.keySet()) {

System.out.println(i);

}

}

}

OUTPUT:

USA
Norway
England
Germany

Program 7:Printing values of hashmap

Note:Here we have used values() so it will print all values

CODE:

import java.util.HashMap;

public class Main {

public static void main(String[] args) {

HashMap<String, String> capitalCities = new HashMap<String, String>();

capitalCities.put(“England”, “London”);

capitalCities.put(“Germany”, “Berlin”);

capitalCities.put(“Norway”, “Oslo”);

capitalCities.put(“USA”, “Washington DC”);

for (String i : capitalCities.values()) {

System.out.println(i);

}

}

}

OUTPUT:

Washington DC
Oslo
London
Berlin

Other Types

Keys and values in a HashMap are actually objects. In the examples above, we used objects of type “String”. Remember that a String in Java is an object (not a primitive type). To use other types, such as int, you must specify an equivalent wrapper class: Integer. For other primitive types, use: Boolean for boolean, Character for char, Double for double, etc:

Program 8:String integer key value pair

CODE:

// Import the HashMap class

import java.util.HashMap;

public class Main {

public static void main(String[] args) {

// Create a HashMap object called people

HashMap<String, Integer> people = new HashMap<String, Integer>();

// Add keys and values (Name, Age)

people.put(“John”, 32);

people.put(“Steve”, 30);

people.put(“Angie”, 33);

for (String i : people.keySet()) {

System.out.println(“key: “ + i + “ value: “ + people.get(i));

}

}

}

OUTPUT:

Name: Angie Age: 33
Name: Steve Age: 30
Name: John Age: 32

You can use any other mix data type such as string,float and so on.

Program 9:Change value using key and put method

CODE:

// Java program to change

// elements of HashMap

import java.io.*;

import java.util.*;

class ChangeElementsOfHashMap {

public static void main(String args[])

{

// Initialization of a HashMap

HashMap<Integer, String> hm

= new HashMap<Integer, String>();

// Change Value using put method

hm.put(1, “Somnath”);

hm.put(2, “Sachin”);

hm.put(3, “You”);

System.out.println(“Initial Map “ + hm);

hm.put(2, “good”);

System.out.println(“Updated Map “ + hm);

}

}

OUTPUT:

Initial Map {1=Somnath, 2=Sachin, 3=You}Updated Map {1=Somnath 2=good, 3=You}

Program 10:Checking its key is present or not using containsKey() method

CODE:

import java.util.*;

public class Hash_Map_Demo {

public static void main(String[] args)

{

// Creating an empty HashMap

HashMap<Integer, String> hash_map = new HashMap<Integer, String>();

// Mapping string values to int keys

hash_map.put(10, “Somnath”);

hash_map.put(15, “4”);

hash_map.put(20, “Sachin”);

hash_map.put(25, “Welcomes”);

hash_map.put(30, “You”);

// Displaying the HashMap

System.out.println(“Initial Mappings are: “ + hash_map);

// Checking for the key_element ‘20’

System.out.println(“Is the key ‘20’ present? “ +

hash_map.containsKey(20));

// Checking for the key_element ‘5’

System.out.println(“Is the key ‘5’ present? “ +

hash_map.containsKey(5));

}

}

OUTPUT:

Initial Mappings are: {20=Sachin, 25=Welcomes, 10=Somnath, 30=You, 15=4}Is the key '20' present? trueIs the key '5' present? false

Thank you.I have discussed all important method in hashmap .So I hope you like it.

--

--