Getting Least Value Element From a Set by Using Sorting Logic on TreeSet in Java

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 packageimport 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 logicclass 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 --> Nameclass 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 functionalityclass 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());    }} |
Output
Least value =>element: D, value: 4564
Â



