how to get a set starting from first element to the desired element and some basic operation on a TreeSet using collections in java
Previous | Home | Next |
In this example we shall see how to create a subset of elements starting from the starting element to the desired element and some other basic operations using collections.
For creating a TreeSet we have to import the java.util package in which we have the class java.util.TreeSet which contains the definition and various that can be performed on a TreeSet. In this example we have created a class named CollectionExample in which we have created a TreeSet and added some elements to it.
In this example we have used the following methods ,
SortedSet headSet(endingele)
This method returns set the portion of this set whose elements are strictly less than endingele.
boolean isEmpty()
This method returns true if this set contains no elements.
boolean remove(Object obj)
This method removes the the specified element from this set if present.
int size()
This method returns the number of elements in this set.
SortedSet<E> subSet(startingele, endingele)
This method returns a view of the portion of the set whose elements are in the range of startingele( inclusive), endingele(exclusive).
SortedSet<E> tailSet(startingele)
This method returns a set elements which are greater than or equal to startingele.
In this example we have created a TreeSet and added some items to it. Then we used various methods for creating the head set and tail sets.
package r4r.co.in;
import java.util.*;
public class CollectionExample {
/**
* @param args
*/
public static void main(String[] args) throws ClassCastException, NullPointerException, IllegalArgumentException
{
// TODO Auto-generated method stub
try
{
TreeSet<Integer> ts=new TreeSet<Integer>(); // creating a TreeSet object ts
ts.add(new Integer(2)); // adding object to the TreeSet
ts.add(4);
ts.add(7);
ts.add(8);
ts.add(1);
ts.add(9);
System.out.println("The objects of the tree set are as follows: ");
System.out.println(ts); // displaying the contents of the TreeSet
System.out.println("The headSet starting from first element is: "+ts.headSet(7)); // creating a sublist that starts from first element ie head to the specified element(exclusive)
System.out.println("The headSet starting from first element is: "+ts.headSet(7, true)); // creating a sublist that starts from first element ie head to the specified element(inclusive)
System.out.println("The tailset starting from from the element to last element is: "+ts.tailSet(7)); // creating a tailSet with the specified element as inclusive
System.out.println("The headSet starting from first elemtn is: "+ts.tailSet(7, false)); // creating a tailSet with the specified element as exclusive
System.out.println("The size of the TreeSet is :"+ts.size()); // displaying the size of the TreeSet
System.out.println("This method is to check whether a particular value exist in a TreeSet or not:"+ts.contains(7)); // checking whether the TreeSet contains 7 as an object
System.out.println("This method will retrieve and remove the first element in the treeSet: "+ts.pollFirst()); // retrieving and removing the first element of the TreeSet using pollFirst() method
System.out.println(ts); // Now displaying the content of the TreeSet
System.out.println("This method will return the greatest element in this treeSet, less than or equal to the given element: "+ts.floor(9)); // here we are using the floor function
System.out.println("This object is removed from the list: "+ts.remove(2)); // we are removing the object 2 from the TreeSet
System.out.println("This object is removed from the list: "+ts.remove(7)); // we are removing the object 7 from the TreeSet
System.out.println("Now the TreeSet contains the follwing objects: ");
Iterator<Integer> itr=ts.iterator(); // Invoking the iterator on the TreeSet
while(itr.hasNext())
{
System.out.println(itr.next()); // displaying the contents of the TresSet after removal operations
}
}
catch(ClassCastException e)
{
System.out.println(e.getMessage());
}
catch(NullPointerException e)
{
System.out.println(e.getLocalizedMessage());
}
catch(IllegalArgumentException e)
{
System.out.println(e.getMessage());
}
}
}
The output of the above program is the following one:
The objects of the tree set are as follows:
[1, 2, 4, 7, 8, 9]
The headSet starting from first element is: [1, 2, 4]
The headSet starting from first element is: [1, 2, 4, 7]
The tailset starting from from the element to last element is: [7, 8, 9]
The headSet starting from first elemtn is: [8, 9]
The size of the TreeSet is :6
This method is to check whether a particular value exist in a TreeSet or not:true
This method will retrieve and remove the first element in the treeSet: 1
[2, 4, 7, 8, 9]
This method will return the greatest element in this treeSet, less than or equal to the given element: 9
This object is removed from the list: true
This object is removed from the list: true
Now the TreeSet contains the follwing objects:
4
8
9
Previous | Home | Next |