Sort and optimize Java Vector with custom objects
I have to optimize Java Vector with "class Row_i" - objects (see below), which describes two series(A and B) of number ranges (startPos - endPos) are equal in every row. This vector has to be sorted and optimized.
Group/Sort Criterias:
1 - Row grouping by id_A,
2 - Row grouping by id_B, 3 - Row grouping by startPosA, 4 - Row grouping by startPosB,So after sorting have to remove redundant rows.
Redundant rows:
1. if startPosA(i+1) value is the next number startPosA(i) valueRow-Optimizing should happend only if PosB meet same criteria.
Vector not Sorted: ..................................
id_A id_B sPosA - ePosA sPosA - ePosA
2392 ++ 4061 ++ 3158 - 3163 ++ 13222 - 13227;
1192 ++ 2064 ++ 287 - 290 ++ 257 - 260; 2392 ++ 1063 ++ 480 - 590 ++ 1950 - 1960; 1092 ++ 1555 ++ 7385 - 7395 ++ 193 - 203; 1192 ++ 2064 ++ 273 - 286 ++ 243 - 256; 1192 ++ 2064 ++ 291 - 294 ++ 261 - 264;Vector sorted and optimized
1092 ++ 1555 ++ 7385 - 7395 ++ 193 - 203;
1192 ++ 2064 ++ 273 - 294 ++ 243 - 264; 2392 ++ 1063 ++ 480 - 590 ++ 1950 - 1960; 2392 ++ 4061 ++ 31开发者_C百科58 - 3163 ++ 13222 - 13227 .........................Depends of implementation collection-size vary. And is in one case 200 - 5000 objects Has anybody idea how to solve this, i efficient way.
I would be gratefull for any help.
Entire classes in Java here:
Objects - class "Row_i" -> http://pastebin.com/wc3ytUqf,@Missing Faktor: Thank you for the hint!!
Use SortedSet as variable/field/return-type and TreeSet when you are creating instances. Let your Row_i class implement Comparable or create a class that implemenents Comparator and pass a comparator when creating a tree set (The comparator should be a singleton).
This is the best solution unless you need random access.
Edit: I wrote an example for Comparable http://pastebin.com/5Z3GBNrV
I don't really understand everything you're doing but having a Vector doesn't seem the right choice of collection if you want to sort and remove duplicate. What about a TreeSet?
To guarantee no duplicate and sorting order, your class Row_i would then need to override equals() and hashCode() methods as well as implement Comparable interface, unless you decide to use a Comparator.
精彩评论