4xaoc
asked on
Java merging and sort list of objects
I am looking for simple comparator logic or any other solution that helps merge objects within a List itself.
Suppose List represents database table with 2 pairs of columns (so overall 4 columns with 4 integer values.) So every list object has two individual pairs (containing two values ).
Initial state of every object:
And Value y1 is always less then y2. (y1 < y2)
In final state
So reducing of List size through, merging of two (or more list objects). According following criteria:
AND
y2 value (e.g. 14) of 1st object, is next number of y1 value (e.g. 15) of 2nd object.
Is there anybody who is willing to help a newbie? And show how to solve this problem in Java, with concrete programming code / examples?
Pls see, below I added an example and also attached txt data file if necessary for testing.
Thanks.
Suppose List represents database table with 2 pairs of columns (so overall 4 columns with 4 integer values.) So every list object has two individual pairs (containing two values ).
Initial state of every object:
1
Every list object is unique in the list.2
Every list object consist 2 pairs of integer values. Suppose a pair_X and a pair_Y. (overall 4 integer values)3
Every pair has 2 integer values. Suppose x1 and x2 and y1 and y2.4
Value x1 is always less then x2. (x1 < x2)And Value y1 is always less then y2. (y1 < y2)
5
Balance in every pair is always equal. (x2 - x1) == (y1 - y2).In final state
So reducing of List size through, merging of two (or more list objects). According following criteria:
x2 value (e.g. 10) of 1st object, is next number of x1 value (e.g. 11) of 2nd object.
AND
y2 value (e.g. 14) of 1st object, is next number of y1 value (e.g. 15) of 2nd object.
Is there anybody who is willing to help a newbie? And show how to solve this problem in Java, with concrete programming code / examples?
Pls see, below I added an example and also attached txt data file if necessary for testing.
Thanks.
A Simplified example just for 4 Columns (which are 2 pairs).
Example:
#Column_1 Column_2 Column_3 Column_4,#
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 merging and sorting-----------------
1. 100 115 204 219
2. 106 110 310 314
3. 502 505 181 184
4. 502 520 767 785
5. 714 717 270 273
Sampletable-4int-withou-hypen.txt
ASKER
Hi mplungian, my issue is really trivial. Generally,
1.) I can sort List_1 asc,
2.) create a second List_2 and then
3.) by iterating through List_1,
4.) looking for adjacent element in L matching this cirteria ->. (obj1.b==obj2.a - 1) && (obj1.d == obj2.c - 4.1) if there is a match, overwriting in list2 ( obj1.b = obj2.b, obj1.d=obj2.d),
4.2) if no match copy element into List_2
But here is a problem. If in entire List_1 exits an element that match criteria (obj1.b==obj2.a - 1) && (obj1.d == obj2.c -1), but element is not adjacent element of for my current element (suppose acturally holding by iterator). I can't merge them.
I do not know how to transfer this logic into java programming code. That's all!!
Is it really, that much difficult for an "Expert"??
1.) I can sort List_1 asc,
2.) create a second List_2 and then
3.) by iterating through List_1,
4.) looking for adjacent element in L matching this cirteria ->. (obj1.b==obj2.a - 1) && (obj1.d == obj2.c - 4.1) if there is a match, overwriting in list2 ( obj1.b = obj2.b, obj1.d=obj2.d),
4.2) if no match copy element into List_2
But here is a problem. If in entire List_1 exits an element that match criteria (obj1.b==obj2.a - 1) && (obj1.d == obj2.c -1), but element is not adjacent element of for my current element (suppose acturally holding by iterator). I can't merge them.
I do not know how to transfer this logic into java programming code. That's all!!
Is it really, that much difficult for an "Expert"??
a - b - c - d
1.) 6 8 16 18
2.) 38 40 55 57
3.) 6 8 25 27
4.) 1 5 11 15
5.) 6 8 3 5
6.) 9 12 19 22
7.) 18 20 1 3
8.) 23 25 15 17
---- Sorted Order ----
4.) 1 5 11 15
5.) 6 8 3 5
1.) 6 8 16 18
3.) 6 8 25 27
6.) 9 12 19 22
7.) 18 20 1 3
8.) 23 25 15 17
2.) 38 40 55 57
------ expected result after merging------
4.) 1 12 11 22
5.) 6 8 3 5
3.) 6 8 25 27
7.) 18 20 1 3
8.) 23 25 15 17
2.) 38 40 55 57
ASKER
Hi mplungian, my issue is really trivial. Generally,
1.) I can sort List_1 asc,
2.) create a second List_2 and then
3.) by iterating through List_1,
4.) looking for adjacent element in L matching this cirteria
->. (obj1.b==obj2.a - 1) && (obj1.d == obj2.c -1)
4.1) if there is a match, overwriting in list2 ( obj1.b = obj2.b, obj1.d=obj2.d),
4.2) if no match add element into List_2 as new element.
But here is a problem.
If in entire List_1 exits an element that match criteria (obj1.b==obj2.a - 1) && (obj1.d == obj2.c -1) (line number 1 in example)
but existing element is not adjacent element of my current element (suppose iterator acturally holding them). So I can't merge them.
I do not know how to transfer this logic into java programming code. That's all!!
Is it really, that much difficult for an "Expert"??
1.) I can sort List_1 asc,
2.) create a second List_2 and then
3.) by iterating through List_1,
4.) looking for adjacent element in L matching this cirteria
->. (obj1.b==obj2.a - 1) && (obj1.d == obj2.c -1)
4.1) if there is a match, overwriting in list2 ( obj1.b = obj2.b, obj1.d=obj2.d),
4.2) if no match add element into List_2 as new element.
But here is a problem.
If in entire List_1 exits an element that match criteria (obj1.b==obj2.a - 1) && (obj1.d == obj2.c -1) (line number 1 in example)
but existing element is not adjacent element of my current element (suppose iterator acturally holding them). So I can't merge them.
I do not know how to transfer this logic into java programming code. That's all!!
Is it really, that much difficult for an "Expert"??
Erm... I am not a JAVA expert and I still after reading through your description several times do not quite see the logic without spending more time than I currently have. I just wanted to help you since no-one seemed to have made any comments.
ASKER
Anyway, thanks for trying...
YW. I pushed the "Request Attention" for you
This class will sort your objects :
import java.util.Comparator;
public class YourClass implements Comparator<YourClass> {
int x1, x2, y1, y2;
public int compare(YourClass a, YourClass b) {
if (a.x1 > b.x1) return 1;
if (a.x1 < b.x1) return -1;
// if we are here it means that a.x1 = b.x1, so we have to compare a.y1 and b.y1
if (a.y1 > b.y1) return 1;
if (a.y1 < b.y1) return -1;
// if we are here it means that a.y1 = b.y1, so a is equal to b
return 0;
}
}
import java.util.Comparator;
public class YourClass implements Comparator<YourClass> {
int x1, x2, y1, y2;
public int compare(YourClass a, YourClass b) {
if (a.x1 > b.x1) return 1;
if (a.x1 < b.x1) return -1;
// if we are here it means that a.x1 = b.x1, so we have to compare a.y1 and b.y1
if (a.y1 > b.y1) return 1;
if (a.y1 < b.y1) return -1;
// if we are here it means that a.y1 = b.y1, so a is equal to b
return 0;
}
}
Hm, it's interesting about merging...
ok, I'll send you solution tomorrow
ok, I'll send you solution tomorrow
Download the files, attached to this comment. This is what you need.
In your comment #33690322 in section ------ expected result after merging------
the first element shoul be 1 5 11 15, isn't it?
Good luck!
YourClass.java
YourClassComparator.java
In your comment #33690322 in section ------ expected result after merging------
the first element shoul be 1 5 11 15, isn't it?
Good luck!
YourClass.java
YourClassComparator.java
ASKER
Hi Valera!! First element after merging should be 1 12 11 22. If you keep a closer look on lines
4.) 1 5 11 15
1.) 6 8 16 18
6.) 9 12 19 22,
you'll see some order:
4.) 1-5 11-15,
1.) 6-8 16-18,
6.) 9-12 19-22,
Step 1:) 1 . . . 5, 6 . 8, 9 . . 12 and 11 . . . 15, 16 . 18, 19 . . 22.
Step 2:) 1 . . . . . . 8, 9 . . 12 and 11 . . . . . . 18, 19 . . 22.
Step 3:) 1 . . . . . . . . . . 12 and 11 . . . . . . . . . . 22.
That's all. some kind of merging that I have to do with a large ArrayList.
Thanks a lot for programming code example. Actually this gives me a great thought-provoking impulse.
4.) 1 5 11 15
1.) 6 8 16 18
6.) 9 12 19 22,
you'll see some order:
4.) 1-5 11-15,
1.) 6-8 16-18,
6.) 9-12 19-22,
Step 1:) 1 . . . 5, 6 . 8, 9 . . 12 and 11 . . . 15, 16 . 18, 19 . . 22.
Step 2:) 1 . . . . . . 8, 9 . . 12 and 11 . . . . . . 18, 19 . . 22.
Step 3:) 1 . . . . . . . . . . 12 and 11 . . . . . . . . . . 22.
That's all. some kind of merging that I have to do with a large ArrayList.
Thanks a lot for programming code example. Actually this gives me a great thought-provoking impulse.
Ahaaaaaaa! I got the point! Now I understood what merge means.
I was thinking that the equal elements must disapear from the list and that's all, but I was wrong...
Ok, I 'll try to rework the code I sent you.
I was thinking that the equal elements must disapear from the list and that's all, but I was wrong...
Ok, I 'll try to rework the code I sent you.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi 4xaoc,
The updated version does exactly what you need. Did you see it?
The updated version does exactly what you need. Did you see it?
This question has been classified as abandoned and is being closed as part of the Cleanup Program. See my comment at the end of the question for more details.
Perhaps you will find something useful here: http://www.google.com/search?q=java+sort+object
since it combines sorting with object manipulation which is what I think you need, e.g. create a compound object from your columns and sort them while manipulating them.