Pages

SortedSet in java

What is SortedSet in java?
SortedSet(I):: 
  • It is the child interface of the set
  • If we want to represent a group of individual objects according to some sorting order with out duplicates then we should go for sortedset.
Demo program on SortedSet:
import java.util.SortedSet;
import java.util.TreeSet;
public class setDemo {
    public static void main(String[] args) {
        SortedSet hs = new TreeSet();
        hs.add(1);
        hs.add(0);
        hs.add(5);
        hs.add(3);
        System.out.println(hs.add(3));
        System.out.println(hs);
    }
}
Output:

SortedSet in java
SortedSet in java

Demo program using sorted set interface defines specific methods::
import java.util.SortedSet;
import java.util.TreeSet;
public class setDemo {
    public static void main(String[] args) {
        SortedSet s = new TreeSet();
        s.add(100);
        s.add(120);
        s.add(150);
        s.add(140);
        s.add(170);
        System.out.println(s);
        System.out.println("using first() method: " + s.first());
        System.out.println("using last() method: " + s.last());
        System.out.println("using headSet() method: " + s.headSet(150));
        System.out.println("using tailSet() method: " + s.tailSet(150));
        System.out.println("using subSet() method: " + s.subSet(120, 150));
        System.out.println("using comparator() method: " + s.comparator());
    }
}
Output:
TreeSet program in java
TreeSet program in java

Please comment below to feedback or ask questions.

No comments:

Post a Comment

Please comment below to feedback or ask questions.