Curriculum
Course: Java Basic
Login

Curriculum

Java Basic

Java Home

0/1

Java Introduction

0/1

Java Get Started

0/1

Java Syntax

0/1

Java Comments

0/1

Java Type Casting

0/1

Java Operators

0/1

Java Booleans

0/1

Java Switch

0/1

Java Break / Continue

0/1

Java Errors and Exception

0/1
Text lesson

Java Hash Map

Java HashMap

In the ArrayList section, you discovered that Arrays organize elements in a specific order, accessed through integer indices. In contrast, HashMaps store elements as pairs of keys and values, enabling access using keys of various types (such as strings) rather than just integers.

An object serves as a key to retrieve another object, functioning as its value. HashMaps can accommodate various types, such as pairs of String keys and Integer values, or uniform types like String keys and String values.

Example

Instantiate a HashMap named capitalCities to store pairs of 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:

Example

// 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("England", "London");
   capitalCities.put("Germany", "Berlin");
   capitalCities.put("Norway", "Oslo");
   capitalCities.put("USA", "Washington DC");
   System.out.println(capitalCities);
}
}

Access an Item

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

Example

capitalCities.get("England");

Remove an Item

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

Example

capitalCities.remove("England");

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

Example

capitalCities.clear();

HashMap Size

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

Example

capitalCities.size();

Loop Through a HashMap

Iterate through the elements of a HashMap using a for-each loop.

Reminder: Utilize the keySet() method to obtain only the keys, and employ the values() method if you solely require the values.

Example

// Print keys
for (String i : capitalCities.keySet()) {
  System.out.println(i);
}

Example

// Print values
for (String i : capitalCities.values()) {
  System.out.println(i);
}

Example

// Print keys and values
for (String i : capitalCities.keySet()) {
  System.out.println("key: " + i + " value: " + capitalCities.get(i));
}

Other Types

Keys and values within a HashMap are object instances. In the preceding examples, we utilized objects of the “String” type. It’s important to note that in Java, a String is an object, not a primitive type. When using other types like int, you should specify a corresponding wrapper class, such as Integer. For other primitive types, utilize Boolean for boolean, Character for char, Double for double, etc.

Example

Instantiate a HashMap named people to store pairs of String keys and Integer values.

// 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));

    }

  }

}