Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8310271

StringJoiner and String.join() are creating String duplicates

XMLWordPrintable

    • generic
    • generic

      A DESCRIPTION OF THE PROBLEM :
      In Java 11 StringJoiner treated the cases of zero and one element strings such no duplicates where created.
      In Java 17+ it not cares. Even if the result string is of zero length it not returns a shared String instance (unless in some constellations, if emptyValue has been set).
      In the application I am currently working on, this causes myriads of duplicate strings which not occurred in Java 11.

      REGRESSION : Last worked in version 11

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      In Java 17:
      "new StringJoiner("").toString()" returns a new empty String instance.
      "new StringJoiner("").add("foo").toString()" returns a dupe of the single input string "foo".

      In Java 11:
      "new StringJoiner("").toString()" returns the empty string constant.
      "new StringJoiner("").add("foo").toString()" returns the same String instance "foo".

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      At least the behaviour like in Java 11 should be restored.
      Perfect would be to also avoid all other cases of unnecessary duplications.

      CUSTOMER SUBMITTED WORKAROUND :
      Handcrafted treatment of special cases, but that's not always as simple, especially when using StringJoiner in a Collector.

      FREQUENCY : always


            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: