Close

Java Collections - Collections.unmodifiableNavigableSet() Examples

Java Collections Java Java API 


Class:

java.util.Collections

java.lang.Objectjava.lang.Objectjava.util.Collectionsjava.util.CollectionsLogicBig

Method:

public static <T> NavigableSet<T> unmodifiableNavigableSet(NavigableSet<T> s)

Returns an unmodifiable view (read-only) of the specified NavigableSet. Attempts to modify the returned NavigableSet, whether direct or via its iterator or via subSet, headSet, or tailSet views , result in an UnsupportedOperationException.


Examples


Since the Set created by this method is a view of the original set, modifying the original will reflect the changes in it:

package com.logicbig.example.collections;

import java.util.*;

public class UnmodifiableNavigableSetExample {

public static void main(String... args) {
NavigableSet<Integer> set = new TreeSet<>();
Collections.addAll(set, 1, 4, 7);
System.out.println("Original Set: " + set);

NavigableSet<Integer> set2 = Collections.unmodifiableNavigableSet(set);
System.out.println("unmodifiableNavigableSet: " + set2);
//modifying the original
set.add(10);
System.out.println("unmodifiableNavigableSet: " + set2);
}
}

Output

Original Set: [1, 4, 7]
unmodifiableNavigableSet: [1, 4, 7]
unmodifiableNavigableSet: [1, 4, 7, 10]




Modifying itself will throw exception:

package com.logicbig.example.collections;

import java.util.Collections;
import java.util.NavigableSet;
import java.util.TreeSet;

public class UnmodifiableNavigableSetExample2 {

public static void main(String... args) {
NavigableSet<Integer> set = new TreeSet<>();
Collections.addAll(set, 1, 4, 7);
System.out.println("Original Set: " + set);

NavigableSet<Integer> set2 = Collections.unmodifiableNavigableSet(set);
set2.add(10);
}
}

Output

Caused by: java.lang.UnsupportedOperationException
at java.base/java.util.Collections$UnmodifiableCollection.add(Collections.java:1056)
at com.logicbig.example.collections.UnmodifiableNavigableSetExample2.main(UnmodifiableNavigableSetExample2.java:21)
... 6 more




package com.logicbig.example.collections;

import java.util.Collections;
import java.util.NavigableSet;
import java.util.SortedSet;
import java.util.TreeSet;

public class UnmodifiableNavigableSetExample3 {

public static void main(String... args) {
NavigableSet<Integer> set = new TreeSet<>();
Collections.addAll(set, 1, 3, 4, 7, 9, 11);
System.out.println("Original Set: " + set);
//headset of the original set
SortedSet<Integer> headSet = set.headSet(4);

NavigableSet<Integer> set2 = Collections.unmodifiableNavigableSet(set);
System.out.println("unmodifiableNavigableSet: " + set2);
//modifying headSet
headSet.add(2);
System.out.println("unmodifiableNavigableSet: " + set2);
System.out.println("Original Set: " + set);
}
}

Output

Original Set: [1, 3, 4, 7, 9, 11]
unmodifiableNavigableSet: [1, 3, 4, 7, 9, 11]
unmodifiableNavigableSet: [1, 2, 3, 4, 7, 9, 11]
Original Set: [1, 2, 3, 4, 7, 9, 11]




package com.logicbig.example.collections;

import java.util.Collections;
import java.util.NavigableSet;
import java.util.SortedSet;
import java.util.TreeSet;

public class UnmodifiableNavigableSetExample4 {

public static void main(String... args) {
NavigableSet<Integer> set = new TreeSet<>();
Collections.addAll(set, 1, 3, 4, 7, 9, 11);
System.out.println("Original Set: " + set);

NavigableSet<Integer> set2 = Collections.unmodifiableNavigableSet(set);
System.out.println("unmodifiableNavigableSet: " + set2);
//headset of the unmodifiableNavigableSet
SortedSet<Integer> headSet = set2.headSet(4);
headSet.add(2);
}
}

Output

Caused by: java.lang.UnsupportedOperationException
at java.base/java.util.Collections$UnmodifiableCollection.add(Collections.java:1056)
at com.logicbig.example.collections.UnmodifiableNavigableSetExample4.main(UnmodifiableNavigableSetExample4.java:24)
... 6 more




See Also