views:

86

answers:

2

I have to filter and to sort a ArrayList wíth objects.
- Every object has 2 integer pairs => 4 ints per Object.
- Every value of Column_1 < Column_2 and
- Every value of Column_3 < Column_4.
... so each pair represents a distance.

1.) Distance in 1st(Column_1,Column_2) pair and 2nd(Column_3, Column_4,) pair have to be equal.

2.) if there exists in the list a Obj_1 , whose Column_2 value is equal to Column_1 value+1 of Obj_2 and

3.) if there exists in the list a Obj_1 , whose Column_4 value is equal to Column_3 value+1 of Obj_2

then this objects should be merged to one Object respecting values in each pair. ...minimal values in(Column_1,Column_3) and maximal values(Column_2,Column_4)

Example:

Column_1  Column_2  Column_3  Column_4

----------- before filtering --------------

1. 506       520     771       785
2. 106       110     210       214
3. 502       505     181       184
4. 714       717     270       273
5. 106       110     310       314
6. 111       115     215       219
7. 521       524     767       770
8. 502       505     350       353
9. 100       105     204       209    

-----------after filtering----------

1. 100        115    204       219
2. 106        110    310       314
3. 502        505    181       184
4. 714        717    270       273
5. 502        520    767       785

How can this kind of filtering be done in Java?

+1  A: 
Péter Török
One iteration won't be enough since there are two dimensions. I would suggest a tweak - for step 4, while iterating construct a new collection. Create a new data point using first elem of sorted collection. If next elem is contiguous extend the new data point. Otherwise push it to new collection and create new data point.
aar
Thanks Peter!! You suggesting also Treeset it will automatically remove dublicates. When a element is duplicate, i have to explain in compareTo and equals methods, right? Here is my "preSolution" http://pastebin.com/RkUu0fuY. But somehow don't right explanation for comparable, or at least right order of elements, can you check pls?
jackdaniels
@jack, since that earlier version of my answer I have realized that removing duplicates is not the right solution here - you need to merge them (by hand). And you should remove the `-1`s from your `compareTo` implementation, otherwise it won't be correct (think "symmetricity" - i.e. `sgn(a.compareTo(b))` should be `-sgn(b.compareTo(a))`).
Péter Török
yeah, I am trying to merge two object. By looking for next objects start-value those should be next following number of this.object end-value. How can I ohterwise compare two not equal objects!? Generally all objects/rows in the list are equal...
jackdaniels
@jack, you should separate the order comparison from the detection of "adjacent" objects. The former is used for ordering the elements within the set, thus it must obey the constraints defined by the Java API. The latter should obey the rules defined above, in your post.
Péter Török
@peter, exactly at this point I am disordered. Points 1 and 2 in your approach responsible for comparison, right? And detection of "adjacents" happends in 4. during iteration over a TreeSet right? So having one object, I have to iterate over whole Set, then move to next one. Is this the only possible solution? There is no really use of Treeset in this case!?
jackdaniels
@jack, now this boils down to your "specification" above, which is not 100% clear to me on second thought. My understanding is that for two objects to be merged, _both_ conditions 2) and 3) above must be fulfilled. If this is correct, you only need the right comparator for the job - TreeSet then orders the elements for you so that candidates for merge will be adjacent! Thus you only need to iterate over the set once.
Péter Török
@peter, yes conditions 1-3 above must be fullfilled. And yes this is the point I can't implement the right comparator with compare(),equals(),hash()-overridin. Because I never used comparable/comparator before. What I am doing wrong when describing, how can I describe this more clear!?
jackdaniels
@jack, oops, just now I realized that one of my assumptions was wrong: I failed to notice that there are overlapping intervals between distinct items. So my comparator approach does not work :-( excuse me for the misleading ideas. I will think of an alternative approach...
Péter Török
@jack, see my update.
Péter Török
+1  A: 

Google collections gives you the option to specify a filter predicate that will operate on all entries and decide which to preserve:

Collections2.filter(yourCollection, new Predicate<YourType>() {
    @Override
    public boolean apply(YourType param) {
    // return whether to retain or remove
    }
});
Bozho
@Bozho: Thanks Bozho for this hint! Do you have a simple example or link how to implement this Predicate object... i didn't find any example..
jackdaniels
well, you have your param (which is an element of the collection) - then go and implement your logic.
Bozho
If I could implement, this logic with comparator or for Predicate in google collections2. I would't ask anybody and even write this post!!
jackdaniels