2012-08-16 135 views
1

我有一個有趣的問題排序Java對象,並根據屬性

這裏找到相對位置是基於序列和窗口對象結構

public class Testdata { 
    //Which is a consecutive running number i.e 1,2,3..etc 
    private int sequence; 

    //classified based on this again any random numbers 
    private int window; 

    //need to calculate 
    private int windowposition; 

} 

現在,我需要得到windowposition在相對於窗口

測試數據
所以對於TESTDATA序列/窗口

 1/2 
     2/3 
     3/2 
     4/3 
     5/3 

期望輸出

sequence/window : window position would be (in the same order) 

    1/2  : 1 

    2/3  : 1 

    3/2  : 2 

    4/3  : 2 

    5/3  : 3 

更新:

是真的,我已經下面爲了實現可比性和排序的名單到現在

1/2 
3/2 
2/3   
4/3 
5/3 

我怎麼計算與其窗口相關的每個元素的windowposition

+0

你需要windowsposition = sumOfPreviousWindowById(窗口)+ 1嗎? – neworld 2012-08-16 12:24:56

+7

什麼問題? – 2012-08-16 12:25:20

+0

@ngmiceli仔細閱讀這個問題,你會注意到,如果你仍然沒有「現在基於序列和窗口,我將需要派生窗口相對於窗口」 – Sudhakar 2012-08-16 13:16:53

回答

1

實施Comparable可能有意義。這可以讓你的對象被排序。你可以這樣實現compareTo(T)

int compareTo(Testdata o) { 
    return ((Integer)this.sequence).compareTo(o.sequence); 
} 

這樣你的對象可以按順序排序。

現在收集所有與window 1對象爲List,與window 2對象到另一個列表使用Collections.sort(List)

HashMap<Integer, ArrayList<Testdata>> map = new HashMap<Integer, ArrayList<Testdata>>(); 

// Add all the objects like this 
while (...) { // While there are more objects 
    Testdata td = ... // Get next object 

    List<TestData> list = map.get(td.window); 
    if (list == null) { 
    list = new ArrayList<Testdata>(); 
    map.put(td.window, list); 
    } 

    list.add(td.sequence); 
} 

排序所有列表:

for (ArrayList<TestData> list : map) { 
    Collections.sort(list); 
} 

然後你有每個窗口一個列表,可通過map.get(window)訪問。這些列表中的每一個都具有最低的sequence作爲其第一個對象,最低的第二個對象等。 - >窗口位置是對象的索引+ 1。

編輯:

如果你的對象已經由窗口和順序排序(到一個列表),你可以做這樣的事情來分配窗口的位置:

int window = 1; 
int wp = 0; 
for (Testdata td : list) { 
    if (td.window > window) { 
    wp = 1; 
    window = td.window; 
    } else { 
    wp++; 
    } 

    td.windowposition = wp; 
} 
+1

這不會給你想要的輸出。您需要根據序列找到窗口位置。所以compareTo應該基於「序列」。 – Deepa 2012-08-16 13:20:16

+0

是的,只是編輯了我的答案,而不是用'sequence'排序。 – riha 2012-08-16 13:23:17

+0

@Deepa其實compareTo應該基於序列和windowposition – Sudhakar 2012-08-16 13:27:48

0

因此,窗口位置只是另一個序列。我會在每個窗口的最後窗口位置中輸入一個Map<Integer,Integer>。 你不一定要排序你的對象。

0
So its basically window's no. of occurrence in the array of objects. 
Seq/Window:Position 
1/2 : 1 => Window 2 , 1st position (1st occurrence of Window 2) 
2/3 : 1 => Window 3 , 1st position (1st occurrence of Window 3) 
3/2 : 2 => Window 2 , 2nd position (since Window 2 has already positioned in sequence 1) 
4/3 : 2 => Window 3 , 2nd position (since Window 3 has already positioned in sequence 2) 
5/3 : 3 => Window 3 , 3rd position (since Window 3 has already positioned in sequence 2 and 4) 

Is that right? 

List<Window> windows = new ArrayList<Window>(); 
     windows.add(new Window(2, 3)); 
     windows.add(new Window(1, 2)); 
     windows.add(new Window(3, 2)); 
     windows.add(new Window(4, 3)); 
     windows.add(new Window(5, 3)); 

     Collections.sort(windows); 

HashMap<Integer, Integer> wpMap = new HashMap<Integer, Integer>(); 
    Integer wpos; 
     for (Window w : windows) { 
      wpos = wpMap.get(w.window); 
      if (wpos == null) { 
       wpos = 1; 
      } else { 
       wpos++; 
      } 
      w.setWindowPosition(wpos); 
      wpMap.put(w.window, wpos); 
     } 
    for (Window w : windows) { 
     System.out.println(w.sequence+"/"+w.window+":"+w.windowposition); 
    } 
+0

是的,我已經實現了可比較和排序列表以下面順序 二分之一 3/2 三分之二 4/3 5/3 現在如何計算每個元件的windowposition相對於其窗口 – Sudhakar 2012-08-16 13:18:36

+0

檢查更新註釋。這將基於序列對列表進行排序後。 – Deepa 2012-08-16 13:30:39

0

試試這個代碼

windowposition = sequence - window < 0 ? 1 : sequence - window + 1; 
相關問題