Treeset Java Collection Framework

Sharing is healthy !!

Hey guys!! Welcome to flower brackets blog. Today we are going to learn treeset java collection framework.

introduction : Treeset Java Collection Framework

Here you can see treeset hierarchy

Treeset Java Collection Framework

As you can see in the above image, treeset class implements navigableset interface.

Navigableset interface extends sortedset interface. Sortedset interface extends set interface.

Set interface extends collection interface. Collection interface extends iterable interface.

Syntax

java.util

Class TreeSet<E>

java.lang.Object
     →java.util.AbstractCollection<E>
          →java.util.AbstractSet<E>
                →java.util.TreeSet<E>

Parameters -

E - type of elements maintained by this set

Implemented Interfaces -

Serializable, Cloneable, Iterable<E>, Collection<E>, NaviagableSet<E>, Set<E>, SortedSet<E>

Treeset class belongs to java.util package. Above you can see “E” this is nothing but type of elements which we are going to store in the treeset.

Also Read – Treemap In Java

Treeset class extends abstractset class. Abstractset class extends abstractcollection class. Abstractcollection class extends object class.

Treeset class implements serializable, cloneable, iterable, collection, navigableset, set and sortedset interface.


what is treeset?

Treeset is a collection which is used to store objects or elements. It does not contain duplicate elements.

It will contain only unique elements. Treeset maintains ascending order by default.

Treeset is not synchronised and treeset class won’t allow null elements.

Methods supported by treeset

MethodDescription
boolean add( E e )adds specified element to this set if it is not already present.
boolean addAll( Collection c)adds all the elements in the specified collection to this set.
E ceiling( E e )returns least element in this set greater than or equal to the given element or null if there is no such element.
void clear( )removes all the elements form this set.
Object clone( )returns a shallow copy of this treeset instance.
Comparator comparator( )returns the comparator used to order the elements in this set or null if this set uses the natural ordering of its elements.
boolean contains( Object o )returns true if this set contains the specified element.
Iterator descendingIterator( )returns an iterator over the elements in this set in descending order.
NavigableSet descendingSet( )returns a reverse order view of the elements contained in this set.
E first( )returns the first (lowest) element currently in this set.
E floor( E e )returns the greatest element in this set less than or equal to the given element or null if there is no such element.
SortedSet headSet( E toElement )returns a view of the portion of this set whose elements are strictly less than toElement.
NavigableSet headSet( E toElement, boolean inclusive )returns a view of the portion of this set whose elements are less than (or equal to, if inclusive is true) toElement.
E higher( E e )returns the least element in this set strictly greater than the given element or null if there is no such element.
boolean isEmpty( )returns true if this set contains no elements.
Iterator iterator( )returns an iterator over the elements in this set in ascending order.
E last( )returns the last (highest) element currently in this set.
E lower( E e )returns the greatest element in this set strictly less than the given element or null if there is no such element.
E pollFirst( )retrieves and removes the first (lowest) element or returns null if this set is empty.
E pollLast( )retrieves and removes the last (highest) element or returns null if this set is empty.
boolean remove( Object o )removes the specified element from this set if it is present.
int size( )returns the number of elements in this set.
NavigableSet subSet( E fromElement, boolean fromInclusive, E toElement, boolean toInclusive )returns a view of the portion of this set whose elements range from fromElement to toElement.
SortedSet subSet( E fromElement, E toElement )returns a view of the portion of this set whose elements range from fromElement. inclusive to toElement. exclusive
SortedSet tailSet( E fromElement )returns a view of the portion of this set whose elements are greater than or equal to fromElement.
NavigableSet tailSet( E fromElement, boolean inclusive )returns a view of the portion of this set whose elements are greater than ( or equal to, if inclusive is true ) fromElement

Below are the methods inherited from

Methods inherited from class java.util.AbstractSet
equals, hashCode, removeAll


Methods inherited from class java.util.AbstractCollection
containsAll, retainAll, toArray, toArray, toString


Methods inherited from class java.lang.Object
wait, wait, wait, finalize, getClass, notify, notifyAll


Methods inherited from interface java.util.Set
removeAll, containsAll, hashCode, equals, retainAll, toArray, toArray

 example: Treeset Java Collection Framework

import java.util.TreeSet;

public class TreesetExample
{
public static void main(String[] args)
{
// Here's treeset of string type
TreeSet<String> tsStr = new TreeSet<String>(); tsStr.add("orange");
tsStr.add("mango");
tsStr.add("strawberry");
tsStr.add("cherry");
tsStr.add("apple");
tsStr.add("banana");
System.out.println("------------Treeset in java example--------");
System.out.println("Displaying Treeset of type string : " + tsStr);

// Here's treeset of integer type
TreeSet<Integer> tsInt = new TreeSet<Integer>();
tsInt.add(2);
tsInt.add(10);
tsInt.add(1);
tsInt.add(50);
tsInt.add(100);
tsInt.add(140);
System.out.println("Displaying Treeset of type integer : " + tsInt);
}
}

Output:

Treeset Java Collection Framework


conclusion

So this is about treeset java collection framework. I hope you have understood the concept.

You can subscribe to my blog flower brackets if you haven’t already.


Reference – Oracle help centre

Sharing is healthy !!