in Core Java

TreeSet in Java – Sorting example

TreeSet in Java

TreeSet is the concrete implementation of NavigableSet, SortedSet and Set interfaces. Treeset is the collection which sorts the elements and maintains that order. TreeSet implements SortedSet, NavigableSet. Any collection implementation that implements sorted set should sort the elements when they are getting added to the collection.
That means when we fetch the elements from collection they are already sorted.

TreeSet by default sorts the elements based on natural order ie., The sorting criteria defined in the compareTo method of a type.

A custom comparator can also be specified with the TreeSet for Sorting.

 

References:

https://docs.oracle.com/javase/7/docs/api/java/util/TreeSet.html

https://docs.oracle.com/javase/7/docs/api/java/util/SortedSet.html

https://docs.oracle.com/javase/7/docs/api/java/util/NavigableSet.html

https://docs.oracle.com/javase/7/docs/api/java/util/Set.html

 

Srinivas Reddy

Srinivas Reddy

An IT - Specialist with loads of experience in Java, J2ee platforms and loves to share his experience on technology with You...

More Posts - Website

Follow Me:
TwitterFacebook

Srinivas Reddy
Written By:

An IT - Specialist with loads of experience in Java, J2ee platforms and loves to share his experience on technology with You...