wellnwill Marathahalli Bangalore

SortedSet

  • Duplicates are not allowed.
  • Elements will be arrange in some sorting order.

Commonly used methods


SortedSet headSet(Object ob):


This method returns the elements smaller than specified object.

SortedSet tailSet(Object ob):

This method returns the elements greater than or equal to specified object.

SortedSet subSet(Object ob):

This method returns the elements greater than or equal to first specified object and less than second specified object.

Object first():

this method returns the first element of SortedSet.

Object last():

this method returns the last element of SortedSet.

Comparator comparator()

  • Number: The default sorting order is ascending.
  • String: The default sorting order is alphabetical.

TreeSet

  • Duplicate objects are not allowed.
  • Insertion order is not preserved because data will be inserted based on sorting order (either default sorting order or user defined sorting order).
  • Heterogeneous data is not allowed because it is not possible to compare data for dissimilar type. Hence sorting of data is impossible.
  • TreeSet implements Clonnable and Serilizable interface but it does not implement RandomAccess interface. That is why, accessing the element randomly is not possible.
  • Null value is not allowed from java 1.7 version onwards. If we try to add null value then we get NullPointerException at execution time.
  • Comparable interface must be implemented by the class, if we have to add the object of this class into TreeSet.
  • Internally compareTo() method is used to check object uniqueness i.e comparision of objects for sorting purpose.

  • Constructor


    TreeSet t=new TreeSet():

    The default constructor is used to create an empty TreeSet object and elements will be inserted with default natural sorting order.

    TreeSet t=new TreeSet(Comparator c):

    The constructor with Comparator type argument is used to create an empty TreeSet object and elements will be inserted with user defined sorting order.

    import java.util.*;
    class Test251{
    public static void main(String[] args){
    TreeSet ts=new TreeSet();
    ts.add(3);
    ts.add(2);
    ts.add(4);
    ts.add(5);
    ts.add(1);
    System.out.println(ts);//[1,2,3,4,5]
    SortedSet st1=ts.headSet(4);
    System.out.println(st1);//[1,2,3]
    SortedSet st2=ts.tailSet(3);
    System.out.println(st2);//[3,4,5]
    SortedSet st3=ts.subSet(1,3);
    System.out.println(st3);//[1,2]
    }
    }
    
    import java.util.*;
    class Test253
    {
    public static void main(String[] args)
    {
    TreeSet ts=new TreeSet();
    ts.add(null);
    }
    }
    //O/P-Runtime Exception:NullPointerException.
    
    
    
    
    
    
    
    
    import java.util.*;
    class Test254
    {
    public static void main(String[] args){
    TreeSet ts=new TreeSet();
    ts.add(new StringBuilder("a"));
    ts.add(new StringBuilder("b"));
    ts.add(new StringBuilder("c"));
    }
    }
    //O/P-Runtime Exception:Classcast Exception
    Note: StringBuilder class does not implement 
    java.lang.Comparable interface.
    By default, while adding elements into TreeSet, 
    compareTo() is called for default sorting.
    
     
    
    import java.util.*;
    class Test251{
    public static void main(String[] args){
    TreeSet ts=new TreeSet();
    ts.add(3);
    ts.add(2);
    ts.add(4);
    ts.add(5);
    ts.add(1);
    System.out.println(ts);//[1,2,3,4,5]
    SortedSet st1=ts.headSet(4);
    System.out.println(st1);//[1,2,3]
    SortedSet st2=ts.tailSet(3);
    System.out.println(st2);//[3,4,5]
    SortedSet st3=ts.subSet(1,3);
    System.out.println(st3);//[1,2]
    }
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    import java.util.*;
    class Test285{
    public static void main(String[] args){
    TreeSet ts=new TreeSet(new DescComparator());
    ts.add(3);
    ts.add(2);
    ts.add(4);
    ts.add(5);
    ts.add(1);
    System.out.println(ts);
    }
    }
    class DescComparator implements Comparator
    {
     public int compare(Object ob1,Object ob2) {
    	 int x=(Integer)ob1;
                  int y=(Integer)ob2;	 
          if(x>y)
    	  return -1;
    	  else if(y>x)
    	  return 1;
    	  else
    	  return 0;
    	  }
    }
    //O/p-5,4,3,2,1
    
    
    
     
    
    import java.util.*;
    class Employee implements Comparable
    {
    int id;
    String name;
    Employee(int id,String name)
    {
    this.id=id;
    this.name=name;
    }
    public String toString()
    {
    	return id+"  "+name;
    }
    
    public int compareTo(Object o)
    //comparision based on id
     {
      Employee e=(Employee)o;
      if(this.id==e.id)  
         return 0;  
      else if(this.id>e.id)  
         return 1;  
      else  
         return -1;
     }
    }
    class Test0254
    {
    public static void main(String[] args){
    TreeSet ts=new TreeSet();
    ts.add(new Employee(20,"David"));
    ts.add(new Employee(19,"John"));
    ts.add(new Employee(30,"Cena"));
    System.out.println(ts);
    }
    }
    
    
    
    
    
    import java.util.*;
    class Employee
    {
    int id;
    String name;
    
    Employee(int id,String name)
    {
    this.id=id;
    this.name=name;
    }
    public String toString()
    {
    	return id+"  "+name;
    }
    }
    
    class DescComparator implements Comparator
    {
     public int compare(Object ob1,Object ob2) {
    	 Employee x=(Employee)ob1;
                  Employee y=(Employee)ob2;	 
          
    	  if(x.id > y.id)
    	  return -1;
    	  else if(x.id < y.id)
    	  return 1;
    	  else
    	  return 0;
    	  }
    }
    class Test0255
    {
    public static void main(String[] args){
    TreeSet ts1=new TreeSet(new DescComparator());
    ts1.add(new Employee(91,"David"));
    ts1.add(new Employee(20,"John"));
    ts1.add(new Employee(30,"Cena"));
    System.out.println(ts1);
    }
    }
    
Footer with Map

Top