使用Comparable获取多个项目的最小值和最大值

Mr *_*ker 3 java

我试图min distance, min speed and max speed在我的查询结果中获取记录 .目前我的距离最短,但我面临着获得最小和最大速度的问题,我问自己是否有可能public int compareTo(BehaviourItem otherItem)BehaviourItem课堂上添加另一种方法来达到这个目的但是我得到了错误 Duplicate method compareTo(BehaviourItem) in type BehaviourItem.

如何从BehaviourItem课堂上获得最低和最高速度?

码:

         PreparedStatement prepared = con
                 .prepareStatement("SELECT speed, stop_distance from behaviour where mac = ? and stop_name = ?");
                 prepared.setString(1, macD);
                 prepared.setString(1, sto_nam);
                 ResultSet rsBehav = prepared.executeQuery();
                 List<BehaviourItem> behavList = new ArrayList<BehaviourItem>();
                 while (rsBehav.next()) {
                     int distance = rsBehav.getInt("stop_distance");
                     int speed = rsBehav.getInt("speed");
                     BehaviourItem behItem = new BehaviourItem(distance, speed);
                     behavList.add(behItem);

                 }
                 Collections.sort(behavList);
                 int minDistance =  behavList.get(0).getDistance();
Run Code Online (Sandbox Code Playgroud)

BehaviourItem类:

public class BehaviourItem implements Comparable<BehaviourItem>{
    int speed;
    int distance;

    public BehaviourItem(int speed, int distance) {
        super();
        this.speed = speed;
        this.distance = distance;
    }

    public int getSpeed() {
        return speed;
    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }

    public int getDistance() {
        return distance;
    }

    public void setDistance(int distance) {
        this.distance = distance;
    }

    @Override
    public int compareTo(BehaviourItem otherItem) {
        // TODO Auto-generated method stub
        return Integer.compare(this.distance, otherItem.distance);
    }

}
Run Code Online (Sandbox Code Playgroud)

Hol*_*ger 7

你不应该让BehaviourItem工具,Comparable因为它没有自然的顺序.相反,为不同的属性实现不同的Comparators.

请注意,在Java 8中,您可以Comparator简单地实现这样的

Comparator<BehaviourItem> orderBySpeed=Comparator.comparingInt(BehaviourItem::getSpeed);
Run Code Online (Sandbox Code Playgroud)

这相当于

Comparator<BehaviourItem> orderBySpeed=new Comparator<BehaviourItem>() {
    public int compare(BehaviourItem a, BehaviourItem b) {
        return Integer.compare(a.getSpeed(), b.getSpeed());
    }
};
Run Code Online (Sandbox Code Playgroud)

要么

Comparator<BehaviourItem> orderByDistance
                         =Comparator.comparingInt(BehaviourItem::getDistance);
Run Code Online (Sandbox Code Playgroud)

对于其他财产.

几乎每个使用订单的收集方法都有一个重载支持传递Comparator定义订单而不是使用自然顺序:

Collections.sort(behavList, orderBySpeed);
Run Code Online (Sandbox Code Playgroud)

RESP.

Collections.sort(behavList, orderByDistance);
Run Code Online (Sandbox Code Playgroud)

您甚至可以创建比较器ad-hoc:

Collections.sort(behavList, Comparator.comparingInt(BehaviourItem::getDistance));
Run Code Online (Sandbox Code Playgroud)

Collections.sort(behavList, Comparator.comparingInt(BehaviourItem::getSpeed));
Run Code Online (Sandbox Code Playgroud)

但是流API允许您查找最小值或最大值,甚至无需排序:

Optional<BehaviourItem> minBySpeed=behavList.stream()
                       .max(Comparator.comparingInt(BehaviourItem::getSpeed));
Run Code Online (Sandbox Code Playgroud)