I have an interesting problem
Here is the object structure
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;
}
Now based on the sequence and window , i would need to derive windowposition in relation to the window
Test Data
So for testdata sequence/window
1 / 2
2 / 3
3 / 2
4 / 3
5 / 3
Expected Output
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
Update:
yes indeed , I have already implemented comparable and sorted the list to below order
1 / 2
3 / 2
2 / 3
4 / 3
5 / 3
Now how do I calculate the windowposition of each element in relation to its window
It would probably make sense to implement Comparable. That allows your objects to be sorted. You would implement
compareTo(T)like so:That way your objects can be sorted by sequence.
Now collect all the objects with
window1 into aList, objects withwindow2 into another list etc.Sort all of the lists using Collections.sort(List):
You then have one list for each window, accessible via
map.get(window). Each of these lists has the object with the lowestsequenceas its first object, second lowest as second object etc. -> the window position is the index of the object + 1.EDIT:
If your objects are already sorted by window and sequence (into a single list), you can do something like this to assign the window positions: