This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Building Blockchain Apps and have Michael Yuan on-line!
See this thread for details.
Win a copy of Building Blockchain Apps this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

StringBuffer as key in TreeMap

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can we make StringBuffer as key in TreeMap? If yes, then what are the challenges seen while doing so? Can a sample code be provided showing StringBuffer added as key for a TreeMap?
 
Ranch Foreman
Posts: 129
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A properly made TreeMap will use generic types for the key and value - so why wouldn't you be able to use a StringBuffer as a key? Are there any special restrictions you can think of for the generic types on a TreeMap?
 
Marshal
Posts: 6869
182
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The technical answer is "yes," but I question why you would want to do so.  Typically your keys don't change in a map, so why use StringBuffer over just String?  You can always create a StringBuffer from the key if you need it.
So why do you want a StringBuffer as a key?

 
Bartender
Posts: 3776
154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wonder most what Values woud be used. But if the Value is of type V, and you have a Map<StringBuffer, V>, then you can do like:
 
Piet Souris
Bartender
Posts: 3776
154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops: I forgot to mention that a TreeMap either needs Comparable Keys, or you must specify a Comparator for the Keys. So, do you have a Comparator in mind?
 
Saloon Keeper
Posts: 11488
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Honestly, it's a really bad idea. Keys must not alter in a meaningful way while the tree is used, and in that case you might as well use String, as Knute pointed out.

I see absolutely no use for this.
 
Mary Rani
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't realize that we can use StringBuffer as a Key in TreeMap, until I was asked in an interview. As a developer, I have used String.

So in this scenario, I understand that Comparable or Comparator needs to be implemented [ as Piet has mentioned ]. Thanks for clarifying this.

I am looking for an example that does this. Can someone provide a sample code?
 
Sheriff
Posts: 15043
252
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The point of the interview question really was not to answer "can" but "should" -- the correct answer is what Knute and Stephan said. The tack you're going on will lose you the job.
 
Marshal
Posts: 68111
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can in theory use any reference type as the “K” in any Map. The Map will only work if it has correctly‑overridden hashCode() (Hash‑based maps only) and equals() methods and (sorted maps only) a means of comparison, which must be consistent with equals(). Since the StringBuilder#compareTo() isn't consistent with equals, you will get incorrect behaviour. Run those three put() calls twice and see how many elements you have. Then change it to a hash map and repeat the procedure.

If StringBuilder doesn't override equals(), why on earth does it have a compareTo() method?

[edit] You should of course use new StringBuilder("Campbell") as the key rather than the plain simple String. Also see what happens when you try get(). Alter the state of some of the Ks with insert(0, "something") and see what happens.
 
Stephan van Hulst
Saloon Keeper
Posts: 11488
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:If StringBuilder doesn't override equals(), why on earth does it have a compareTo() method?


To provide a way to perform lexicographical comparisons while still maintaining the notion that StringBuffers are not actually value types.

Was it the right way to do this? Probably not. Classes that can't make their compareTo() method consistent with equals() probably shouldn't implement Comparable but should rather have a method that returns a Comparator.
 
Campbell Ritchie
Marshal
Posts: 68111
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, I don't think it was right either, but I have only just noticed that. I would have preferred StringBuilder to have equals() and hashCode() overridden too. If you aren't performing equality comparisons, what business have you got performing difference comparisons, which is what compareTo() and compare() do?

BigDecimal has compareTo() not consistent with equals(), but I can't see a problem with that.
it
By the way, OP: did you ask why they were using StringBuffer at all, not StringBuilder?
 
Mary Rani
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, I haven't .

Is there a noticeable difference when using StringBuffer vs StringBuilder as key? I see both have the compareTo() method supported.
 
Campbell Ritchie
Marshal
Posts: 68111
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to work out the difference between a StringBuilder and a StringBuffer. And work out why somebody was asking about StringBuffer in an interview any time since 2010.

Don't know aboiut performance, but any difference will be slight.
 
Piet Souris
Bartender
Posts: 3776
154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To clarify my reply: I am using java 10, where StringBuilder and StringBuffer did NOT implement Comparable. It did not occur to me that they would, in later java versions.
 
Campbell Ritchie
Marshal
Posts: 68111
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So we can't blame that bit of daftness on early programmers in Java1.0.2?

That method says, “Since 11”.
 
Junilu Lacar
Sheriff
Posts: 15043
252
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to reiterate what I said earlier. Since this was an interview question I doubt the point was whether or not you could. It's kind of a trap question if you ask me. If I was doing the interviewing and the candidate didn't point out the issues noted by Knute and Stephan then that tells me you probably dont know much about how map keys work and the issues involved with using mutable objects as keys.

[Edit] and this is the trap part: "If yes, then what are the challenges seen while doing so?" -- If you let yourself get distracted by the "Can you do it?" part, you're going to fall into the trap.
 
Did you just should on me? You should read this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!