# Maps in Java

In 
Published 2022-12-03

This tutorial explains to you when to use and how to use Maps in Java. Maps objects are used for storing key/value pairs. The keys must be unique.

# Maps in Java: HashMap

The properties of HashMap objects are:

  • are not synchronized
  • the insertion order is not kept

# Maps in Java: HashMap

The properties of LinkedHashMap objects are:

  • are not synchronized (for a SYNCHRONIZED object use ConcurrentHashMap instead)
  • the insertion order is kept
  • more memory used than a HashMap object

# Maps in Java: EnumMap

The properties of EnumMap objects are:

  • the key are from an ENUM
  • are not synchronized

# Maps in Java: TreeMap

The properties of TreeMap objects are:

  • are not synchronized
  • are sorted upon a rule

# Maps in Java: HashTable

The properties of HashTable objects are:

  • are synchronized
  • no null allowed for both key/value

# Maps in Java - examples

Here it is an example for HashMap :

import java.util.HashMap;

public class MainApp {

    public static void main(String [] args) {

        // Create a HashMap object called people
        HashMap<String, Integer> people = new HashMap<>();

        //Add key/value pairs in the HashMap object
        people.put("Kevin", 23);
        people.put("Ann", 56);
        people.put("Paul", 43);
        people.put("Marian", 56);

        // go through the keys of the map
        for (String k : people.keySet()) {
            System.out.println("key: " + k + " value: " + people.get(k));
        }

        // go through the values of the map
        for (Integer val : people.values()) {
            System.out.println("val="+ val);
        }

        //Get the size of the map
        System.out.println("size="+ people.size());

        people.remove("Marian");

        System.out.println("size without Marian="+ people.size());

        System.out.println(people);

        //Get the value of a specific key
        System.out.println("Age Paul= "+people.get("Paul"));

        //Remove all the elements from a Map
        people.clear();

        System.out.println(people);

    }
}

Here it is an example for EnumMap :

import java.util.EnumMap;

public class MainApp {
public enum SomePeople {
KEVIN, ANN, PAUL, MARIAN
}

    public static void main(String [] args) {

        EnumMap<SomePeople, Integer> peopleMap = new EnumMap<>(SomePeople.class);
        peopleMap.put(SomePeople.KEVIN, 23);
        peopleMap.put(SomePeople.ANN, 45);
        peopleMap.put(SomePeople.PAUL, 44);
        peopleMap.put(SomePeople.MARIAN, 34);
        
        // go through the keys of the map
        for (SomePeople k : peopleMap.keySet()) {
            System.out.println("key: " + k + " value: " + peopleMap.get(k));
        }

        System.out.println(peopleMap);
    }
}

Here it is another example for TreeMap :

import java.util.Collections;
import java.util.SortedMap;
import java.util.TreeMap;

public class MainApp {

    public static void main(String [] args) {

        SortedMap<String, Integer> peopleSortedMap = new TreeMap<>(Collections.reverseOrder());

        //Add key/value pairs in the HashMap object
        peopleSortedMap.put("Kevin", 23);
        peopleSortedMap.put("Ann", 56);
        peopleSortedMap.put("Paul", 43);
        peopleSortedMap.put("Marian", 56);

        System.out.println(peopleSortedMap);
    }
}