Saturday, November 22, 2025
HomeLanguagesJavaJava Collections emptyMap() Method with Examples

Java Collections emptyMap() Method with Examples

The emptyMap() method of Java Collections is a method that is used to return an empty map such that we can not change the data in map IE it is immutable.

Syntax:

public static final <Key,Value> Map<Key,Value> emptyMap()   

where, 

  • key is the key element
  • value is the value element

Parameters: This will not accept any parameters,

Return Type: This will return an empty map that is immutable.

Exceptions: It will not arise any exception.

Example 1:

Java




// Java program to create an empty map
import java.util.*;
  
public class GFG {
    // main method  
    public static void main(String[] args)
    {      
        // create an empty map
        Map<String, String> data = Collections.emptyMap();
             
        System.out.println(data);
    }
}


Output

{}

Example 2: 

Java




// Java program to create an
// empty map and add elements
// We will get an error because 
// the method will work on only 
// an empty map
import java.util.*;
  
public class GFG { 
    // main method
    public static void main(String[] args)
    {      
        // create an empty map
        Map<String, String> data = Collections.emptyMap();
              
        // add element
        data.put("1", "python/R");
            
        System.out.println(data);
    }
}


Output:

Exception in thread "main" java.lang.UnsupportedOperationException
    at java.util.AbstractMap.put(AbstractMap.java:209)
    at GFG.main(GFG.java:8)
RELATED ARTICLES

Most Popular

Dominic
32407 POSTS0 COMMENTS
Milvus
97 POSTS0 COMMENTS
Nango Kala
6785 POSTS0 COMMENTS
Nicole Veronica
11932 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12000 POSTS0 COMMENTS
Shaida Kate Naidoo
6907 POSTS0 COMMENTS
Ted Musemwa
7168 POSTS0 COMMENTS
Thapelo Manthata
6864 POSTS0 COMMENTS
Umr Jansen
6852 POSTS0 COMMENTS