前言
网站建设公司,为您提供网站建设,网站制作,网页设计及定制网站建设服务,专注于企业网站制作,高端网页制作,对火锅店设计等多个行业拥有丰富的网站建设经验的网站建设公司。专业网站设计,网站优化推广哪家好,专业seo优化优化,H5建站,响应式网站。StringJoiner是Java里1.8新增的类,主要是帮助我们把一个列表拼接字符串, 或许有一部分人没有接触过. 所以本文将从使用例子入手, 分析StringJoiner的源码.
基本好的同学, 其实只要把这段例子自己运行一下, 自己看看源码就可以了.因为我觉得这个类挺简单的. 没必要看我下面的废话....
public class StringJoinerTest { public static void main(String[] args) { StringJoiner joiner = new StringJoiner("--", "[[[_", "_]]]"); System.out.println("toString: " + joiner.toString()); System.out.println("length: " + joiner.length()); System.out.println("******************(1)********************"); joiner.add("1"); joiner.add("2"); joiner.add("3"); joiner.add("4"); System.out.println("toString: " + joiner.toString()); System.out.println("length: " + joiner.length()); System.out.println("******************(2)********************"); StringJoiner joiner2 = new StringJoiner("..."); System.out.println("toString: " + joiner2.toString()); System.out.println("length: " + joiner2.length()); System.out.println("******************(3)********************"); joiner2.add("a"); joiner2.add("b"); joiner2.add("c"); System.out.println("toString: " + joiner2.toString()); System.out.println("length: " + joiner2.length()); System.out.println("******************(4)********************"); joiner.merge(joiner2); System.out.println("toString: " + joiner.toString()); System.out.println("******************(5)********************"); StringJoiner joiner3 = new StringJoiner("==", "qianzhui", "houzhui"); joiner3.add("壹"); joiner3.add("贰"); joiner3.add("叁"); joiner.merge(joiner3); System.out.println("toString: " + joiner.toString()); System.out.println("length: " + joiner.length()); System.out.println("******************(6)********************"); joiner.merge(joiner); // joiner.merge(this) System.out.println("toString: " + joiner.toString()); System.out.println("length: " + joiner.length()); } }