Getting Least Value Element From a Set by Using Sorting Logic on TreeSet in Java Last Updated : 21 Aug, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report In order to get the value element of the user-defined object, one needs to implement the sorting logic in TreeSet. Now in order to implement the sorting logic on the user-defined objects, the Comparator object needs to be passed along the TreeSet constructor call. Further, Comparator implementation will hold the sorting logic. For this, the compare() method needs to be overridden to provide sorting logic on user-defined objects. Lastly, the least element is extracted out using the first() method. Illustration: Input : Set: ["A"=36678 , "B"=456456 ,"C"=76434 ,"D"=4564] Output: Least value => element: D, value: 4564 Input : Set: ["x"=1, "y"=2 ,"z"=3] Output: Set: Least value =>element: x, value: 1 Approach To get the least value element TreeSet Use comparators to order the object of a user-defined class.Implementing the sorting functionality that overrides the compare() method to sort the TreeSet by value.After sorting, the first object of the TreeSet will be the least value element. Implementation: Example Java // Java program to find out the least value element // in a TreeSet // Importing java input output libraries // Importing Comparator and TreeSet class // from java.util package import java.io.*; import java.util.Comparator; import java.util.TreeSet; // Two auxiliary classes are required // Class1- Sorting logic class invoking comparator // Class2- Element class // Class3 - Main class(Implementation class) // Class1- Sorting logic where // comparator holds the sorting logic class MySort implements Comparator<Element> { @Override // Overriding // To provide the sorting logic to below created TreeSet // in main() method using compare() method public int compare(Element e1, Element e2) { // Condition check // Comparing values of element if (e1.getvalue() > e2.getvalue()) { // If condition holds true return 1; } else { // If condition is false return -1; } } } // Class- 2 // key for TreeSet --> Name class Element { // Member variables of this class private String name; private int value; // Constructor of this class public Element(String n, int s) { // Referring to same object // using this keyword this.name = n; this.value = s; } // key--> Name // Using name as a key for TreeSet public String getname() { return name; } // Return value for the given key(name) public int getvalue() { return value; } // Format in which output is returned public String toString() { return "element: " + this.name + ", value: " + this.value; } } // Main Class- Implementing sorting functionality class GFG { // Main driver method public static void main(String[] args) { // Implementing sorting functionality with TreeSet // by implementing Comparator and // calling (MySort) from TreeSet constructor // Creating an object of Treeset // where object type is Element TreeSet<Element> Tree = new TreeSet<Element>(new MySort()); // Adding elements to adobe object of TreeSet // Custom inputs Tree.add(new Element("A", 36778)); Tree.add(new Element("B", 456456)); Tree.add(new Element("C", 76433)); Tree.add(new Element("D", 4564)); // Printing first element of above created TreeSet // which will be least value among all elements System.out.println("Least value =>" + Tree.first()); } } OutputLeast value =>element: D, value: 4564 Comment More infoAdvertise with us Next Article How to Sort TreeSet Elements using Comparable Interface in Java? A arun0318cse19 Follow Improve Article Tags : Java Technical Scripter Java Programs Technical Scripter 2020 Java-Collections java-treeset +2 More Practice Tags : JavaJava-Collections Similar Reads Getting TreeSet Element Smaller than Specified Element using Floor Method in Java To get TreeSet Element smaller than Specified Element using floor() Method in Java.The TreeSet is used to store elements in sorted order. The floor method returns the greatest element in the set less than or equal to the given element, or null if there is no such element. set = {10,20,30,40,50} // g 2 min read How to Sort TreeSet Elements using Comparable Interface in Java? TreeSet is an implementation of the SortedSet interface in Java that uses a Tree for storage. The ordering of the elements is maintained by a Set using their natural ordering whether an explicit comparator is provided. To sort TreeSet elements using Comparable interface in java first, we create a cl 3 min read How to Get TreeMap Key or Value using Index in Java? The TreeMap in Java is used to implement the Map interface and NavigableMap along with the AbstractMap Class. The TreeMap is sorted according to the natural ordering of its keys. The TreeMap class is a Red-Black tree implementation of the Map interface and thus does not expose any methods using whic 5 min read How to Implement a Custom Order or Sorting for Key-Value Pairs in a TreeMap in Java? The Java Collections Framework includes the TreeMap class, in which Java offers a sorted collection of key-value pairs. By default, TreeMap uses a custom Comparator or arranges components according to their natural ordering. In this article, we will learn how to apply a custom order for key-value pa 2 min read How to Sort LinkedHashSet Elements using Comparable Interface in Java? The LinkedHashSet is an ordered version of HashSet that maintains a doubly-linked List across all elements. When the iteration order is needed to be maintained this class is used. When iterating through a HashSet the order is unpredictable, while a LinkedHashSet lets us iterate through the elements 3 min read Reversing Elements Order in TreeSet in Java Using the descendingIterator Method The descendingIterator() method of java.util.TreeSet<E> class is used to return an iterator over the elements in the set in descending order. Once the iterator assigns with the return value of the descendingIterator(), iterate the iterator using while loop. Example: Input : TreeSet = [2, 5, 6] 2 min read Like