示例一

public class StringJoinerTest1 {
public static void main(String[] args) {
StringJoiner joiner = new StringJoiner("--", "[[[_ ", "_]]]");
System.out.println(joiner.toString());
System.out.println(joiner.length());
}
}
// result
[[[_ _]]]
8

API介绍

StringJoiner 是 java8 新增的类。

构造器:

public StringJoiner(CharSequence delimiter,
CharSequence prefix,
CharSequence suffix) {
Objects.requireNonNull(prefix, "The prefix must not be null");
Objects.requireNonNull(delimiter, "The delimiter must not be null");
Objects.requireNonNull(suffix, "The suffix must not be null");
// make defensive copies of arguments
this.prefix = prefix.toString();
this.delimiter = delimiter.toString();
this.suffix = suffix.toString();
this.emptyValue = this.prefix + this.suffix;
} public StringJoiner(CharSequence delimiter) {
this(delimiter, "", "");
}

delimiter 是分隔符 , prefix 是前缀 , suffix 是 后缀.

emptyValue 是本类的空值.

add

StringJoiner joiner = new StringJoiner("--", "[[[_", "_]]]");
joiner.add("1");
System.out.println("toString: " + joiner.toString());
System.out.println("length: " + joiner.length());

分析源码:

public StringJoiner add(CharSequence newElement) {
prepareBuilder().append(newElement);
return this;
}
private StringBuilder prepareBuilder() {
if (value != null) {
value.append(delimiter);
} else {
value = new StringBuilder().append(prefix);
}
return value;
}
public AbstractStringBuilder append(CharSequence s) {
if (s == null) return appendNull();
if (s instanceof String) return this.append((String)s);
if (s instanceof AbstractStringBuilder)
return this.append((AbstractStringBuilder)s);
return this.append(s, 0, s.length());
}

​ 发现StringJoiner底层依旧使用的 StringBuilder,第一次添加数据时,会生成StringBuilder对象,并添加 “前缀”,后续添加字符时,追加 “分隔符”,最后调用 append 方法,最底层调用 System.arraycopy 方法。

public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length)

src:源数组

srcPos:源数组要复制的起始位置

dest:目的数组

destPos:目的数组放置的起始位置

length:复制的长度

注意:src and dest都必须是同类型或者可以进行转换类型的数组.

toString

public String toString() {
if (value == null) {
return emptyValue;
} else {
if (suffix.equals("")) {
return value.toString();
} else {
int initialLength = value.length();
String result = value.append(suffix).toString();
value.setLength(initialLength);
return result;
}
}
}

value == null, 返回 空。

不为空,判断 是否需要添加 后缀

length

public int length() {
// Remember that we never actually append the suffix unless we return
// the full (present) value or some sub-string or length of it, so that
// we can add on more if we need to.
return (value != null ? value.length() + suffix.length() :
emptyValue.length());
}
}

value 不为 null ,返回 value的长度 + 后缀长度(不为null时,已经计算了value+前缀)

merge

StringJoiner joiner = new StringJoiner("--", "[[[_", "_]]]");
joiner.add("1").add("2").add("3").add("4"); StringJoiner joiner2 = new StringJoiner("...");
joiner2.add("a").add("b").add("c"); joiner.merge(joiner2);
System.out.println(joiner.toString());

分析:

public StringJoiner merge(StringJoiner other) {
Objects.requireNonNull(other);
if (other.value != null) {
final int length = other.value.length();
// lock the length so that we can seize the data to be appended
// before initiate copying to avoid interference, especially when
// merge 'this'
StringBuilder builder = prepareBuilder();
builder.append(other.value, other.prefix.length(), length);
}
return this;
}
// result
[[[_1--2--3--4--a...b...c_]]]

实例二

import java.util.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(); joiner.add("1");
joiner.add("2");
joiner.add("3");
joiner.add("4");
System.out.println("toString: " + joiner.toString());
System.out.println("length: " + joiner.length()); }
}
// result
toString: [[[__]]]
length: 8 toString: [[[_1--2--3--4_]]]
length: 18

StringJoiner的更多相关文章

  1. [源码分析]Java1.8中StringJoiner的使用以及源码分析

    [源码分析]StringJoiner的使用以及源码分析 StringJoiner是Java里1.8新增的类, 或许有一部分人没有接触过. 所以本文将从使用例子入手, 分析StringJoiner的源码 ...

  2. Java 8 – StringJoiner example

    In this article, we will show you a few StringJoiner examples to join String. 1. StringJoiner1.1 Joi ...

  3. Java StringJoiner

    Java StringJoiner Java added a new final class StringJoiner in java.util package. It is used to cons ...

  4. StringBuilder、StringBuffer和StringJoiner

    StringBuilder是可变对象,用来高效拼接字符串: StringBuilder可以支持链式操作,实现链式操作的关键是返回实例本身: StringBuffer是StringBuilder的线程安 ...

  5. JAVA8之StringJoiner

    作用:运用了StringBuilder的一个拼接字符串的封装处理 示例: StringJoiner sj = new StringJoiner("-", "[" ...

  6. Java8 拼接字符串 StringJoiner

    StringJoiner1.简单的字符串拼接 输出:HelloWorld 注:当我们使用StringJoiner(CharSequence delimiter)初始化一个StringJoiner的时候 ...

  7. StringJoiner,StringBuffer的一些lamada写法

    public String friendlyText(List data) { if(CollectionUtils.isEmpty(data)) { return "[]"; } ...

  8. StringJoiner 源码阅读

    StringJoiner 属性说明 /** * StringJoiner 使用指定的分割符将多个字符串进行拼接,并可指定前缀和后缀 * * @see java.util.stream.Collecto ...

  9. JAVA8—————StringJoiner类

    JAVA8——StringJoiner类引言:在阅读项目代码是,突然看到了StringJoiner这个类的使用,感觉很有意思,对实际开发中也有用,实际上是运用了StringBuilder的一个拼接字符 ...

随机推荐

  1. hdu 3091 Necklace 状态压缩dp *******

    Necklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 327680/327680 K (Java/Others)Total ...

  2. SQLSTATE[HY000] [2002] No such file or directory in

    这个错误将数据库配置信息的localhost改成127.0.0.1就行了

  3. 移动端 line-height 不垂直居中问题

    本文是从简书复制的, markdown语法可能有些出入, 想看"正版"和更多内容请关注 简书: 小贤笔记 一般情况下, 我们把 line-height 的值设置为 height 的 ...

  4. vue click事件获取当前元素属性

    Vue可以传递$event对象 <body id="app"> <ul> <li @click="say('hello!', $event) ...

  5. ASPF(Application Specific Packet Filter)

    ASPF ASPF(Application Specific Packet Filter)是针对应用层的包过滤,其原理是检测通过设备的报文的应用层协议信息,记录临时协商的数据连接,使得某些在安全策略中 ...

  6. vim使用方法----转载

    转载自:http://www.cnblogs.com/itech/archive/2009/04/17/1438439.html vi/vim 基本使用方法本文介绍了vi (vim)的基本使用方法,但 ...

  7. 初学scrum及首次团队开发

    一.初学scrum 1.什么是scrum Scrum在英语的意思是橄榄球里的争球.而在这里Scrum是一种迭代式增量软件开发过程,经常性的被用于敏捷软件开发.Scrum包括了一系列实践和预定义角色的过 ...

  8. java 从网上下载文件的几种方式

    package com.github.pandafang.tool; import java.io.BufferedOutputStream; import java.io.File; import ...

  9. django模板报错:Requested setting TEMPLATE_DEBUG, but settings are not configured. You must either define

    转自:http://blog.csdn.net/xiaowanggedege/article/details/8651236 django模板报错: Requested setting TEMPLAT ...

  10. SQL点点滴滴_修改数据库的兼容级别

    语法 ALTER DATABASE database_name SET COMPATIBILITY_LEVEL = { 80 | 90 | 100 } 参数 database_name 要修改的数据库 ...