I am trying to get the records with min distance, min speed and max speed in the result of my query. Currently I am getting the shortest distance but I am facing problem to get the min and max speed and I am asking myself whether it is possible to add another public int compareTo(BehaviourItem otherItem) method in the BehaviourItem class to reach that but I am getting the error Duplicate method compareTo(BehaviourItem) in type BehaviourItem.
How can I get the min and max speed from the BehaviourItem class?
Code:
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(); BehaviourItem class:
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); } }
Comparators and pass them toCollection.sortmethod