Basic Java program to Remove specified element from TreeSet example

1.Basic java example program to remove specified  element from treeset.

  1. package com.TreeSetExamplePrograms;

  2. public Class TreeSetRemoveDemo{ 
  3. import java.util.TreeSet;
  4.  
  5. public static void main(String args[]) {
  6. //create TreeSet object
  7.  
  8. TreeSet<Integer> treeSet = new TreeSet<Integer>();
  9.            
  10.    //add elements to TreeSet object
  11. treeSet.add(11);
  12. treeSet.add(12);
  13. treeSet.add(13);
  14.  
  15. System.out.println("TreeSet before removal : " + treeSet);
  16.            
  17.  /*
  18.  To remove an element from Java TreeSet object use,
  19.  boolean remove(Object o) method.
  20.  This method removes an element from TreeSet if it is present and returns
  21. true. Otherwise remove method returns false.
  22.  */
  23.            
  24.    boolean isRemoved = treeSet.remove(13);
  25.    System.out.println("Was 13 removed from TreeSet ? " + isRemoved);
  26.            
  27.   System.out.println("TreeSet elements after removal : " + treeSet);
  28.           
  29.  
  30. }
  31. }


Output:

  1. TreeSet before removal : [11, 12, 13]
  2. Was 13 removed from TreeSet ? true
  3. TreeSet elements after removal : [11, 12]


Remove all elements from Java TreeSet example:

1.Basic java example program to remove all elements from treeset.


  1. package com.TreeSetExamplePrograms;

  2. public Class TreeSetRemoveDemo{ 
  3. import java.util.TreeSet;
  4.  
  5. public static void main(String args[]) {
  6. //create TreeSet object
  7.  
  8. TreeSet<Integer> treeSet = new TreeSet<Integer>();
  9.            
  10.    //add elements to TreeSet object
  11. treeSet.add(11);
  12. treeSet.add(12);
  13. treeSet.add(13);
  14.  
  15. System.out.println("TreeSet before removal : " + treeSet);
  16.            
  17.  treeSet.clear();
  18. System.out.println("TreeSet after removal : " + treeSet);
  19.            
  20.  /*
  21.  To check whether TreeSet contains any elements or not use
  22.  boolean isEmpty() method.
  23.  isEmpty() method returns true if the TreeSet does not contains any elements
  24.  otherwise returns false.
  25.  */
  26.  
  27.  System.out.println("Is TreeSet empty ? " + treeSet.isEmpty());              
  28.  
  29. }
  30. }




Output:

  1. TreeSet before removal : [11, 12, 13]
  2. TreeSet after removal : []
  3. Is TreeSet empty ? true

Sort employee object in descending order using comparable and TreesSet

1.Basic Java example program sort student object descending order by id using treeset

  1. package com.TreeSetExamplePrograms;

  2. public Class Employee Implements Comparable<Employee>{ 

  3.     String name;
  4.     int id;
  5.     
  6.     public String getName() {
  7.         return name;
  8.     }
  9.  
  10.     public void setName(String name) {
  11.         this.name = name;
  12.     }
  13.  
  14.     public int getId() {
  15.         return id;
  16.     }
  17.  
  18.     public void setId(int id) {
  19.         this.id = id;
  20.     }
  21.  
  22.     Employee (String name, int id){
  23.         this.name=name;
  24.         this.id=id;
  25.     }
  26.  
  27.     @Override    public int compareTo(Employee obj) {
  28.         if (this.getId() == obj.getId()) return 0;
  29.         else
  30.         
  31.         if (this.getId() < obj.getId()) return 1;
  32.         else return -1;
  33.             
  34. }
  35.  
  36. }



  1. package com.TreeSetExamplePrograms;

  2. public Class Test{ 
  3. public static void main(String args[]) {
  4. //create TreeSet object
  5.  
  6. TreeSet<Employee> treeSet = new TreeSet<Employee>();     
  7.      
  8.        //add elements to TreeSet
  9.     treeSet.add(new Employee("abc",1));
  10.     treeSet.add(new Employee("xyz",2));
  11.     treeSet.add(new Employee("ssd",3));
  12.     treeSet.add(new Employee("ert",4));
  13.          
  14.     Iterator itr = treeSet.iterator();
  15.  
  16.  while(itr.hasNext()){
  17.  
  18.      Employee obj= (Employee)itr.next();
  19.      System.out.println(obj.getName()+"  "+obj.getId()); 

  20.   }         

  21. }
  22. }



Output:

  1. ert  4
  2. ssd  3
  3. xyz  2
  4. abc  1 

Get lowest and highest value stored in Java TreeSet example

1.Basic Java example program get lowest and highest values from treeset

  1. package com.TreeSetExamplePrograms;

  2. public Class TreeSetGetLowestAndHighestDemo{ 
  3.  
  4. public static void main(String args[]) {
  5. //create TreeSet object
  6.  
  7.  
  8.  TreeSet<Integer> treeSet = new TreeSet<Integer>();
  9.        
  10.  //add elements to TreeSet
  11.   treeSet.add(1);
  12.   treeSet.add(3);
  13.   treeSet.add(2);
  14.   treeSet.add(5);
  15.   treeSet.add(6);
  16.   treeSet.add(7);
  17.   treeSet.add(8);
  18.   treeSet.add(9);
  19.   treeSet.add(10);
  20.           
  21.  System.out.println(treeSet);
  22.           
  23.  System.out.println("First element in treeset : " + treeSet.first());           
  24.    
  25.  System.out.println("Last element in treeste" + treeSet.last()); 


  26. }
  27. }


Output:


  1. [1, 2, 3, 4, 5, 6, 7, 8, 9,10]
  2. First element in treeset :1
  3. Last element in treeset :10


2.Basic Java example program print elements of treeset using iterator


  1. package com.TreeSetExamplePrograms;

  2. public Class TreeSetDemo{ 
  3.  
  4. public static void main(String args[]) {
  5. //create TreeSet object
  6.  
  7.  TreeSet<String> treeSet = new TreeSet<String>();
  8.  
  9.    //add elements to TreeSet
  10.     treeSet.add("I");
  11.     treeSet.add("N");
  12.     treeSet.add("S");
  13.     treeSet.add("T");
  14.     treeSet.add("A");
  15.     treeSet.add("N");
  16.     treeSet.add("C");
  17.     treeSet.add("E");
  18.           
  19.     treeSet.add("O");
  20.     treeSet.add("F");
  21.   
  22. Iterator itr = treeSet.iterator();
  23. while(itr.hasNext()){
  24.              System.out.println(itr.next());
  25. }

  26. }
  27. }




Output:

  1. A
  2. C
  3. E
  4. F
  5. I
  6. N
  7. O
  8. S
  9. T

Get Size of Java TreeSet Example

1.Basic Java example program get size of treeset


  1. package com.TreeSetExamplePrograms;

  2. public Class TreeSetGetSizeDemo{ 
  3.  
  4. public static void main(String args[]) {
  5. //create TreeSet object
  6.  
  7.  TreeSet<String> treeSet = new TreeSet<String>();
  8.  
  9.    //add elements to TreeSet
  10.     treeSet.add("I");
  11.     treeSet.add("N");
  12.     treeSet.add("S");
  13.     treeSet.add("T");
  14.     treeSet.add("A");
  15.     treeSet.add("N");
  16.     treeSet.add("C");
  17.     treeSet.add("E");
  18.           
  19.     treeSet.add("O");
  20.     treeSet.add("F");
  21.           
  22.     System.out.println(treeSet);
  23.           
  24.    System.out.println("Size of TreeSet  : " + treeSet.size());           
  25.     //remove one element from TreeSet using remove method
  26.  
  27.      treeSet.remove("A");
  28.  
  29.  System.out.println("Size of TreeSet after removal of element A : " + treeSet.size()); 
  30.  System.out.println(treeSet);

  31. }
  32. }



Output:


  1. [A, C, E, F, I, N, O, S, T]
  2. Size of TreeSet  : 9
  3. Size of TreeSet after removal of element A : 8
  4. [C, E, F, I, N, O, S, T]

Java Program to convert Treesrt to an interger array

1.Basic Java program to copy all elements of treeset to an array


  1. package com.TreeSetExamplePrograms;

  2. public Class TreeSetDemo{ 
  3.  
  4. public static void main(String args[]) {
  5. //create TreeSet object
  6.  TreeSet<Integer> treeSet = new TreeSet<Integer>();
  7.        
  8.  //add elements to TreeSet
  9.   treeSet.add(1);
  10.   treeSet.add(3);
  11.   treeSet.add(2);
  12.   treeSet.add(5);
  13.   treeSet.add(6);
  14.   treeSet.add(7);
  15.   treeSet.add(8);
  16.   treeSet.add(9);
  17.   treeSet.add(10);
  18.         
  19.    Object[] objArray = treeSet.toArray();
  20.      //display contents of Object array
  21.  
  22.    System.out.println("TreeSet elements are copied into an Array. Now Array Contains..");
  23.  
  24.   for(int index=0; index < objArray.length ; index++)
  25.       System.out.println(objArray[index]);
  26.          
  27. }
  28. }


Output:


  1. TreeSet elements are copied into an Array. Now Array Contains..
  2. 1
  3. 2
  4. 3
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10

2.Java example program to Convert treeset to integer array

Copy all elements of Java TreeSet to an Object Array

Select Menu