4

I noticed that adding an element to a list does change its hash-key value and therefore it cannot be accessed again since .contains(modifiedObject) won't get a collision here. I did not expect that behavior to be honest. Makes me wonder how HashSet does its hashing .. So how can I make sure to not destroy my HashSet as I modify e.g. a list of strings as shown below. Is there a method to do that safe or is that just something I have to look out as a programmer?

private HashSet<List<String>> bagOfWordsMap = new HashSet<List<String>>();

private void createBagOfWordsList(UnifiedTag[] invalidTags) {


    for(List<String> sentences : getSentenceList()) {

        List<String> sentenceStemWords = new ArrayList<String>();

        // Not what you would want to do since sentenceStemWords is 
        // modified right after and bagOfWordsMap.contains(sentenceStemWords)
        // won't collide again:

        // bagOfWordsMap.add(sentenceStemWords);

        for(String word : sentences) {

            String stem = Stemmer.getStem(word);
            sentenceStemWords.add(stem);
        }   

        bagOfWordsMap.add(sentenceStemWords);           
    }
}
6
  • 1
    Have you tried overriding the equals and hashCode methods in some subclass of ArrayList that you've made? Mar 14, 2015 at 10:09
  • @christopher: what would be a useful/meaningful alternative definition of equals? Mar 14, 2015 at 10:11
  • Perhaps collating the values inside the list into some format? Mar 14, 2015 at 10:12
  • In C I might think of just hashing the objects start address in memory I don't know if that is smart but I guess that would be one way to ensure that you always get back the object you wanted to get access to later on. I am not sure if or how I could do such a thing in Java but overriding hashCode seems to be a possible solution for that. Mar 14, 2015 at 10:14
  • I have the feeling that the problem is that a Set is not the correct data structure for what you want. Why a Set and not something else? How do you get the lists from the Set?
    – Augusto
    Mar 14, 2015 at 10:16

2 Answers 2

2

Never use a mutable object as key in a map or set

Implement a frozen type that cannot be modified anymore if you want to prevent accidential modification!

  • fine print: it's technically okay to have mutable attributes on an object if they don't change the key, but you won't be able to access them easily by key in a java set, as there is no HashSet.get to get the current member, only a contains. Also, it's bad style and fragile. It's much better to split such objects into key, value.
2

One way is to use an UnmodifiableList<String> instead of a List<String> in your HashSet.

Another option is to use a HashMap<String,List<String>> instead of your HashSet<List<String>>, provided that you can associate some unique String key with each of your Lists.

7
  • Then I'm pretty sure OP won't be able to add it into the list in the first place? Mar 14, 2015 at 10:08
  • @christopher It would force him to remove a List from the HashSet and add a new List with the modified content. While this would have some performance cost, it would protect against corrupting the HashSet.
    – Eran
    Mar 14, 2015 at 10:10
  • Aaaah yes. Sorry, it's early! +1. Mar 14, 2015 at 10:12
  • Okay I guess that is also a valid solution but only in the case if I a) want to disallow manipulation of my List<String> and b) have to live with the fact that I am only allowed to add the object to the hash map after initializing it. Mar 14, 2015 at 10:16
  • @StefanFalk what HashMap? Your code uses a HashSet. If you keep the lists as the values in a HashMap (and decide on some unique immutable key), you'll be able to modify them after adding them to the Map.
    – Eran
    Mar 14, 2015 at 10:26

Your Answer

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

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