Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
Java Program to insert a value to a SortedSet
Let’s say we have the following SortedSet −
SortedSet<String> set = new TreeSet<String>();
set.add("T");
set.add("R");
set.add("S");
set.add("Q");
set.add("V");
set.add("U");
set.add("W");
Now, after displaying the above elements with Iterator, you can insert values like this to a SortedSet
set.add("Z");
set.add("Y");
Example
import java.util.Iterator;
import java.util.SortedSet;
import java.util.TreeSet;
public class Demo {
public static void main(String[] argv) throws Exception {
SortedSet<String> set = new TreeSet<String>();
set.add("T");
set.add("R");
set.add("S");
set.add("Q");
set.add("V");
set.add("U");
set.add("W");
Iterator<String> i = set.iterator();
System.out.println("SortedSet elements...");
while (i.hasNext()) {
Object ob = i.next();
System.out.println(ob.toString());
}
set.add("Z");
set.add("Y");
i = set.iterator();
System.out.println("Updated SortedSet elements...");
while (i.hasNext()) {
Object ob = i.next();
System.out.println(ob.toString());
}
}
}
Output
SortedSet elements... Q R S T U V W Updated SortedSet elements... Q R S T U V W Y Z
Advertisements
