Please disable adblock to view this page.

← Go home

Unordered Data using HashMap in Java

java-display

May 1, 2017
Published By : Pratik Kataria
Categorised in:

Understanding through Code

import java.util.HashMap;
import java.util.Map;

public class HashMapDemo {
	public static void main( String[] args ){
		Map<String, String> map = new HashMap<>();

		map.put("India", "First");
		map.put("US", "Second");
		map.put("Canada", "Third");
		System.out.println(map);

		map.put("Russia", "Fourth");
		System.out.println(map);

		String mapString = map.get("US");
		System.out.println("The US stands at: " +mapString);

		System.out.println(mapString);
		map.remove("Russia");
		
	}
}

Output

  • {Canada=Third, US=Second, India=First}
  • {Canada=Third, US=Second, India=First, Russia=Fourth}
  • The US stands at: Second
  • Second

Notes

  • HashMap is unordered data collection.
  • HashMap is implementation of Map. Just like ArrayList is an implementation of List.
  • Maps are in Key, Value pairs.
  • You can look up an item in HashMap with the help of Key.

Pratik Kataria is a budding programmer, web designer and developer.