Java Collections unmodifiableNavigableSet() Method with Examples
In this article we will discuss about unmodifiableNavigableSet() method.
Introduction
This method is available in NavigableSet. It is an data structure that can store elements in an order. For this we have to use tree set.
we can create a treeset by using the following syntax:
NavigableSet<datatype> data = new TreeSet<String>();
where,
- datatype specifies the type of elements
- data is the input data.
unmodifiableNavigableSet()
This method will return the unmodifiable view of the given Navigable set.
Syntax:
public static <T> NavigableSet<T> unmodifiableSortedSet(SortedSet<T> data)
where, data is the Navigable set which is returned in an unmodifiable view.
Example1 :
- demonstration before and after modification
Java
import java.util.*; public class GFG1 { // main method public static void main(String[] args) { // create a set named data NavigableSet<Integer> data = new TreeSet<Integer>(); // Add values in the data data.add( 1 ); data.add( 2 ); data.add( 3 ); data.add( 34 ); // Create a Unmodifiable sorted set SortedSet<Integer> data2 = Collections.unmodifiableNavigableSet(data); // display System.out.println(data); // add to data data.add( 32 ); // display System.out.println(data2); } } |
Output:
[1, 2, 3, 34] [1, 2, 3, 32, 34]
Example 2
Java
import java.util.*; public class GFG1 { // main method public static void main(String[] args) { // create a set named data NavigableSet<String> data = new TreeSet<String>(); // Add values in the data data.add( "java" ); data.add( "Python" ); data.add( "R" ); data.add( "sql" ); // Create a Unmodifiable sorted set SortedSet<String> data2 = Collections.unmodifiableNavigableSet(data); // display System.out.println(data); // add to data data.add( "bigdata/iot" ); // display System.out.println(data2); } } |
Output:
[Python, R, java, sql] [Python, R, bigdata/iot, java, sql]
Please Login to comment...