2012-08-16 136 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; 
相关问题