前言
之前,我们经常会通过StringBuffer或者StingBuilder对字符串进行拼接,但是你知道Java8中推出的StringJoiner吗?它比前者更加优美、灵活,如果你现在还使用StringBuffer拼接,强烈推荐你试试StringJoiner。
介绍
在实用StringJoiner类之前,如果我们想要数据最终的字符串以逗号隔开,大概是这样的
- StringBuilder sb = new StringBuilder();
- IntStream.range(1,10).forEach(i->{
- sb.append(i+"");
- if( i < 10){
- sb.append(",")
- }
- });
如果引入StringJoiner,如何处理呢?
- StringJoiner sj = new StringJoiner(",");
- IntStream.range(1,10).forEach(i->sj.add(i+""));
看着是不是更简单直观了呢?
另外,StringJoiner类的构造函数,还可以做到可选择性地从我们自定义的前缀开始和自定义的后缀结尾,比较灵活和实用。
- //值依次是分割符 , 前缀 ,后缀
- StringJoiner stringJoiner = new StringJoiner(",", "[", "]");
- stringJoiner.add("xiao");
- stringJoiner.add("zhi");
- System.out.println(stringJoiner.toString());
输出结果:[xiao,zhi]
StringJoiner在处理sql拼接上面,也非常方便,如拼接 sql 的in条件的时候:
- StringJoiner joiner3 = new StringJoiner("','", "'", "'");
- joiner3.add("1").add("2");
- //输出 : '1','2'
更多实用的功能,大家可以探索。
源码
这个类的源码很简单,大家很容易就可以看明白。StringJoiner 更像一个装饰者模式,对外隐藏了StringBuilder。
不过需要注意的是 StringJoiner 并且没有处理一些基本的集合元素情况,比如加入列表的元素,更像针对Collectors而设计。
- package java.util;
- public final class StringJoiner {
- private final String prefix;//前缀
- private final String delimiter;//间隔符
- private final String suffix;//后缀
- private StringBuilder value;//值
- private String emptyValue;//空值
- public StringJoiner(CharSequence delimiter) {
- this(delimiter, "", "");
- //默认前缀和后缀为"",重载调用
- }
- public StringJoiner(CharSequence delimiter,
- CharSequence prefix,
- CharSequence suffix) {
- //间隔符,前缀和后缀判断是否为null,null将抛出异常
- 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");
- // 成员变量赋值
- this.prefix = prefix.toString();
- this.delimiter = delimiter.toString();
- this.suffix = suffix.toString();
- thisthis.emptyValue = this.prefix + this.suffix;//空值被设置为只有前后缀
- }
- //设置空值,检查是否为null
- public StringJoiner setEmptyValue(CharSequence emptyValue) {
- this.emptyValue = Objects.requireNonNull(emptyValue,
- "The empty value must not be null").toString();
- return this;
- }
- @Override
- 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();
- // reset value to pre-append initialLength
- value.setLength(initialLength);
- return result;
- }
- }
- }
- //初始化,先添加前缀,有了之后每次先添加间隔符,StringBuilder后续append字符串
- public StringJoiner add(CharSequence newElement) {
- prepareBuilder().append(newElement);
- return this;
- }
- //合并StringJoiner,注意后面StringJoiner 的前缀就不要了,后面的appen进来
- 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;
- }
- //初始化,先添加前缀,添加之后每次先添加间隔符
- private StringBuilder prepareBuilder() {
- if (value != null) {
- value.append(delimiter);
- } else {
- value = new StringBuilder().append(prefix);
- }
- return value;
- }
- 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());
- }
- }