Home » Java Collections emptyMap() Method with Examples

Java Collections emptyMap() Method with Examples

by Online Tutorials Library

Java Collections emptyMap() Method

The emptyMap() method of Java Collections class returns an empty map which is immutable.

Syntax

Following is the declaration of emptyMap() method:

Parameter

This method does not accept any parameter.

Returns

The emptyMap() method returns an empty immutable Map.

Exceptions

NA

Compatibility Version

Java 1.5 and above

Example 1

Test it Now

Output:

Created Empty Map: {}  

Example 2

Test it Now

Output:

Created Empty Map: {}  Exception in thread "main" java.lang.UnsupportedOperationException  at java.base/java.util.AbstractMap.put(AbstractMap.java:209)  at myPackage.CollectionsEmptyMapExample2.main(CollectionsEmptyMapExample2.java:9)  

Example 3

Test it Now

Output:

Exception in thread "main" java.lang.UnsupportedOperationException  at java.base/java.util.AbstractMap.put(AbstractMap.java:209)  at myPackage.CollectionsEmptyMapExample3.main(CollectionsEmptyMapExample3.java:7)  

You may also like