Well, a Map or Set assumes that the keys (for Map) or values (for Set) will not change. And the entire purpose of a StringBuffer is to change its content. Which makes them extremely poor choices to use in a Map or Set - if you change the content of a StringBuffer after you use it as a key in a Map, the map won't work correctly. There are two basic options:
1. If you're dong changing the content of the StringBuffer, use toString() to convert it to an immutable String, and use the String as the key in your Map.
2. If you're not done changing the content of the StringBuffer, don't even try to use it as a key in a Map. It will only cause you pain.
"I'm not back." - Bill Harding, Twister
Joined: May 19, 2008
hi Jim ,
do you mean we should use only immutable object in Hashmap or Hashset? lot of time we 'll use our custom class (Eg: Student.class) objects as key in hashmap which is mutable which can be modified even after put in collection. developer has to take care of this.
this shouldn't be the reason for not implementing equals in Stringbuffer