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

GitHub 1.3k Star \u7684Java\u5de5\u7a0b\u5e08\u6210\u795e\u4e4b\u8def \uff0c\u4e0d\u6765\u4e86\u89e3\u4e00\u4e0b\u5417?<\/p>\n

\u5728\u4e3a\u4ec0\u4e48\u963f\u91cc\u5df4\u5df4\u4e0d\u5efa\u8bae\u5728for\u5faa\u73af\u4e2d\u4f7f\u7528\u201d+\u201d\u8fdb\u884c\u5b57\u7b26\u4e32\u62fc\u63a5\u4e00\u6587\u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86\u51e0\u79cdJava\u4e2d\u5b57\u7b26\u4e32\u62fc\u63a5\u7684\u65b9\u5f0f\uff0c\u4ee5\u53ca\u4f18\u7f3a\u70b9\u3002\u5176\u4e2d\u8fd8\u6709\u4e00\u4e2a\u91cd\u8981\u7684\u62fc\u63a5\u65b9\u5f0f\u6211\u6ca1\u6709\u4ecb\u7ecd\uff0c\u90a3\u5c31\u662fJava 8\u4e2d\u63d0\u4f9b\u7684StringJoiner \uff0c\u672c\u6587\u5c31\u6765\u4ecb\u7ecd\u4e00\u4e0b\u8fd9\u4e2a\u5b57\u7b26\u4e32\u62fc\u63a5\u7684\u65b0\u5175\u3002<\/p>\n

\u5982\u679c\u4f60\u60f3\u77e5\u9053\u4e00\u5171\u6709\u591a\u5c11\u79cd\u65b9\u6cd5\u53ef\u4ee5\u8fdb\u884c\u5b57\u7b26\u4e32\u62fc\u63a5\uff0c\u6559\u4f60\u4e00\u4e2a\u7b80\u5355\u7684\u529e\u6cd5\uff0c\u5728Intellij IDEA\u4e2d\uff0c\u5b9a\u4e49\u4e00\u4e2aJava Bean\uff0c\u7136\u540e\u5c1d\u8bd5\u4f7f\u7528\u5feb\u6377\u952e\u81ea\u52a8\u751f\u6210\u4e00\u4e2atoString\u65b9\u6cd5\uff0cIDEA\u4f1a\u63d0\u793a\u591a\u79cdtoString\u751f\u6210\u7b56\u7565\u53ef\u4f9b\u9009\u62e9\u3002<\/p>\n<\/p>\n

\n \"Java
<\/figcaption><\/figure>\n

\ufffc <\/p>\n

\u76ee\u524d\u6211\u4f7f\u7528\u7684IDEA\u7684toString\u751f\u6210\u7b56\u7565\u9ed8\u8ba4\u7684\u662f\u4f7f\u7528JDK 1.8\u63d0\u4f9b\u7684StringJoiner\u3002<\/p>\n

\u4ecb\u7ecd<\/h3>\n

StringJoiner\u662fjava.util\u5305\u4e2d\u7684\u4e00\u4e2a\u7c7b\uff0c\u7528\u4e8e\u6784\u9020\u4e00\u4e2a\u7531\u5206\u9694\u7b26\u5206\u9694\u7684\u5b57\u7b26\u5e8f\u5217\uff08\u53ef\u9009\uff09\uff0c\u5e76\u4e14\u53ef\u4ee5\u4ece\u63d0\u4f9b\u7684\u524d\u7f00\u5f00\u59cb\u5e76\u4ee5\u63d0\u4f9b\u7684\u540e\u7f00\u7ed3\u5c3e\u3002\u867d\u7136\u8fd9\u4e5f\u53ef\u4ee5\u5728StringBuilder\u7c7b\u7684\u5e2e\u52a9\u4e0b\u5728\u6bcf\u4e2a\u5b57\u7b26\u4e32\u4e4b\u540e\u9644\u52a0\u5206\u9694\u7b26\uff0c\u4f46StringJoiner\u63d0\u4f9b\u4e86\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\uff0c\u800c\u65e0\u9700\u7f16\u5199\u5927\u91cf\u4ee3\u7801\u3002<\/p>\n

StringJoiner\u7c7b\u5171\u67092\u4e2a\u6784\u9020\u51fd\u6570\uff0c5\u4e2a\u516c\u6709\u65b9\u6cd5\u3002\u5176\u4e2d\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u5c31\u662fadd\u65b9\u6cd5\u548ctoString\u65b9\u6cd5\uff0c\u7c7b\u4f3c\u4e8eStringBuilder\u4e2d\u7684append\u65b9\u6cd5\u548ctoString\u65b9\u6cd5\u3002<\/p>\n

\u7528\u6cd5<\/h3>\n

StringJoiner\u7684\u7528\u6cd5\u6bd4\u8f83\u7b80\u5355\uff0c\u4e0b\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528StringJoiner\u8fdb\u884c\u4e86\u5b57\u7b26\u4e32\u62fc\u63a5\u3002<\/p>\n

public class StringJoinerTest {\n\n    public static void main(String[] args) {\n        StringJoiner sj = new StringJoiner(\"Hollis\");\n\n        sj.add(\"hollischuang\");\n        sj.add(\"Java\u5e72\u8d27\");\n        System.out.println(sj.toString());\n\n        StringJoiner sj1 = new StringJoiner(\":\",\"[\",\"]\");\n\n        sj1.add(\"Hollis\").add(\"hollischuang\").add(\"Java\u5e72\u8d27\");\n        System.out.println(sj1.toString());\n    }\n}\n<\/code><\/pre>\n

\u4ee5\u4e0a\u4ee3\u7801\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n

hollischuangHollisJava\u5e72\u8d27\n[Hollis:hollischuang:Java\u5e72\u8d27]\n<\/code><\/pre>\n

\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u5f53\u6211\u4eecStringJoiner(CharSequence delimiter)<\/code>\u521d\u59cb\u5316\u4e00\u4e2aStringJoiner<\/code>\u7684\u65f6\u5019\uff0c\u8fd9\u4e2adelimiter<\/code>\u5176\u5b9e\u662f\u5206\u9694\u7b26\uff0c\u5e76\u4e0d\u662f\u53ef\u53d8\u5b57\u7b26\u4e32\u7684\u521d\u59cb\u503c\u3002<\/p>\n

StringJoiner(CharSequence delimiter,CharSequence prefix,CharSequence suffix)<\/code>\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<\/p>\n

\u539f\u7406<\/h3>\n

\u4ecb\u7ecd\u4e86\u7b80\u5355\u7684\u7528\u6cd5\u4e4b\u540e\uff0c\u6211\u4eec\u518d\u6765\u770b\u770b\u8fd9\u4e2aStringJoiner\u7684\u539f\u7406\uff0c\u770b\u770b\u4ed6\u5230\u5e95\u662f\u5982\u4f55\u5b9e\u73b0\u7684\u3002\u4e3b\u8981\u770b\u4e00\u4e0badd\u65b9\u6cd5\uff1a<\/p>\n

public StringJoiner add(CharSequence newElement) {\n    prepareBuilder().append(newElement);\n    return this;\n}\n\nprivate StringBuilder prepareBuilder() {\n    if (value != null) {\n        value.append(delimiter);\n    } else {\n        value = new StringBuilder().append(prefix);\n    }\n    return value;\n}\n<\/code><\/pre>\n

\u770b\u5230\u4e86\u4e00\u4e2a\u719f\u6089\u7684\u8eab\u5f71\u2014\u2014StringBuilder \uff0c\u6ca1\u9519\uff0cStringJoiner\u5176\u5b9e\u5c31\u662f\u4f9d\u8d56StringBuilder\u5b9e\u73b0\u7684\uff0c\u5728\u4e3a\u4ec0\u4e48\u963f\u91cc\u5df4\u5df4\u4e0d\u5efa\u8bae\u5728for\u5faa\u73af\u4e2d\u4f7f\u7528\u201d+\u201d\u8fdb\u884c\u5b57\u7b26\u4e32\u62fc\u63a5\u4e2d\u6211\u4eec\u4ecb\u7ecd\u8fc7StringBuilder\u7684\u5b9e\u73b0\u539f\u7406\uff0c\u672c\u6587\u4e0d\u5728\u8d58\u8ff0\u3002<\/p>\n

\u5f53\u6211\u4eec\u53d1\u73b0StringJoiner\u5176\u5b9e\u662f\u901a\u8fc7StringBuilder\u5b9e\u73b0\u4e4b\u540e\uff0c\u6211\u4eec\u5927\u6982\u5c31\u53ef\u4ee5\u731c\u5230\uff0c\u4ed6\u7684\u6027\u80fd\u635f\u8017\u5e94\u8be5\u548c\u76f4\u63a5\u4f7f\u7528StringBuilder\u5dee\u4e0d\u591a<\/strong>\uff01<\/p>\n

\u4e3a\u4ec0\u4e48\u9700\u8981StringJoiner<\/h3>\n

\u5728\u4e86\u89e3\u4e86StringJoiner\u7684\u7528\u6cd5\u548c\u539f\u7406\u540e\uff0c\u53ef\u80fd\u5f88\u591a\u8bfb\u8005\u5c31\u4f1a\u4ea7\u751f\u4e00\u4e2a\u7591\u95ee\uff0c\u660e\u660e\u5df2\u7ecf\u6709\u4e00\u4e2aStringBuilder\u4e86\uff0c\u4e3a\u4ec0\u4e48Java 8\u4e2d\u8fd8\u8981\u5b9a\u4e49\u4e00\u4e2aStringJoiner\u5462\uff1f\u5230\u5e95\u6709\u4ec0\u4e48\u597d\u5904\u5462\uff1f<\/p>\n

\u5982\u679c\u8bfb\u8005\u8db3\u591f\u4e86\u89e3Java 8\u7684\u8bdd\uff0c\u6216\u8bb8\u53ef\u4ee5\u731c\u51fa\u4e2a\u5927\u6982\uff0c\u8fd9\u80af\u5b9a\u548cStream\u6709\u5173\u3002<\/p>\n

\u4f5c\u8005\u4e5f\u5728Java doc\u4e2d\u627e\u5230\u4e86\u7b54\u6848\uff1a<\/p>\n

\n

A StringJoiner may be employed to create formatted output from a Stream using Collectors.joining(CharSequence)<\/p>\n<\/blockquote>\n

\u8bd5\u60f3\uff0c\u5728Java\u4e2d\uff0c\u5982\u679c\u6211\u4eec\u6709\u8fd9\u6837\u4e00\u4e2aList\uff1a<\/p>\n

List<String> list = ImmutableList.of(\"Hollis\",\"hollischuang\",\"Java\u5e72\u8d27\");\n<\/code><\/pre>\n

\u5982\u679c\u6211\u4eec\u60f3\u8981\u628a\u4ed6\u62fc\u63a5\u6210\u4e00\u4e2a\u4ee5\u4e0b\u5f62\u5f0f\u7684\u5b57\u7b26\u4e32\uff1a<\/p>\n

Hollis,hollischuang,Java\u5e72\u8d27\n<\/code><\/pre>\n

\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\uff1a<\/p>\n

StringBuilder builder = new StringBuilder();\n\nif (!list.isEmpty()) {\n    builder.append(list.get(0));\n    for (int i = 1, n = list.size(); i < n; i++) {\n        builder.append(\",\").append(list.get(i));\n    }\n}\nbuilder.toString();\n<\/code><\/pre>\n

\u8fd8\u53ef\u4ee5\u4f7f\u7528\uff1a<\/p>\n

list.stream().reduce(new StringBuilder(), (sb, s) -> sb.append(s).append(','), StringBuilder::append).toString();\n<\/code><\/pre>\n

\u4f46\u662f\u8f93\u51fa\u7ed3\u679c\u7a0d\u6709\u4e9b\u4e0d\u540c\uff0c\u9700\u8981\u8fdb\u884c\u4e8c\u6b21\u5904\u7406\uff1a<\/p>\n

Hollis,hollischuang,Java\u5e72\u8d27,\n<\/code><\/pre>\n

\u8fd8\u53ef\u4ee5\u4f7f\u7528\"+\"\u8fdb\u884c\u62fc\u63a5\uff1a<\/p>\n

list.stream().reduce((a,b)->a + \",\" + b).toString();\n<\/code><\/pre>\n

\u4ee5\u4e0a\u51e0\u79cd\u65b9\u5f0f\uff0c\u8981\u4e48\u662f\u4ee3\u7801\u590d\u6742\uff0c\u8981\u4e48\u662f\u6027\u80fd\u4e0d\u9ad8\uff0c\u6216\u8005\u65e0\u6cd5\u76f4\u63a5\u5f97\u5230\u60f3\u8981\u7684\u7ed3\u679c\u3002<\/p>\n

\u4e3a\u4e86\u6ee1\u8db3\u7c7b\u4f3c\u8fd9\u6837\u7684\u9700\u6c42\uff0cJava 8\u4e2d\u63d0\u4f9b\u7684StringJoiner\u5c31\u6d3e\u4e0a\u7528\u573a\u4e86\u3002\u4ee5\u4e0a\u9700\u6c42\u53ea\u9700\u8981\u4e00\u884c\u4ee3\u7801\uff1a<\/p>\n

list.stream().collect(Collectors.joining(\":\"))\n<\/code><\/pre>\n

\u5373\u53ef\u3002\u4e0a\u9762\u7528\u7684\u8868\u8fbe\u5f0f\u4e2d\uff0cCollectors.joining\u7684\u6e90\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n

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

\u5176\u5b9e\u73b0\u539f\u7406\u5c31\u662f\u501f\u52a9\u4e86StringJoiner\u3002<\/p>\n

\u5f53\u7136\uff0c\u6216\u8bb8\u5728Collector<\/code>\u4e2d\u76f4\u63a5\u4f7f\u7528StringBuilder<\/code>\u4f3c\u4e4e\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u7c7b\u4f3c\u7684\u529f\u80fd\uff0c\u53ea\u4e0d\u8fc7\u7a0d\u5fae\u9ebb\u70e6\u4e00\u4e9b\u3002\u6240\u4ee5\uff0cJava 8\u4e2d\u63d0\u4f9b\u4e86StringJoiner<\/code>\u6765\u4e30\u5bccStream<\/code>\u7684\u7528\u6cd5\u3002<\/p>\n

\u800c\u4e14StringJoiner<\/code>\u4e5f\u53ef\u4ee5\u65b9\u4fbf\u7684\u589e\u52a0\u524d\u7f00\u548c\u540e\u7f00\uff0c\u6bd4\u5982\u6211\u4eec\u5e0c\u671b\u5f97\u5230\u7684\u5b57\u7b26\u4e32\u662f[Hollis,hollischuang,Java\u5e72\u8d27]<\/code>\u800c\u4e0d\u662fHollis,hollischuang,Java<\/code>\u5e72\u8d27\u7684\u8bdd\uff0cStringJoiner\u7684\u4f18\u52bf\u5c31\u66f4\u52a0\u660e\u663e\u4e86\u3002<\/p>\n

\u603b\u7ed3<\/h3>\n

\u672c\u6587\u4ecb\u7ecd\u4e86Java 8\u4e2d\u63d0\u4f9b\u7684\u53ef\u53d8\u5b57\u7b26\u4e32\u7c7b\u2014\u2014StringJoiner\uff0c\u53ef\u4ee5\u7528\u4e8e\u5b57\u7b26\u4e32\u62fc\u63a5\u3002<\/p>\n

StringJoiner\u5176\u5b9e\u662f\u901a\u8fc7StringBuilder\u5b9e\u73b0\u7684\uff0c\u6240\u4ee5\u4ed6\u7684\u6027\u80fd\u548cStringBuilder\u5dee\u4e0d\u591a\uff0c\u4ed6\u4e5f\u662f\u975e\u7ebf\u7a0b\u5b89\u5168\u7684\u3002<\/p>\n

\u5982\u679c\u65e5\u5e38\u5f00\u53d1\u4e2d\u4e2d\uff0c\u9700\u8981\u8fdb\u884c\u5b57\u7b26\u4e32\u62fc\u63a5\uff0c\u5982\u4f55\u9009\u62e9\uff1f<\/p>\n

1\u3001\u5982\u679c\u53ea\u662f\u7b80\u5355\u7684\u5b57\u7b26\u4e32\u62fc\u63a5\uff0c\u8003\u8651\u76f4\u63a5\u4f7f\u7528\"+\"\u5373\u53ef\u3002<\/p>\n

2\u3001\u5982\u679c\u662f\u5728for\u5faa\u73af\u4e2d\u8fdb\u884c\u5b57\u7b26\u4e32\u62fc\u63a5\uff0c\u8003\u8651\u4f7f\u7528StringBuilder<\/code>\u548cStringBuffer<\/code>\u3002<\/p>\n

3\u3001\u5982\u679c\u662f\u901a\u8fc7\u4e00\u4e2aList<\/code>\u8fdb\u884c\u5b57\u7b26\u4e32\u62fc\u63a5\uff0c\u5219\u8003\u8651\u4f7f\u7528StringJoiner<\/code>\u3002<\/p>\n

GitHub 1.3k Star \u7684Java\u5de5\u7a0b\u5e08\u6210\u795e\u4e4b\u8def \uff0c\u4e0d\u6765\u4e86\u89e3\u4e00\u4e0b\u5417?<\/p>\n

GitHub 1.3k Star \u7684Java\u5de5\u7a0b\u5e08\u6210\u795e\u4e4b\u8def \uff0c\u771f\u7684\u4e0d\u6765\u4e86\u89e3\u4e00\u4e0b\u5417?<\/p>\n

GitHub 1.3k Star \u7684Java\u5de5\u7a0b\u5e08\u6210\u795e\u4e4b\u8def \uff0c\u771f\u7684\u786e\u5b9a\u4e0d\u6765\u4e86\u89e3\u4e00\u4e0b\u5417?<\/p>\n<\/p>\n

\n \"Java
<\/figcaption><\/figure><\/p>\n","protected":false},"excerpt":{"rendered":"Java 8\u4e2d\u5b57\u7b26\u4e32\u62fc\u63a5\u65b0\u59ff\u52bf\uff1aStringJoiner\u5728\u4e3a\u4ec0\u4e48\u963f\u91cc\u5df4\u5df4\u4e0d\u5efa\u8bae\u5728for\u5faa\u73af\u4e2d\u4f7f\u7528\u201d+\u201d\u8fdb\u884c\u5b57\u7b26\u4e32\u62fc\u63a5\u4e00\u6587\u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86\u51e0\u79cdJav...","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\/683"}],"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=683"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/683\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=683"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=683"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=683"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}