views:

188

answers:

2

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) value

Row-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 ++ 3158 - 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!!

A: 

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.

Damien
+1  A: 

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

Arian
Thanks, dont need random access. Never bevor worked with Sets, it is possible you write a more exactly example?I managed this problem with simple for - loop and if - query (see below). But it doesn't work properly.
xaoc
@xaoc: Post your code on pastebin.com and put its link here in the comments.
missingfaktor
class Row_i -> http://pastebin.com/wc3ytUqf, class UpdateTable-> http://pastebin.com/K4NeTtCV, @Missing Faktor: Thank you for the hint!!
xaoc
My own "preSolution" but its working not well. class UpdateTable-> http://pastebin.com/K4NeTtCV, Because if I have succeed row matching criteria 1-3 and then matching criteria 4 n -times. So the summarization(grouping) ends at this point. @Missing Faktor: Thank you for the hint!!
xaoc