Java TreeSet Class
Java TreeSet is a class in java collection framework. TreeSet class extends AbstractSet class and implements NavigableSet interface. Java TreeSet class also implements Cloneable and Serializable interfaces.
Java TreeSet class provides implementation for Set interface in java and it is uses tree for storage.
TreeSet class maintains the elements in ascending and sorted order.
In this above diagram, TreeSet class implements NavigableSet interface and NavigableSet interface extends SortedSet interface and SortedSet inteface extends Set interface and Set extends Collection extends Iterable interface.
6) Object clone()
Java TreeSet class provides implementation for Set interface in java and it is uses tree for storage.
TreeSet class maintains the elements in ascending and sorted order.
Some Important Points About TreeSet Class
- TreeSet class contains unique elements, In other words it doesn't contains duplicate elements.
- TreeSet class maintains ascending order of an elements in a set.
- Treeset class are good choice for storing large amounts of sorted information because its provides fast access and retrieval time.
Hierarchy of TreeSet Class
In this above diagram, TreeSet class implements NavigableSet interface and NavigableSet interface extends SortedSet interface and SortedSet inteface extends Set interface and Set extends Collection extends Iterable interface.
Constructors of Java TreeSet Class
There are some constructors of TreeSet class , these are
1) TreeSet()
It is used to constructs a new and empty tree set that will be sorted in and ascending order according to the natural ordering of its elements.
2) TreeSet(Collection c)
It is used to constructs a new tree set containing the elements in the specified collection.
3) TreeSet(Comparator comp)
It is used to constructs a new and empty tree set that will be sorted according to the specified or given comparator.
4) TreeSet(SortedSet)
It is used to constructs a new tree set containing the same elements and using the same ordering as the specified or given sortedset.
Methods of TreeSet Class
There are some useful methods of tree set class.
1) boolean addAll(Collection c)
This method is used to adds all of the elements in the specified collection to this set.
2) void clear()
This method is used to remove all of the elements from this set.
3) boolean contains(Object o)
This method return true if this set contains the given or specified elements.
4) boolean isEmpty()
If there is no elements in this set , this return true.
5) boolean add(E e)
This method adds the specified elements to this set if it is not already present.
6) Object clone()
This method returns a shallow copy of this tree set instance.
7) int size()
This method returns the number of elements in this set.
8) Iterator iterator()
This method returns an iterator over the elements in this set in ascending order.
9) Iterator descendingIterator()
This method returns an iterator over the elements in this set in descending order.
10) Object first()
This method returns the first(lowest) elements currently in this sorted set.
11) Object last()
This method returns the last(highest) elements currently in this sorted set.
12) Spliterator spliterator()
This method creates a late-binding and fail-fast Spliterator over the element in this set.
8) Iterator iterator()
This method returns an iterator over the elements in this set in ascending order.
9) Iterator descendingIterator()
This method returns an iterator over the elements in this set in descending order.
10) Object first()
This method returns the first(lowest) elements currently in this sorted set.
11) Object last()
This method returns the last(highest) elements currently in this sorted set.
12) Spliterator spliterator()
This method creates a late-binding and fail-fast Spliterator over the element in this set.
Java TreeSet Example
This is simple example of tree set.
import java.util.*;
class TreeSetExample
{
public static void main(String args[])
{
//creating TreeSet of String type
TreeSet<String> ts = new TreeSet<String>();
ts.add("mango");
ts.add("mango");//will not print because it is duplicate element
ts.add("banana");
ts.add("orange");
ts.add("apple");
//traversing elements
Iterator<String> itr = ts.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}
}
output : apple
banana
mango
orange
In this above example, There is no duplicate elements in tree set and the elements are displayed in ascending order.
This is another example of tree set , In this example we will take String and Integer type of elements and then print these elements.
import java.util.*;
class TreeSetExample2
{
public static void main(String args[])
{
//creating TreeSet of String type
TreeSet<String> ts = new TreeSet<String>();
ts.add("baman");
ts.add("daram");
ts.add("suraj");
ts.add("karan");
ts.add("ram");
ts.add("aman");//will print first because start with 'a' i.e aman
System.out.println(ts);
//creating TreeSet of Integer type
TreeSet<Integer> ts1 = new TreeSet<Integer>();
ts1.add(100);
ts1.add(99);
ts1.add(2);
ts1.add(1);
System.out.println(ts1);
}
}
output : [aman, baman, daram, karan, ram, suraj]
[1, 2, 99, 100]
Java TreeSet Example 2
This is another example of tree set , In this example we will take String and Integer type of elements and then print these elements.
import java.util.*;
class TreeSetExample2
{
public static void main(String args[])
{
//creating TreeSet of String type
TreeSet<String> ts = new TreeSet<String>();
ts.add("baman");
ts.add("daram");
ts.add("suraj");
ts.add("karan");
ts.add("ram");
ts.add("aman");//will print first because start with 'a' i.e aman
System.out.println(ts);
//creating TreeSet of Integer type
TreeSet<Integer> ts1 = new TreeSet<Integer>();
ts1.add(100);
ts1.add(99);
ts1.add(2);
ts1.add(1);
System.out.println(ts1);
}
}
output : [aman, baman, daram, karan, ram, suraj]
[1, 2, 99, 100]
Nice java post
ReplyDelete