Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 561
  • Last Modified:

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

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
ddzz2010
Asked:
ddzz2010
  • 5
  • 3
  • 3
  • +2
1 Solution
 
ddzz2010Author Commented:
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
 
Gurvinder Pal SinghCommented:
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
cyberkiwiCommented:
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
 
objectsCommented:
> 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
 
Gurvinder Pal SinghCommented:
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
 
CEHJCommented:
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
 
ddzz2010Author Commented:
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
 
CEHJCommented:
How did you use the code i posted?
0
 
ddzz2010Author Commented:
CEHJ, what do you think about your solution compared with cyberkiwi's?

Thanks.
0
 
ddzz2010Author Commented:
Work like a charm. Thank you!
0
 
Gurvinder Pal SinghCommented:
@ddzz2010: why did you ignored my solution? doesn't it look similar to what i have posted?
0
 
ddzz2010Author Commented:
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
 
CEHJCommented:
>>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
 
objectsCommented:
> 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 3
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now