Home » Java Collections emptySortedSet() Method with Examples

Java Collections emptySortedSet() Method with Examples

by Online Tutorials Library

Java Collections emptySortedSet() Method

The emptySortedSet() method of Java Collections class is used to get the sorted Set that has no elements. These empty Set are immutable in nature.

Syntax

Following is the declaration of emptySortedSet() method:

Parameter

This method does not accept any parameter.

Returns

The emptySortedSet() method returns an empty sorted Set.

Exceptions

NA

Compatibility Version

Java 1.8 and above

Example 1

Test it Now

Output:

Empty Sorted Set: []  

Example 2

Test it Now

Output:

Created empty immutable Sorted Set: []Exception in thread "main"   java.lang.UnsupportedOperationException  at java.base/java.util.Collections$UnmodifiableCollection.add(Collections.java:1056)  at myPackage.CollectionsEmptySortedSetExample2.main(CollectionsEmptySortedSetExample2.java:9)  

Example 3

Test it Now

Output:

Exception in thread "main" java.lang.UnsupportedOperationException  at java.base/java.util.Collections$UnmodifiableCollection.add(Collections.java:1056)  at myPackage.CollectionsEmptySortedSetExample3.main(CollectionsEmptySortedSetExample3.java:8)  

You may also like