Solved

Best way to combine two Java arrays and create a new one with unique objects

Posted on 2010-08-16
15
557 Views
Last Modified: 2012-08-14
Hi,

I have two arrays of a user-define object type say outlinks.

Outlink[] a, b;
a = some function to initiate a;
b = some function to initiate b;

I would like to combine a and b and get a new array (say c) that has all unique Outlink objects of the two original arrays. Note please that there is an equal function define in the Outlink object already.

Thanks.
0
Comment
Question by:ddzz2010
[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
  • 5
  • 3
  • 3
  • +2
15 Comments
 
LVL 92

Expert Comment

by:objects
ID: 33451969
0
 

Author Comment

by:ddzz2010
ID: 33451994
object:

I saw the link but it is not very helpful. One thing I forgot to mention is that all elements in array A should stay in front of any new elements in array B. Sorry for missing that part.
0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 33452000
you can also use Set for the same

Outlink[] a, b;

Set set = new HashSet( Arrays.asList(a) );
set.addAll( Arrays.asList(b) );

Set automatically removes the duplicates
0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33452005
Outlink[] a, b;
//// a, b
Outlink[] c = new Outlink[a.length+b.length];
System.arraycopy(a, 0, c, 0, a.length);
System.arraycopy(b, 0, c, a.length, b.length);
Arrays.sort(c);
Outlink[] d = new Outlink[c.length];
int j=0;
for(int i=0;i<c.length;i++) {
if((i==0) || (!c[i].Equals(c[i-1]))) {
d[j++] = c[i];
}
}
Outlink[] e = new Outlink[j];
System.arraycopy(d, 0, e, 0, j);

// e is the result
0
 
LVL 92

Expert Comment

by:objects
ID: 33452028
> One thing I forgot to mention is that all elements in array A should stay in front of any new elements in array B. Sorry for missing that part.

the method i posted above does that
0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 33452114
to continue my answer

then you can convert set to array using

Outlink[] array = (Outlink[])set.toArray(new Outlink[set.size()]);

please check it should retain the order
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 250 total points
ID: 33452118
Try the following
	Set<Outlink> combined = new LinkedHashSet<Outlink>(Arrays.asList(a));
	for(Outlink add : b) {
	    if(!combined.contains(add)) {
		combined.add(add);
	    }
	}
	Outlink[] combinedArray = (Outlink[])new ArrayList<Outlink>(combined).toArray(new Outlink[combined.size()]);

Open in new window

0
 

Author Comment

by:ddzz2010
ID: 33454319
Hi, quite a few solutions mentioned above use such a function, Arrays.asList() on the Outlink array. However, when I tested the function, the Eclipse compiler shows error that Arrays.asList() can only be used to ArrayList<String> instead of ArrayList<Outlink>.

Am I missing something? Thanks.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 33454360
How did you use the code i posted?
0
 

Author Comment

by:ddzz2010
ID: 33455064
CEHJ, what do you think about your solution compared with cyberkiwi's?

Thanks.
0
 

Author Closing Comment

by:ddzz2010
ID: 33455192
Work like a charm. Thank you!
0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 33455568
@ddzz2010: why did you ignored my solution? doesn't it look similar to what i have posted?
0
 

Author Comment

by:ddzz2010
ID: 33456396
gurvinder372:

I didn't ignore. I tried your solution and I had the reported compile error as I mentioned. But when I tried CEH J's solution, I got no error and everything was smooth. I credited him with his solution.

Thanks.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 33457516
>>doesn't it look similar to what i have posted?

Unfortunately your approach won't guarantee the correct ordering

>>CEHJ, what do you think about your solution compared with cyberkiwi's?

Well i haven't got the time to read all that code of cyberwiki's ;)
0
 
LVL 92

Expert Comment

by:objects
ID: 33459985
> But when I tried CEH J's solution

which copied from gurvinder372's solution to iuse a Set.  You should have split the points
Though it would be preferred if CEHJ didn't copy other experts suggestions.
0

Featured Post

 Database Backup and Recovery Best Practices

Join Percona’s, Architect, Manjot Singh as he presents Database Backup and Recovery Best Practices (with a Focus on MySQL) on Thursday, July 27, 2017 at 11:00 am PDT / 2:00 pm EDT (UTC-7). In the case of a failure, do you know how long it will take to restore your database?

Question has a verified solution.

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

Article by: Nadia
Suppose you use Uber application as a rider and you request a ride to go from one place to another. Your driver just arrived at the parking lot of your place. The only thing you know about the ride is the license plate number. How do you find your U…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This video teaches viewers about errors in exception handling.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

617 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