aspose file tools*
The moose likes Java in General and the fly likes Making a collection of unique values Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Making a collection of unique values" Watch "Making a collection of unique values" New topic
Author

Making a collection of unique values

Dave Bosky
Ranch Hand

Joined: Dec 16, 2003
Posts: 72
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

Joined: Jan 10, 2002
Posts: 60055
    
  65

Moving to Java in General(intermediate).


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

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().


[Jess in Action][AskingGoodQuestions]
Dave Bosky
Ranch Hand

Joined: Dec 16, 2003
Posts: 72
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

Joined: Jul 08, 2003
Posts: 24166
    
  30

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

Joined: Dec 16, 2003
Posts: 72
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

Joined: Jul 08, 2003
Posts: 24166
    
  30

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Making a collection of unique values
 
Similar Threads
PriorityQueue issue
Difference between Java 5 adn Java 1.4
To the author: What to do on refactoring
Changing the text colour of JList items
JXTable: Display the default selected row in the scroll