TreeSet Class in Java with
example
Java TreeSet
class which implements NavigableSet interface which is child interface of SortedSet
interface. Objects in TreeSet class are stored in sorted ascending order.
TreeSet
is similar to HashSet except that it sorts the elements in the
ascending order while HashSet doesn’t maintain any insertion or ascending order.
TreeSet
class Declaration
public class
TreeSet extends AbstractSet
implements NavigableSet, Cloneable, java.io.Serializable
{…}
Features
of TreeSet class
1] Java TreeSet class is ultimately subclass of Set interface that’s why TreeSet class stores unique element only.
2] Java TreeSet
class does not allow null , If we try to add null in TreeSet then at runtime it
will throw java.lang.NullPointerException.
TreeSet set = new TreeSet<>();
set.add(123);
set.add(null);
Exception in thread "main" java.lang.NullPointerException
at
java.util.TreeMap.put(TreeMap.java:563)
at
java.util.TreeSet.add(TreeSet.java:255)
at
com.vr.cls.TreeSetDemo.main(TreeSetDemo.java:10)
3] Java TreeSet
class sorts the element in ascending order, So TreeSet does not preserve the
insertion order of the element.
4] TreeSet does not
allow inserting Heterogeneous objects. It will throw classCastException at
Runtime if trying to add hetrogeneous objects.
public static void main(String[] args) {
TreeSet set = new TreeSet<>();
set.add(123);
set.add("abc");
System.out.println(set);
}
Output
Exception in
thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
at java.lang.String.compareTo(String.java:111)
at java.util.TreeMap.put(TreeMap.java:568)
at java.util.TreeSet.add(TreeSet.java:255)
5] Java TreeSet
serves as an excellent choice for storing large amounts of sorted unique information
which are supposed to be accessed quickly because of its access and retrieval times are quiet fast.
6] Java TreeSet class
is not synchronized. If multiple threads access a tree set
concurrently, and at least one of the threads modifies the set, it must be
synchronized externally.
SortedSet s = Collections.synchronizedSortedSet(new TreeSet(...));
7] The iterators
returned by this class's iterator method are fail-fast:
Constructor
and Description
TreeSet set = new TreeSet()
Constructs a new,
empty tree set, sorted according to the natural ordering of its elements.
TreeSet set = new TreeSet(Collection c)
Constructs a new
tree set containing the elements in the specified collection, sorted according
to the natural ordering of its elements.
TreeSet set = new TreeSet(Comparator comparator)
Constructs a new,
empty tree set, sorted according to the specified comparator.
TreeSet set = new TreeSet(SortedSet s)
Constructs a new
tree set containing the same elements and using the same ordering as the
specified sorted set.
Java
TreeSet Example
package com.vr.techfloaters;
import java.util.TreeSet;
public class TreeSetDemo
{
public static void main(String[] args) {
TreeSet set = new TreeSet<>();
//Similier
datatype elements are added using add() method
set.add(123);
set.add(879);
set.add(456);
//Duplicate not
be inserted
set.add(123);
//Can not insert null at Runtime throws java.lang.NullPointerException
//set.add(null);
//Different data type can not insert at runtime it
throws java.lang.NullPointerException
//set.add("abc");
//print in
ascending order
System.out.println(set);
}
}
Output
[123, 456,
879]
0 comments:
Post a Comment