2015-06-22 47 views
3

我想在我的查詢結果中得到min distance, min speed and max speed的記錄。目前我正在獲得最短距離,但我面臨的問題是獲得最小和最大速度,我在問自己是否可以在BehaviourItem課程中添加另一個public int compareTo(BehaviourItem otherItem)方法來達到該目的,但我得到的錯誤爲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(); 

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); 
    } 

} 
+1

,你可以創建自定義'Comparator's並傳遞給'Collection.sort'方法 – user902383

回答

7

您不應讓BehaviourItem實施Comparable,因爲它沒有自然順序。相反,針對不同的屬性實施不同的Comparators

注意,在Java中8,就可以實現這樣一個簡單的Comparator

Comparator<BehaviourItem> orderBySpeed=Comparator.comparingInt(BehaviourItem::getSpeed); 

這是

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

Comparator<BehaviourItem> orderByDistance 
         =Comparator.comparingInt(BehaviourItem::getDistance); 

的其他財產的等價物。

幾乎使用順序每次收集方法的過載支承傳遞一個Comparator來定義,而不是使用天然順序的順序:

Collections.sort(behavList, orderBySpeed); 

RESP。

Collections.sort(behavList, orderByDistance); 

你甚至可以創建比較特設:

Collections.sort(behavList, Comparator.comparingInt(BehaviourItem::getDistance)); 

Collections.sort(behavList, Comparator.comparingInt(BehaviourItem::getSpeed)); 

,但流API允許你尋找最小或最大,即使沒有排序:

Optional<BehaviourItem> minBySpeed=behavList.stream() 
         .max(Comparator.comparingInt(BehaviourItem::getSpeed)); 
+0

我怎麼能在這個方法比較(BehaviourItem otherItem1,BehaviourItem otherItem2)這裏返回這兩個項目?和我在哪裏可以把比較器 orderBySpeed = Comparator.comparingInt(BehaviourItem :: getSpeed);'? –

+0

你可以在你想要的地方放置變量,如果你認爲它會被經常使用,你可以把它變成一個'BehaviourItem'的靜態變量,但是你也可以在使用之前把它放置好,如前所述,你可以甚至創建比較器ad-hoc,將它傳遞給'sort'(或'max')方法,而不需要變量。 – Holger

0

使用Comparator而不是Comparable

您可以根據需要定義任意數量的排序行爲。

1

Comparable基本上定義具有自然順序的對象(例如,數字),因此只能有一個compareTo()方法。

要獲取一個值的最小/最大值,您可以使用排序後的集合,例如,一個列表,並訪問第一個和最後一個元素。

但是,由於您的BehaviourItem沒有自然順序(它是根據速度還是距離?),您必須根據情況定義順序。這就是Comparator發揮作用的地方:當你想按速度排序時,你使用一個比較速度的比較器,如果你想按距離排序,你可以使用比較器來計算距離等。

當然,如果速度和距離發生變化很多,你總是需要得到最小/最大值,你也可以迭代所有項目並選擇最小/最大值的經典方式。

另一種選擇,因爲你反正使用查詢,可能是直接添加min(speed), max(speed)等。這可能需要一個單獨的查詢或被添加到每個結果行,這反過來可能會降低查詢速度,但如果你只得到幾行可能仍然值得。

1

您不能在BehaviourItem類中定義另一個compareTo()函數,但可以創建自定義比較器並使用它來對列表進行排序。

自定義比較的例子 -

public class BehaviourItem implements Comparable<BehaviourItem>{ 
    . 
    . 
    . 

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

    static class BehaviourItemComparator implements Comparator<BehaviourItem> 
    {    
     public int compare(BehaviourItem b1, BehaviourItem b2) 
     { 
      return Integer.compare(b1.getSpeed(), b2.getSpeed()); 
     } 
    } 

} 

然後你可以使用它作爲 -

Collections.sort(behavList, BehaviourItem.BehaviourItemComparator) 
相關問題