GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||
GeezerVison is showing www.tutorialspoint.com — Java - SortedSet Interface
Selected Reading
Java - SortedSet InterfaceThe SortedSet interface extends Set and declares the behavior of a set sorted in an ascending order. In addition to those methods defined by Set, the SortedSet interface declares the methods summarized in the following table − Several methods throw a NoSuchElementException when no items are contained in the invoking set. A ClassCastException is thrown when an object is incompatible with the elements in a set. A NullPointerException is thrown if an attempt is made to use a null object and null is not allowed in the set. SortedSet Interface Methods
Operations on SortedSet InterfaceCreating a SortedSetTreeSet class implements the SortedSet interface. We can use the TreeSet constructor to create a SortedSet instance. Following is the syntax to create a SortedSet instance: Syntax// Create the sorted set SortedSet<String> set = new TreeSet<>(); Here we're creating a sorted set of String values. This map will store the unique string values. If a duplicate value is added, then that will be discarded. Adding Value to a SortedSetSortedSet provides an add() method, which can be used to add value to a SortedSet instance. Whenever a value is added to the set, it is checked against the existing values. If the set is modified then method will return true otherwise false will be returned. Syntaxpublic boolean add(E e) Where E represents the element to be added. If element is already present, then no action will be performed and method will return false. Example// Add elements to the set
set.add("b");
set.add("c");
set.add("a");
Getting value from a SortedSetIn order to get values from a SortedSet, we've to get the iterator object from the SortedSet using the iterator() method. Once the iterator object is available then that object can be used to retrieve values present in the SortedSet. Example// Iterating over the elements in the set
Iterator it = set.iterator();
while (it.hasNext()) {
// Get element
Object element = it.next();
System.out.println(element.toString());
}
Deleting a value from a SortedSetUsing the remove(value) method, we can remove the value/object stored in the SortedSet easily. Syntaxpublic boolean remove(Object value) if value is not present in the set, then it will return false otherwise it will remove the value and return true. set.remove("a");
Iterating SortedSetSortedSet entries can be easily navigated. SortedSet provides a method iterator() which provides an iterator to navigate all the entries of the set. Syntaxpublic Iterator<E> iterator() Where E is the type of objects to be iterated. Example// Iterating over the elements in the set
Iterator it = set.iterator();
while (it.hasNext()) {
// Get element
Object element = it.next();
System.out.println(element.toString());
}
Examples of SortedSet InterfaceAdding Element to a SortedSet ExampleSortedSet have its implementation in various classes like TreeSet. Following is an example of a TreeSet class with add operation− import java.util.Iterator;
import java.util.SortedSet;
import java.util.TreeSet;
public class SortedSetDemo {
public static void main(String[] args) {
// Create the sorted set
SortedSet<String> set = new TreeSet<>();
// Add elements to the set
set.add("b");
set.add("c");
set.add("a");
// Iterating over the elements in the set
Iterator it = set.iterator();
while (it.hasNext()) {
// Get element
Object element = it.next();
System.out.println(element.toString());
}
}
}
Outputa b c Removing Element from a SortedSet ExampleSortedSet have its implementation in various classes like TreeSet. Following is an example of a TreeSet class with add and remove operation − import java.util.Iterator;
import java.util.SortedSet;
import java.util.TreeSet;
public class SortedSetDemo {
public static void main(String[] args) {
// Create the sorted set
SortedSet<String> set = new TreeSet<>();
// Add elements to the set
set.add("b");
set.add("c");
set.add("a");
set.add("d");
set.add("e");
set.add("f");
// remove elements
set.remove("c");
set.remove("f");
// Iterating over the elements in the set
Iterator it = set.iterator();
while (it.hasNext()) {
// Get element
Object element = it.next();
System.out.println(element.toString());
}
}
}
Outputa b d e Clearing a SortedSet ExampleSortedSet have its implementation in various classes like TreeSet. Following is an example of a TreeSet class with add and clear operation − import java.util.Iterator;
import java.util.SortedSet;
import java.util.TreeSet;
public class SortedSetDemo {
public static void main(String[] args) {
// Create the sorted set
SortedSet<String> set = new TreeSet<>();
// Add elements to the set
set.add("b");
set.add("c");
set.add("a");
set.add("d");
set.add("e");
set.add("f");
System.out.println(set);
// remove elements
set.clear();
System.out.println(set);
}
}
Output[a, b, c, d, e, f] [] Advantages of SortedSet Interface
Disadvantages of SortedSet Interface
Advertisements
| ||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |