?
Solved

Java merging and sort list of objects

Posted on 2010-09-15
17
Medium Priority
?
649 Views
Last Modified: 2013-11-23
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:

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

Open in new window

Sampletable-4int-withou-hypen.txt
0
Comment
Question by:4xaoc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 3
  • +1
17 Comments
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 33688900
Hi, I do not have the time to understand your issue with seems not trivial.

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.
0
 

Author Comment

by:4xaoc
ID: 33690322
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"??
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

Open in new window

0
 

Author Comment

by:4xaoc
ID: 33690384
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"??
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 33690972
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.
0
 

Author Comment

by:4xaoc
ID: 33691007
Anyway, thanks for trying...
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 33691077
YW. I pushed the "Request Attention" for you
0
 
LVL 16

Expert Comment

by:Valeri
ID: 33691905
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;
    }

}
0
 
LVL 16

Expert Comment

by:Valeri
ID: 33696941
Hm, it's interesting about merging...
ok, I'll send you solution tomorrow
0
 
LVL 16

Expert Comment

by:Valeri
ID: 33698860
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
0
 

Author Comment

by:4xaoc
ID: 33700760
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.
0
 
LVL 16

Expert Comment

by:Valeri
ID: 33700918
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.
0
 
LVL 16

Accepted Solution

by:
Valeri earned 2000 total points
ID: 33711840
This is the updated version of YourClass.java, the other file is the same.
equals() method has small changes.
merge() method has two loops
1. outer loop - from second element to the end of the list
2. inner loop(!) - from the previous element of current one (from outer loop) to the first element of the list - that merges the elements!
Good luck!
YourClass.java
0
 
LVL 16

Expert Comment

by:Valeri
ID: 33789052
Hi 4xaoc,
The updated version does exactly what you need. Did you see it?
0
 
LVL 29

Expert Comment

by:leonstryker
ID: 35221159
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.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Lotus Notes has been used since a very long time as an e-mail client and is very popular because of it's unmatched security. In this article we are going to learn about  RRV Bucket corruption and understand various methods to Fix "RRV Bucket Corrupt…
In today's business world, data is more important than ever for informing marketing campaigns. Accessing and using data, however, may not come naturally to some creative marketing professionals. Here are four tips for adapting to wield data for insi…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question