Java 使用树形集合的排序逻辑从集合中获取最小值的元素

Java 使用树形集合的排序逻辑从集合中获取最小值的元素

为了获得用户定义的对象的值元素,需要在TreeSet中实现排序逻辑。现在,为了在用户定义的对象上实现排序逻辑,需要在调用TreeSet构造函数时传递比较器对象。此外,比较器的实现将持有排序逻辑。为此,compare()方法需要被重写,以提供用户定义对象的排序逻辑。最后,使用first()方法将最小的元素提取出来。

插图。

输入 : Set: ["A"=36678 , "B"=456456 ,"C"=76434 ,"D"=4564]
Output: Least value => element: D, value: 4564

输入 : Set: ["x"=1, "y"=2 ,"z"=3]
输出: Set: Least value =>element: x, value: 1
Java

步骤

为了获得最小值元素TreeSet

1.使用比较器对用户定义的类的对象进行排序。
2.实现排序功能,重写compare()方法,对TreeSet按值排序。
3.经过排序,TreeSet的第一个对象将是价值最小的元素。

实现。

示例

// 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());
    }
}
Java

输出

Least value =>element: D, value: 4564
Java

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册