5

Edit: explained the problem properly now.

I have a hashmap where i want to store sets of words seen together (key) and the lines in which they were seen together(value). This is the structure i came up with:

HashMap<HashSet<String>, HashSet<Integer>> hm= ...

for inputs:

  1. mango, banana, apple

  2. apple, banana

  3. peach, walrus

  4. walrus, peach

As I read this, line by line, I make new temporary keys (hashsets not yet inserted into hashmap) from the combination of words in the line. Each temporary key is a hashset of a subset of the words in the line. If a temporary key already exists in my hashmap, which i check by

if(hashmap.containsKey(hashset))

i simply add the new line to that key's corresponding value, if not, I make a new entry in the hashmap and take care of it.

At no point do i change an existing key. I only update their corresponding values in the hasmmap.

my hashmap, at the end of reading the file, should look something like this

[apple, banana]=[1,2]

[peach, walrus]=[3,4]

...

the problem is that the

if(hashmap.containsKey(hashset))

piece of code doesn't always detect existing keys. Why is this? Is this structure not allowed?

Thank you

5
  • Wouldn't line 5 be mapped to peach, walrus as well? And what about mango ?
    – Thomas
    Mar 19, 2012 at 16:25
  • @pcalcao yes. it behaves strangely. sometimes it detects the set to exist, sometimes not. I just want validation of whether HashMap can handle hashset has a key.
    – student101
    Mar 19, 2012 at 16:25
  • @Thomas. sorry, typo. removed it.
    – student101
    Mar 19, 2012 at 16:27
  • You can make it work for you.. Mar 19, 2012 at 16:29
  • If you doing a one-on-one mapping to key and value then definitely its a bad idea. As the ordering is not guaranteed in the set. For Ex if you are planning to map apple to 1 and banana to 2 this data structure is not going to work.
    – Prasanna
    Mar 19, 2012 at 16:31

3 Answers 3

8

This should work, but you need to watch out for mutability of the keys. If you ever change the contents of one of the keys, its hashcode will change, and your map will start doing strange things. From the javadoc for Map:

Note: great care must be exercised if mutable objects are used as map keys. The behavior of a map is not specified if the value of an object is changed in a manner that affects equals comparisons while the object is a key in the map. A special case of this prohibition is that it is not permissible for a map to contain itself as a key. While it is permissible for a map to contain itself as a value, extreme caution is advised: the equals and hashCode methods are no longer well defined on such a map.

To avoid this, wrap the keys with Collections.unmodifiableSet() immediately upon creation, or just use ImmutableSet from Guava.

1
  • @matt hi, i've rephrased the question now. It wasn't very clearly explained. At no point do i change the keys, i only update the values to which the point to in the hashmap.
    – student101
    Mar 19, 2012 at 17:20
4

You can, but once you have added a HashSet as a key to a HashMap you shouldn't modify it again, as the HashSet.hashCode() might change and you'll never find your HashSet again. In other words, if you're doing something like that, be sure that your keys are immutable HashSets (see also Matt's answer here)

An alternative is to use the MultiKeyMap along with a MultiKey from commons collections

11
  • +1 by me but why should he use an alternative?He could just overide the equals and hashcode in hashset
    – Cratylus
    Mar 19, 2012 at 16:41
  • @user384706: The OP asked for a possible alternative
    – Lukas Eder
    Mar 19, 2012 at 16:41
  • Ok but he could just extend hashset.I can't think of a reason why it wouldn't suffice
    – Cratylus
    Mar 19, 2012 at 16:42
  • @user384706: There's no need to extend HashSet... But why don't you post an answer illustrating what you mean?
    – Lukas Eder
    Mar 19, 2012 at 16:44
  • 1
    Ok, I posted something.Perhaps I am not seeing something correctly
    – Cratylus
    Mar 19, 2012 at 16:51
1

The problem you have is well explained by @Lukas ans @Matt.
I think you could get away by using extending or using a decorator pattern to create a Hashset that overides equals and hashCode in a way that is independent of the contents.

This way you can avoid introducing dependencies on third party jars just for a specific problem

2
  • That's an interesting option! It's hard to tell from the OP's text, whether it will produce the wanted results, though...
    – Lukas Eder
    Mar 19, 2012 at 16:56
  • I was just wondering if there is some pitfall I am not seeing.IMHO it is good to avoid introducing extra libraries if you don't really use them
    – Cratylus
    Mar 19, 2012 at 17:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.