{"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
\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
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
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- \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
- 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\nJDK8\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