对Java对象进行排序,并根据属性查找相对位置

时间:2012-08-16 12:21:40

标签: java algorithm logic

我有一个有趣的问题

这是对象结构

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; 

}

现在基于序列和窗口,我需要派生与窗口相关的窗口位置

测试数据
    所以对于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

4 个答案:

答案 0 :(得分:1)

实施Comparable可能有意义。这允许对对象进行排序。您可以像这样实施compareTo(T)

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

这样你的对象就可以按顺序排序。

现在将window 1的所有对象收集到List,将window 2的对象收集到另一个列表中等。

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

使用Collections.sort(List)对所有列表进行排序:

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

然后,每个窗口都有一个列表,可通过map.get(window)访问。这些列表中的每一个都具有最低sequence作为其第一对象,第二最低作为第二对象等的对象 - &gt;窗口位置是对象的索引+ 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 :(得分:0)

因此, windowposition 只是另一个序列。我会在每个窗口的最后一个 windowposition 中放入Map<Integer,Integer>。 您不一定要对对象进行排序。

答案 2 :(得分: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);
    }

答案 3 :(得分:0)

试试此代码

windowposition = sequence - window < 0 ? 1 : sequence - window + 1;