Win a copy of Mastering Corda: Blockchain for Java Developers 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
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

StringBuilder Now Implements Comparable in JDK 11+ -- is this good or bad?

 
Ranch Foreman
Posts: 175
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I was first learning Java, the very thought of immutability was negative to me.  All I could think was "I can't change this??  I wanna change this!!"

I have a relatively extremely healthy attitude towards immutability now.
It allows pooling and saves memory.
It make multi-threading so much safer...
It lets me safely use things as keys in Maps and as elements in Sets.

It is great.

On a page with 128_000 hits (popular YouTube Channel) helpful people are celebrating the wonderful news that starting with JDK 11 you can now use StringBuilder and StringBuffer objects in cool new ways because they now implement Comparable<StringBuilder>....

I remember being disappointed at one point that I couldn't do some things with StringBuffer/StringBuilder and memorizing that if you really want to answer questions about the lexical ordering of two StringBuilder objects just call .toString on them in the compare....

Is there some good use case for JDK 11+ making StringBuffer/StringBuilder implement Comparable or we just going to see a lot more broken TreeSet and TreeMap objects in the ecosystems including newbies and careless developers who then go ahread and chuck them in there and then proceed to modify them after, as people are wont to do with StringBuilders?
 
Marshal
Posts: 71682
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, the whole idea of a StringBuilder is to modify its state. Yes, you will break tree maps and tree sets. I can't see why they made a class which doesn't override equals() implement Comparable. In a few years, shall we hear people saying, “Well, it seemed like a good idea at the time”?
 
Marshal
Posts: 26291
80
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesse Silverman wrote:I remember being disappointed at one point that I couldn't do some things with StringBuffer/StringBuilder and memorizing that if you really want to answer questions about the lexical ordering of two StringBuilder objects just call .toString on them in the compare....



Those classes have always (since Java 1.4 anyway) implemented CharSequence, and there's a static method CharSequence.compare(CharSequence, CharSequence) which would have allowed you to compare two StringBuilder objects lexicographically.

I don't really have an opinion on the question which is your main point, though.
 
Campbell Ritchie
Marshal
Posts: 71682
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I never realised they aleady had that compare() method. Shows how long it is since I last read CharSequence. I suppose that makes it another case of, “Don't use mutable types as keys or as . . .”
 
Saloon Keeper
Posts: 12608
273
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I only very rarely have need for a StringBuilder, and even then I never need to compare two of them.

In short, I don't care.
 
permaculture is giving a gift to your future self. After reading this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic