Home » Java Collections unmodifiableSortedSet() Method with Examples

Java Collections unmodifiableSortedSet() Method with Examples

by Online Tutorials Library

Java Collections unmodifiableSortedSet() Method

The unmodifiableSortedSet() method of Java Collections class is used to get an unmodifiable view of the specified sorted set.

Syntax

Following is the declaration of unmodifiableSortedSet() method:

Parameter

Parameter Description Required/Optional
s It is the sorted set for which an unmodifiable view is to be returned. Required

Returns

The unmodifiableSortedSet() method returns an unmodifiable view of the specified Sorted Set.

Exceptions

NA

Example 1

Test it Now

Output:

Unmodifiable Sorted set is :[Facebook, Instagram, Twitter, Whatsapp]  Unmodifiable Sorted set after modify is:[Facebook, Google, Instagram, Twitter, Whatsapp]  

Example 2

Test it Now

Output:

Original Set: [1, 7, 9]  Unmodifiable Sorted Set: [1, 7, 9]  Unmodifiable Sorted Set: [1, 7, 9, 10]  

Example 3

Test it Now

Output:

Original Set: [1, 4, 7]  Exception in thread "main" java.lang.UnsupportedOperationException  at java.base/java.util.Collections$UnmodifiableCollection.add(Collections.java:1056)  at myPackage.CollectionsUnmodifiableSortedSetExample3.main(CollectionsUnmodifiableSortedSetExample3.java:9)  

You may also like