This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Making a collection of unique values

 
Dave Bosky
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have two collections, collection 1 contains all items and collection 2 contains items selected. I need to create a new collection of only items that haven't been selected. What is the easiest method to create a new collection that only contains unselected items? A little snippet would be cool too.

Thanks,
Dave
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64848
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to Java in General(intermediate).
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The standard Collection classes have a "removeAll()" method which accepts another Collection as an argument; all the items in the first collection which appear in the second one are removed. So what you could do (if you need to preserve the full collection) is to construct a copy of it, and then use removeAll().
 
Dave Bosky
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried to use the "Collection.removeAll()" method but both of my collections contain a collection of beans. How will collection2 know to remove the items that appear in collection1? I'm obviously missing something here. I'm fairly new to Java so excuse my lack of knowledge.

collection2.removeAll(collection1);

Thanks,
Dave
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, let's see. How will removeAll() know to do this? Because that's what it does. I'm not sure how else to answer this tautological question. For the sake of discussion, here's a little example program. Is your situation substantially different from this?



This program prints "[a, d]".
 
Dave Bosky
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have two collections that each contain a collection ItemBeans.
Each ItemBean has 2 getters/setters - ID(int) and Name(String).

Collection c1 - contains All Items(3 items).
ex: bean1 = ID:1 Name:Rock
bean2 = ID:2 Name aper
bean3 = ID:3 Name:Scissors

Collection c2 - contains only selected items(1 item).
ex: bean1 = ID:1 Name:Rock

I need to remove Collection c2 from Collection c1.
c1.removeAll(c2);

In this case I need to remove (ID:1 Name:Rock) from my collection.

I'm not sure how else to explain it. Like I mentioned in an earlier post, I'm still a little green in the Java world.

Thanks Again,
Dave
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As long as there's just one "copy" of a given ItemBean, this will just work, as given. If there are multiple copies -- i.e., two physically different ItemBeans that both represent "bean1", then for this to work, the ItemBean class has to properly override the equals() method, so that this method returns true when two ItemBeans are the same. Whenever you override equals(), you're supposed to do hashCode too. So...



Now what I showed you should just work.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic