wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes HashSet and TreeMap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "HashSet and TreeMap" Watch "HashSet and TreeMap" New topic

HashSet and TreeMap

Raj Ohadi
Ranch Hand

Joined: Jun 30, 2006
Posts: 316
1. when we do TreeMap's remove(obj), does that remove

a) both key (obj) and value that maps to the key
b) only the value that maps to the key


2. If I have a HashSert set_1 and HashSet set_2, how can I find the objects that are in set_2 but not in set_1, i.e. set_2 - set_1 ?
I don't find such method.

Rob Spoor

Joined: Oct 27, 2005
Posts: 19655

The mapping is removed. This means both key and value. If you only want to remove the value, use "put(key, null)".

The second one can be obtained using addAll and removeAll:

With most collections you can combine the first two lines:

That second answer is in the API of Set#removeAll though:
Removes from this set all of its elements that are contained in the specified collection (optional operation). If the specified collection is also a set, this operation effectively modifies this set so that its value is the asymmetric set difference of the two sets.

[ October 02, 2008: Message edited by: Rob Prime ]

How To Ask Questions How To Answer Questions
Consider Paul's rocket mass heater.
subject: HashSet and TreeMap
Similar Threads
how to compare two Hashmap contents.
Difference in HashMap and HashSet
Is there a Properties-like class that allows multiple values per key?
which Collection?