I have the following code where I am trying to put the StringBuffer objects as keys in a TreeSet. The reason I do this is to see if I can put mutable objects as keys. I do n
Yes, you can but as the above answers state, you must write a Comparator.
But the real question is why would you want to? The purpose of a StringBuffer is to modify the state while creating a string. Since it is a key in your SortedMap you shouldn't be modifying the key, so there is no point in saving the StringBuffer. What you want to do is call StringBuffer.toString() which returns a String and use the String as your key.