Home Merge two ArrayLists with no duplicate subclasses
Reply: 0

Merge two ArrayLists with no duplicate subclasses

user1015
1#
user1015 Published in June 19, 2018, 11:53 pm

Given:

public abstract class Cars {}

...

public class Ford extends Cars {}

...

public class Dodge extends Cars {}

...

public class Volkswagen extends Cars {}

...

If I have two ArrayList objects:

List<Cars> dealer1 = new ArrayList<>;
List<Cars> dealer2 = new ArrayList<>;

dealer1.addAll(asList(new Ford("ford1"), new Dodge("dodge1")));
dealer2.addAll(asList(new Dodge("dodge2"), new Volkswagen("vw1")));

I then want to create a merged list from the two with only one instance of each subclass, such that:

dealerMerged = ["ford1", "dodge1", "vw1"]

OR

dealerMerged = ["ford1", "dodge2", "vw1"]

It doesn't matter which instance makes it into the merged list.

Is this possible? I had a search through and saw something about using Set but that seems to only ensure unique references, unless I've badly misunderstood something.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.311806 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO