如何在自定义集合中使用`Collections.min()`?

时间:2021-10-06 22:54:16

I wrote a custom collection called MySet<T>, it's essentially a wrapper over HashSet:

我写了一个名为MySet 的自定义集合,它本质上是HashSet的包装器:

import java.util.Collections;
import java.util.HashSet;

public class MySet <T> {
    private HashSet<T> set;

    public MySet() {
        this.set = new HashSet<>();
    }

    public MySet(T[] elements) {
        this.set = new HashSet<>();
        Collections.addAll(this.set, elements);
    }
    public HashSet<T> getSet() {
        return this.set;
    }
}

It contains the methods that are relevant for sets like union(), intersect() etc.

它包含与union(),intersect()等集合相关的方法。

I also wrote a class called Person which represents basic details about a person and implements Comparable interface:

我还写了一个名为Person的类,它代表了一个人的基本细节并实现了Comparable接口:

public class Person implements Comparable<Person> {
    private int id;
    private String name;
    private int age;

    public Person(int id, int age, String name) {
        this.id = id;
        this.age = age;
        this.name = name;
    }

    private boolean equals(Person p) {
        return this.age == p.age;
    }

    public int compareTo(Person p) {
        return this.age - p.age;
    }
}

We compare Person objects based on age field.

我们根据年龄字段比较Person对象。

I need to write a third class that will only have the method which will receive a MySet<Person> object and will determine the minimum Person. Of course this can be done manually by using two loops to find the minimum age but the point is to use Java methods for generics, so I though using Collections.min() on MySet<Person>:

我需要编写第三个类,它只有接收MySet 对象的方法,并确定最小的Person。当然这可以通过使用两个循环来手动完成,以找到最小年龄,但重点是使用Java方法进行泛型,所以我在MySet 上使用Collections.min():

public class MinimumClass<T> {
    public T minElement(MySet<T> set) {
        Collections.min(set);
    }
}

Which doesn't compile and gives an error: no instance(s) of type variable(s) T exist so that MySet<T> conforms to Collection<? extends T> which as far as I understand means that I have to implement Collection interface in MySet.

哪个不编译并给出错误:没有类型变量T的实例存在,以便MySet 符合Collection 据我所知,这意味着我必须在MySet中实现Collection接口。

Is there an easier way to write the method that will determine the minimum person using the fact that Person implements Comparable and MySet is a generic class?

是否有一种更简单的方法来编写使用Person实现Comparable和MySet是泛型类的事实来确定最小人的方法?

1 个解决方案

#1


2  

Since HashSet is a Collection, you could just call Collections.min on it. Note that you should limit it to Ts that are Comparable in order to use Collections.min.

由于HashSet是一个集合,你可以在其上调用Collections.min。请注意,为了使用Collections.min,您应该将其限制为可比较的Ts。

public class MinimumClass<T extends Comparable<T>> {
    public T minElement(MySet<T> mySet) {
        return Collections.min(mySet.getSet());
    }
}

#1


2  

Since HashSet is a Collection, you could just call Collections.min on it. Note that you should limit it to Ts that are Comparable in order to use Collections.min.

由于HashSet是一个集合,你可以在其上调用Collections.min。请注意,为了使用Collections.min,您应该将其限制为可比较的Ts。

public class MinimumClass<T extends Comparable<T>> {
    public T minElement(MySet<T> mySet) {
        return Collections.min(mySet.getSet());
    }
}