Java TreeSet - Smart Tech Guides

Latest

Hi this is Sravan Kumar from India. SMART TECH GUIDES is a technical blog. it helps you to learn about Java topics, frameworks and tools like Jenkins, GitHub & related explanations, data base related topics. All these Explanations are explained in simple and understandable manner.

Subscribe Us

SMART TECH GUIDES

Java TreeSet

Java TreeSet:

The TreeSet is part of collection framework and it is available in “java.util” package.

TreeSet inherits AbstractSet class and implements the Navigable Set interface.

Java TreeSet class contains unique elements only like HashSet.

Java TreeSet class maintains ascending order.

Java TreeSet class access and retrieval times are quite fast.

Java TreeSet class doesn't allow null element, throwing null pointer exception.

Java TreeSet class is non synchronized.

 

Creation of TreeSet:

TreeSet<E> CustomerName = new TreeSet<E>();


Methods of TreeSet:

Some of the important methods in TreeSet

Boolean add(Element obj): This method is used to place the specified element into the set.

Boolean remove(Element obj): This method is used to delete the specified element from the set. If it is available.

Boolean contains(Element obj): This method return true if the specified element is available in the set.

Boolean isEmpty(): This method return true if the set isEmpty.

intsize(): This method returns the count of the no.of elements available in the set.

void clear(): This method is used to delete all the elements from the set.

First():  This method returns the first (lowest) element currently in this set.

Last(): This method Returns the last (highest) element currently in this set.

pollFirst(): This method retrives and removes the first (lowest) element, or returns null if this set is empty.

pollLast(): This method retrives and removes the last (highest) element, or returns null if this set is empty.

descendingIterator(): This method returns an iterator over the elements in this set  in descending order.


Java Code

Output:

No comments:

Post a Comment