StringJoiner
示例一
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的更多相关文章
- [源码分析]Java1.8中StringJoiner的使用以及源码分析
[源码分析]StringJoiner的使用以及源码分析 StringJoiner是Java里1.8新增的类, 或许有一部分人没有接触过. 所以本文将从使用例子入手, 分析StringJoiner的源码 ...
- Java 8 – StringJoiner example
In this article, we will show you a few StringJoiner examples to join String. 1. StringJoiner1.1 Joi ...
- Java StringJoiner
Java StringJoiner Java added a new final class StringJoiner in java.util package. It is used to cons ...
- StringBuilder、StringBuffer和StringJoiner
StringBuilder是可变对象,用来高效拼接字符串: StringBuilder可以支持链式操作,实现链式操作的关键是返回实例本身: StringBuffer是StringBuilder的线程安 ...
- JAVA8之StringJoiner
作用:运用了StringBuilder的一个拼接字符串的封装处理 示例: StringJoiner sj = new StringJoiner("-", "[" ...
- Java8 拼接字符串 StringJoiner
StringJoiner1.简单的字符串拼接 输出:HelloWorld 注:当我们使用StringJoiner(CharSequence delimiter)初始化一个StringJoiner的时候 ...
- StringJoiner,StringBuffer的一些lamada写法
public String friendlyText(List data) { if(CollectionUtils.isEmpty(data)) { return "[]"; } ...
- StringJoiner 源码阅读
StringJoiner 属性说明 /** * StringJoiner 使用指定的分割符将多个字符串进行拼接,并可指定前缀和后缀 * * @see java.util.stream.Collecto ...
- JAVA8—————StringJoiner类
JAVA8——StringJoiner类引言:在阅读项目代码是,突然看到了StringJoiner这个类的使用,感觉很有意思,对实际开发中也有用,实际上是运用了StringBuilder的一个拼接字符 ...
随机推荐
- 实例化geoserver wms服务
var vectorWmsJHdataLayer = new ol.layer.Tile({ source: new ol.source.TileWMS({ //地址 url:'http://loca ...
- 获取指定包名下继承或者实现某接口的所有类(扫描文件目录和所有jar)
import java.io.File; import java.io.FileFilter; import java.io.IOException; import java.net.JarURLCo ...
- json转换工具类
using System;using System.Collections.Generic;using System.Text;using Newtonsoft.Json;using System.I ...
- shell脚本报错 value too great for base
此错误是shell脚本在计算以0开头的数字时,默认以8进制进行计算,导致在计算08时超过了8进制的范围,报此错误. shell脚本代码如下: #!/bin/bash a= ..} do a=$[$a+ ...
- IO流之打印流
打印流的概述(只有输出就是只与数据目的有关,不会抛出IO异常) 打印流添加输出数据的功能,使它们能够方便地打印各种数据值表示形式. 打印流根据流的分类: l 字节打印流 PrintStream l ...
- KMP算法的一个简单实现
今天学习KMP算法,参考网上内容,实现算法,摘录网页内容并记录自己的实现如下: 原文出处: http://www.ruanyifeng.com/blog/2013/05/Knuth%E2%80%93M ...
- atoi、itoa,strcpy,strcmp,memcpy等实现
原文:http://www.cnblogs.com/lpshou/archive/2012/06/05/2536799.html 1.memcpy.memmove.memset源码 link:http ...
- MXNet 分布式环境部署
MXNet 分布式环境部署 1. MxNet 分布式介绍 先忽略吧, 回头在填上去 2. 分布式部署方法 假设有两台主机ip地址分别是xxx.xxx.xxx.114 和 xxx.xxx.xxx.111 ...
- libgdx for eclipse开发环境搭建
1.安装jdk1.7以上 2.下载libgdx1.2.0 下载地址:https://libgdx.badlogicgames.com/releases 3.下载项目创建工具(老版本的) 下载地址:ht ...
- 密码存储中MD5的安全问题与替代方案
md5安全吗?有多么地不安全?如何才能安全地存储密码?... md5安全吗? 经过各种安全事件后,很多系统在存放密码的时候不会直接存放明文密码了,大都改成了存放了 md5 加密(hash)后的密码,可 ...