{"id":681,"date":"2023-11-29T09:01:01","date_gmt":"2023-11-29T01:01:01","guid":{"rendered":""},"modified":"2023-11-29T09:01:01","modified_gmt":"2023-11-29T01:01:01","slug":"\u62fc\u63a5\u5b57\u7b26\u4e32\u65b0\u59ff\u52bf\u2014StringJoiner","status":"publish","type":"post","link":"https:\/\/mushiming.com\/681.html","title":{"rendered":"\u62fc\u63a5\u5b57\u7b26\u4e32\u65b0\u59ff\u52bf\u2014StringJoiner"},"content":{"rendered":"

\u8fd9\u662f\u6211\u53c2\u4e0e\u66f4\u6587\u6311\u6218\u7684\u7b2c4\u5929\uff0c\u6d3b\u52a8\u8be6\u60c5\u67e5\u770b\uff1a \u66f4\u6587\u6311\u6218<\/p>\n

\u524d\u8a00<\/h2>\n

\u4f60\u77e5\u9053\u4f7f\u7528\u539f\u751f JDK \u62fc\u63a5\u5b57\u7b26\u4e32\u6709\u591a\u5c11\u79cd\u73a9\u6cd5\u5417\uff1f\u6211\u4eec\u80fd\u60f3\u5230\u7684\u5927\u6982\u5c31\u662fStringBuilder\u3001StringBuffer\u3001 String.concat\u3001\u8fd8\u6709\u7528 + \u62fc\u63a5\u3002\u8fd8\u6709\u5176\u4ed6\u7684\u5417\uff1f\u63a5\u4e0b\u6765\u5c31\u6765\u8bb2\u8bb2 Java 8 \u91cc\u9762\u65b0\u589e\u4e86\u4e00\u4e2a\u62fc\u63a5\u5b57\u7b26\u4e32\u7684\u7c7b \uff0c\u53eb StringJoiner \u7684\u7528\u6cd5\u3002<\/p>\n

\u6982\u8ff0<\/h2>\n

StringJoiner \u662f java.util \u5305\u4e2d\u7684\u4e00\u4e2a\u7c7b\uff0c\u7528\u4e8e\u6784\u9020\u4e00\u4e2a\u7531\u5206\u9694\u7b26\u5206\u9694\u7684\u5b57\u7b26\u4e32\uff0c\u5e76\u4e14\u53ef\u4ee5\u4ece\u63d0\u4f9b\u7684\u524d\u7f00\u5b57\u7b26\u4e32\u5f00\u5934\uff0c\u4ee5\u63d0\u4f9b\u7684\u540e\u7f00\u5b57\u7b26\u4e32\u7ed3\u5c3e\u3002\u867d\u7136\u8fd9\u4e9b\u529f\u80fd\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 StringBuilder \u6216\u8005 StringBuffer \u6765\u5b9e\u73b0\u7684\uff0c \u4f46\u662f StringJoiner \u63d0\u4f9b\u7684\u65b9\u6cd5\u66f4\u7b80\u5355\uff0c\u800c\u4e14\u4e5f\u4e0d\u9700\u8981\u5199\u5927\u91cf\u7684\u4ee3\u7801\u5c31\u80fd\u6210\u3002<\/p>\n

\u6784\u9020\u51fd\u6570<\/h2>\n

StringJoiner \u4e00\u5171\u6709 2 \u4e2a\u6784\u9020\u51fd\u6570\u3002 \u6784\u9020\u51fd\u6570\u5f88\u7b80\u5355\uff0c\u5c31\u662f\u5bf9 \u5206\u9694\u7b26\uff0c\u524d\u7f00\u548c\u540e\u7f00\u5b57\u7b26\u4e32\u7684\u521d\u59cb\u5316\u3002<\/p>\n

    public<\/span> StringJoiner<\/span>(CharSequence delimiter)<\/span> {\n        this<\/span>(delimiter, \"\"<\/span>, \"\"<\/span>);\/\/\u9ed8\u8ba4\u524d\u7f00\u548c\u540e\u7f00\u4e3a\"\",\u91cd\u8f7d\u8c03\u7528<\/span>\n    }\n\n    public<\/span> StringJoiner<\/span>(CharSequence delimiter, CharSequence prefix, CharSequence suffix)<\/span> {\n        \/\/\u95f4\u9694\u7b26\uff0c\u524d\u7f00\u548c\u540e\u7f00\u5224\u65ad\u662f\u5426\u4e3anull\uff0cnull\u5c06\u629b\u51fa\u5f02\u5e38<\/span>\n        Objects.requireNonNull(prefix, \"The prefix must not be null\"<\/span>);\n        Objects.requireNonNull(delimiter, \"The delimiter must not be null\"<\/span>);\n        Objects.requireNonNull(suffix, \"The suffix must not be null\"<\/span>);\n        \/\/\u6210\u5458\u53d8\u91cf\u8d4b\u503c<\/span>\n        this<\/span>.prefix = prefix.toString();\n        this<\/span>.delimiter = delimiter.toString();\n        this<\/span>.suffix = suffix.toString();\n        \/\/\u7a7a\u503c\u88ab\u8bbe\u7f6e\u4e3a\u53ea\u6709\u524d\u540e\u7f00<\/span>\n        this<\/span>.emptyValue = this<\/span>.prefix + this<\/span>.suffix;\n    }\n<\/code><\/pre>\n

\u7528\u6cd5<\/h2>\n

StringJoiner \u7684\u7528\u6cd5\u5176\u5b9e\u5f88\u7b80\u5355\uff0c\u6765\uff0c\u6211\u4eec\u770b\u4e00\u4e0b StringJoiner \u5b57\u7b26\u4e32\u62fc\u63a5\u7684\u7528\u6cd5\u3002<\/p>\n

public<\/span> class<\/span> StringJoinerTest<\/span> {\n    public<\/span> static<\/span> void<\/span> main<\/span>(String[] args)<\/span> {\n        StringJoiner<\/span> sj<\/span> =<\/span> new<\/span> StringJoiner<\/span>(\"hello\"<\/span>);\n        sj.add(\"Java 8 \"<\/span>);\n        sj.add(\"Java 11\"<\/span>);\n        System.out.println(sj.toString());\n        StringJoiner<\/span> sj1<\/span> =<\/span> new<\/span> StringJoiner<\/span>(\":\"<\/span>,\"[\"<\/span>,\"]\"<\/span>);\n        sj1.add(\"Java8\"<\/span>).add(\"Java11\"<\/span>).add(\"Java15\"<\/span>);\n        System.out.println(sj1.toString());\n    }\n}\n<\/code><\/pre>\n

\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n

Java 8<\/span> helloJava 11<\/span>\n[Java8:Java11:Java15]\n<\/code><\/pre>\n

\u6ce8\u610f\uff1a<\/code><\/p>\n

    \n
  1. \u5f53\u6211\u4eec\u4f7f\u7528StringJoiner(CharSequence delimiter)\u521d\u59cb\u5316\u4e00\u4e2aStringJoiner\u7684\u65f6\u5019\uff0c\u8fd9\u4e2adelimiter\u5176\u5b9e\u662f\u5206\u9694\u7b26\uff0c\u5e76\u4e0d\u662f\u5b57\u7b26\u4e32\u7684\u521d\u59cb\u503c\u3002<\/li>\n
  2. StringJoiner(CharSequence delimiter,CharSequence prefix,CharSequence suffix)\u7684\u7b2c\u4e8c\u4e2a\u548c\u7b2c\u4e09\u4e2a\u53c2\u6570\u5206\u522b\u662f\u62fc\u63a5\u540e\u7684\u5b57\u7b26\u4e32\u7684\u524d\u7f00\u548c\u540e\u7f00\u3002<\/li>\n<\/ol>\n

    \u6e90\u7801\u5206\u6790<\/h2>\n
    \n

    JDK8\u4e2d\u6e90\u7801<\/p>\n<\/blockquote>\n

      public<\/span> StringJoiner setEmptyValue<\/span>(CharSequence emptyValue)<\/span> {\n        this<\/span>.emptyValue = Objects.requireNonNull(emptyValue,\n            \"The empty value must not be null\"<\/span>).toString();\n        return<\/span> this<\/span>;\n    }\n\n    @Override<\/span>\n    public<\/span> String toString<\/span>()<\/span> {\n        if<\/span> (value == null<\/span>) {\n            return<\/span> emptyValue;\n        } else<\/span> {\n            if<\/span> (suffix.equals(\"\"<\/span>)) {\n                return<\/span> value.toString();\n            } else<\/span> {\n                int<\/span> initialLength<\/span> =<\/span> value.length();\n                String<\/span> result<\/span> =<\/span> value.append(suffix).toString();\n                value.setLength(initialLength);\n                return<\/span> result;\n            }\n        }\n    }\n\n    public<\/span> StringJoiner add<\/span>(CharSequence newElement)<\/span> {\n        prepareBuilder().append(newElement);\n        return<\/span> this<\/span>;\n    }\n\n    public<\/span> StringJoiner merge<\/span>(StringJoiner other)<\/span> {\n        Objects.requireNonNull(other);\n        if<\/span> (other.value != null<\/span>) {\n            final<\/span> int<\/span> length<\/span> =<\/span> other.value.length();\n            StringBuilder<\/span> builder<\/span> =<\/span> prepareBuilder();\n            builder.append(other.value, other.prefix.length(), length);\n        }\n        return<\/span> this<\/span>;\n    }\n\n    private<\/span> StringBuilder prepareBuilder<\/span>()<\/span> {\n        if<\/span> (value != null<\/span>) {\n            value.append(delimiter);\n        } else<\/span> {\n            value = new<\/span> StringBuilder<\/span>().append(prefix);\n        }\n        return<\/span> value;\n    }\n\n\n    public<\/span> int<\/span> length<\/span>()<\/span> {\n        return<\/span> (value != null<\/span> ? value.length() + suffix.length() :\n                emptyValue.length());\n    }\n<\/code><\/pre>\n
    \n

    JDK11\u4e2d\u6e90\u7801<\/p>\n<\/blockquote>\n

    public<\/span> final<\/span> class<\/span> StringJoiner<\/span> {\n    private<\/span> String[] elts;\n    \n    @Override<\/span>\n    public<\/span> String toString<\/span>()<\/span> {\n        final<\/span> String[] elts = this<\/span>.elts;\n        if<\/span> (elts == null<\/span> && emptyValue != null<\/span>) {\n            return<\/span> emptyValue;\n        }\n        final<\/span> int<\/span> size<\/span> =<\/span> this<\/span>.size;\n        final<\/span> int<\/span> addLen<\/span> =<\/span> prefix.length() + suffix.length();\n        if<\/span> (addLen == 0<\/span>) {\n            compactElts();\n            return<\/span> size == 0<\/span> ? \"\"<\/span> : elts[0<\/span>];\n        }\n        final<\/span> String<\/span> delimiter<\/span> =<\/span> this<\/span>.delimiter;\n        final<\/span> char<\/span>[] chars = new<\/span> char<\/span>[len + addLen];\n        int<\/span> k<\/span> =<\/span> getChars(prefix, chars, 0<\/span>);\n        if<\/span> (size > 0<\/span>) {\n            k += getChars(elts[0<\/span>], chars, k);\n            for<\/span> (int<\/span> i<\/span> =<\/span> 1<\/span>; i < size; i++) {\n                k += getChars(delimiter, chars, k);\n                k += getChars(elts[i], chars, k);\n            }\n        }\n        k += getChars(suffix, chars, k);\n        return<\/span> new<\/span> String<\/span>(chars);\n    }\n    public<\/span> StringJoiner add<\/span>(CharSequence newElement)<\/span> {\n        final<\/span> String<\/span> elt<\/span> =<\/span> String.valueOf(newElement);\n        if<\/span> (elts == null<\/span>) {\n            elts = new<\/span> String<\/span>[8<\/span>];\n        } else<\/span> {\n            if<\/span> (size == elts.length)\n                elts = Arrays.copyOf(elts, 2<\/span> * size);\n            len += delimiter.length();\n        }\n        len += elt.length();\n        elts[size++] = elt;\n        return<\/span> this<\/span>;\n    }\n}\n<\/code><\/pre>\n

    \u6bd4\u8f83\u4e00\u4e0bJDK8\u548cJDK11\u4e2dStringJoiner\u7684\u6e90\u7801add \u65b9\u6cd5\u7684\u5b9e\u73b0\uff1a<\/p>\n

      \n
    1. \n

      JDK8\u4e2dStringJoiner \u662f\u901a\u8fc7 StringBuilder \u6765\u5b9e\u73b0\u7684\uff1b<\/p>\n<\/li>\n

    2. \n

      JDK11\u4e2dStringJoiner \u5c31\u662f\u628a\u5f85\u62fc\u63a5\u7684\u5b57\u7b26\u4e32\uff0c\u653e\u5230\u4e00\u4e2a\u5b57\u7b26\u4e32\u6570\u7ec4\u91cc\u9762\uff0ctoString() \u65b9\u6cd5\u7684\u65f6\u5019\uff0c\u624d\u662f\u771f\u6b63\u505a\u5b57\u7b26\u4e32\u62fc\u63a5\u7684\u8fc7\u7a0b\u3002<\/p>\n<\/li>\n<\/ol>\n

      \u90a3\u4e48\u95ee\u9898\u6765\u4e86\uff0c\u65e2\u7136 JDK 8 \u7684\u65f6\u5019\uff0c\u5df2\u7ecf\u4f7f\u7528\u4e86StringBuilder \u6765\u5b9e\u73b0\uff0cJDK11\u4e3a\u4ec0\u4e48\u8fd8\u8981\u6539\u6210 String[] \u6765\u7f13\u5b58\u6240\u6709\u7684\u5f85\u62fc\u63a5\u7684\u5b57\u7b26\u4e32\u3002\u8fd9\u4e2a\u5c31\u8981\u6d89\u53ca\u5230JVM\u5e95\u5c42\u7684\u4f18\u5316\u4e86\u3002<\/p>\n

      \u65e2\u7136\u5df2\u7ecf\u6709\u4e86StringBuilder\uff0c\u4e3a\u4ec0\u4e48\u8fd8\u8981\u9020\u4e00\u4e2aStringJoiner\uff0c\u5b83\u7684\u4f18\u52bf\u5230\u5e95\u5728\u54ea\u91cc\uff0c\u90a3\u6211\u4eec\u518d\u6765\u627e\u627e\u539f\u56e0\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u4ee3\u7801\u7684\u6ce8\u91ca\u5757\u4e2d\uff0c\u6807\u8bb0\u4e86 Collectors#joining \u3002<\/p>\n

      \"\u62fc\u63a5\u5b57\u7b26\u4e32\u65b0\u59ff\u52bf\u2014StringJoiner<\/p>\n

      \u6211\u4eec\u770b\u770b Collectors#joining \u7a76\u7adf\u505a\u4e86\u4ec0\u4e48\u5462\uff1f<\/p>\n

          public<\/span> static<\/span> Collector<CharSequence, ?, String> joining() {\n        return<\/span> new<\/span> CollectorImpl<\/span><CharSequence, StringBuilder, String>(\n                StringBuilder::new<\/span>, StringBuilder::append,\n                (r1, r2) -> { r1.append(r2); return<\/span> r1; },\n                StringBuilder::toString, CH_NOID);\n    }\n\n    public<\/span> static<\/span> Collector<CharSequence, ?, String> joining(CharSequence delimiter) {\n        return<\/span> joining(delimiter, \"\"<\/span>, \"\"<\/span>);\n    }\n\n\n    public<\/span> static<\/span> Collector<CharSequence, ?, String> joining(CharSequence delimiter,\n                                                             CharSequence prefix,\n                                                             CharSequence suffix) {\n        return<\/span> new<\/span> CollectorImpl<\/span><>(\n                () -> new<\/span> StringJoiner<\/span>(delimiter, prefix, suffix),\n                StringJoiner::add, StringJoiner::merge,\n                StringJoiner::toString, CH_NOID);\n    }\n<\/code><\/pre>\n

      \u539f\u6765 Java 8 \u4e2dStream \u662f\u501f\u52a9\u4e86 StringJoiner \u6765\u5b9e\u73b0\u7684\u3002 \u8fd9\u4e2a\u65f6\u5019\uff0c\u6211\u4eec\u53ef\u80fd\u4f1a\u60f3\uff0c\u4e3a\u4ec0\u4e48\u4e0d\u4f7f\u7528 StringBuilder \u6765\u5b9e\u73b0\u5462\uff1f \u6211\u4eec\u53ef\u4ee5\u4ece\u793a\u4f8b\u91cc\u770b\u51fa\uff0c\u5982\u679c StringBuilder \u6765\u6784\u9020\u62fc\u63a5\u7684\u8bdd\uff0c\u5728\u6ca1\u6709\u524d\u540e\u7f00\u7684\u60c5\u51b5\u4e0b\uff0c\u5e94\u8be5\u8fd8\u662f\u7b80\u5355\u7684\uff0c\u4f46\u662f\u4e00\u65e6\u5230\u9700\u8981\u5176\u4ed6\u524d\u540e\u7f00\u7684\u62fc\u63a5\uff0c\u90a3\u8fd9\u4e2a\u5c31\u53d8\u5f97\u5f88\u590d\u6742\u5566\u3002<\/p>\n

      \u6240\u4ee5 StringJoiner \u5728 Java 8 \u7684\u5730\u4f4d\u662f StringBuilder \u6240\u4e0d\u80fd\u4ee3\u66ff\u7684\u3002<\/p>\n

      \u603b\u7ed3<\/h2>\n

      \u672c\u6587\u4ecb\u7ecd\u4e86 Java 8 \u5f00\u59cb\u63d0\u4f9b\u7684\u5b57\u7b26\u4e32\u62fc\u63a5\u7c7b StringJoiner\u3002 JDK 8 \u4e2d StringJoiner \u662f\u901a\u8fc7 StringBuilder \u5b9e\u73b0\u7684\uff0c \u6240\u4ee5\u5b83\u7684\u6027\u80fd\u548cStringBuilder \u5dee\u4e0d\u591a\uff0c\u5b83\u4e5f\u662f\u975e\u7ebf\u7a0b\u5b89\u5168\u7684\u3002JDK 11 \u4e2d\u5df2\u7ecf\u5bf9\u5176\u8fdb\u884c\u4e86\u4f18\u5316\uff0c\u901a\u8fc7 String[] \u6765\u4ee3\u7406 StringBuilder \u3002<\/p>\n

      \u5728\u65e5\u5e38\u7684\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u600e\u4e48\u9009\u62e9\u5b57\u7b26\u4e32\u62fc\u63a5\u7c7b\u5462\uff1f<\/p>\n

        \n
      1. \u7b80\u5355\u7684\u5b57\u7b26\u4e32\u62fc\u63a5\uff0c\u76f4\u63a5\u4f7f\u7528 + \u5373\u53ef\u3002<\/li>\n
      2. \u5728 for \u5faa\u73af\u4e4b\u7c7b\u7684\u573a\u666f\u4e0b\u9700\u8981\u5b57\u7b26\u4e32\u62fc\u63a5\uff0c\u53ef\u4ee5\u4f18\u5148\u8003\u8651\u4f7f\u7528 StringBuilder \u3002<\/li>\n
      3. \u5728\u4f7f\u7528 Java Stream \u548c lambda \u7684\u573a\u666f\u4e0b\u9700\u8981\u5b57\u7b26\u4e32\u62fc\u63a5\uff0c\u53ef\u4ee5\u4f18\u5148\u8003\u8651\u4f7f\u7528 StringJoiner\u3002<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"\u62fc\u63a5\u5b57\u7b26\u4e32\u65b0\u59ff\u52bf\u2014StringJoiner\u4f60\u77e5\u9053\u4f7f\u7528\u539f\u751f JDK \u62fc\u63a5\u5b57\u7b26\u4e32\u6709\u591a\u5c11\u79cd\u73a9\u6cd5\u5417\uff1f\u6211\u4eec\u80fd\u60f3\u5230\u7684\u5927\u6982\u5c31\u662fStringBuilder\u3001StringBu...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[82],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/681"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=681"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/681\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=681"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=681"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=681"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}