Java HashMap

The HashMap is a Collections class in Java. Creating a HashMap requires key/value pairs. It is a part of the java.util package i.e., you need to import the java.util.HashMap package.

Let us see some LinkedList operation/ methods with examples:

  • Create a HashMap
  • Size of a HashMap
  • Display Values from a HashMap
  • Display Keys from a HashMap
  • Remove an item from a HashSMap

Let us first see how to create a HashMap in Java.

Create a HashMap

To create a HashMap with string key and value pair, use <String, String>. This creates String keys and String values. Set the type in angular braces i.e., use the wrapper class:

HashMap<String, String> myMap = new HashSet <String, String>();

Let us now see an example and create a HashMap in Java with string elements:

import java.util.HashMap;

class Studyopedia {

  public static void main(String[] args) {

    // HashMap with key value pairs
    HashMap<String, String> player = new HashMap<String, String>();

    player.put("Virat", "India");
    player.put("Steve", "Australia");
    player.put("Joe", "England");
    player.put("Kane", "New Zealand");
    player.put("Karunaratne", "Sri Lanka");
    player.put("Litton", "Bangladesh");
    
    System.out.println(player); 
  }
}

Output

{Joe=England, Steve=Australia, Kane=New Zealand, Virat=India, Karunaratne=Sri Lanka}

Size of a HashMap

To get the size of HashMap i.e., how many elements does it have, use the size() method. Let us see an example:

import java.util.HashMap;

class Studyopedia {

  public static void main(String[] args) {

    // HashMap with key value pairs
    HashMap<String, String> player = new HashMap<String, String>();

    player.put("Virat", "India");
    player.put("Steve", "Australia");
    player.put("Joe", "England");
    player.put("Kane", "New Zealand");
    player.put("Karunaratne", "Sri Lanka");
    
    System.out.println(player); 
    
    // Size of the HashMap
    System.out.println("\nHashMap Size = "+player.size());
  }
}

Output

{Joe=England, Steve=Australia, Kane=New Zealand, Virat=India, Karunaratne=Sri Lanka}

HashMap Size = 5

Display Values from a HashMap

The values() method is used to display only values from the key/value pair of a HashMap in Java. Let us see an example:

import java.util.HashMap;

class Studyopedia {

  public static void main(String[] args) {

    // HashMap with key value pairs
    HashMap<String, String> player = new HashMap<String, String>();

    player.put("Virat", "India");
    player.put("Steve", "Australia");
    player.put("Joe", "England");
    player.put("Kane", "New Zealand");
    player.put("Karunaratne", "Sri Lanka");
    
    System.out.println(player); 
    
    // Display values
    System.out.println("\nValues...(Country Name)"); 
    for (String s : player.values()) {
      System.out.println(s);
    }
  }
}

Output

{Joe=England, Steve=Australia, Kane=New Zealand, Virat=India, Karunaratne=Sri Lanka}

Values...(Country Name)
England
Australia
New Zealand
India
Sri Lanka

Display Keys from a HashMap

The keySet() method is used to display only keys from the key/value pair of a HashMap in Java. Let us see an example:

import java.util.HashMap;

class Studyopedia {

  public static void main(String[] args) {

    // HashMap with key value pairs
    HashMap<String, String> player = new HashMap<String, String>();

    player.put("Virat", "India");
    player.put("Steve", "Australia");
    player.put("Joe", "England");
    player.put("Kane", "New Zealand");
    player.put("Karunaratne", "Sri Lanka");
    
    System.out.println(player); 
    
    // Display values
    System.out.println("\nKeys...(Player Name)"); 
    for (String s : player.keySet()) {
      System.out.println(s);
    }
  }
}

Output

{Joe=England, Steve=Australia, Kane=New Zealand, Virat=India, Karunaratne=Sri Lanka}

Keys...(Player Name)
Joe
Steve
Kane
Virat
Karunaratne

Remove an item from a HashMap

Use the remove() method to remove an item from a HashMap. For the key/value pair, you want to remove, set only the key as a parameter of the remove():

import java.util.HashMap;

class Studyopedia {

  public static void main(String[] args) {

    // HashMap with key value pairs
    HashMap<String, String> player = new HashMap<String, String>();

    player.put("Virat", "India");
    player.put("Steve", "Australia");
    player.put("Joe", "England");
    player.put("Kane", "New Zealand");
    player.put("Karunaratne", "Sri Lanka");
    
    // Display the HashMap
    System.out.println(player); 
    
    // Remove an item
    player.remove("Kane");

    // Updated HashMap
    System.out.println("\nUpdated HashMap after removing an item =\n"+player); 
  }
}

Output

{Joe=England, Steve=Australia, Kane=New Zealand, Virat=India, Karunaratne=Sri Lanka}

Updated HashMap after removing an item =
{Joe=England, Steve=Australia, Virat=India, Karunaratne=Sri Lanka}

If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.


For Videos, Join Our YouTube Channel: Join Now


Java HashSet
Java Inheritance
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment