点击上方“码农突围”,马上关注
这里是码农充电第一站,回复“666”,获取一份专属大礼包
真爱,请设置“星标”或点个“在看”
在阅读项目代码是,突然看到了StringJoiner这个类的使用,感觉很有意思,对实际开发中也有用,实际上是运用了StringBuilder的一个拼接字符串的封装处理。
为什么会新增这样一个String辅助类?
原有的StringBuilder太死板,不支持分割,如果想让最终的字符串以逗号隔开,需要这样写
StringBuilder sb = new StringBuilder();
IntStream.range(1,10).forEach(i->{
sb.append(i+"");
if( i < 10){
sb.append(",")
}
});
StringJoiner sj = new StringJoiner(",");
IntStream.range(1,10).forEach(i->sj.add(i+""));
有哪些平时用的还比较少的功能:
让我实现StringJoiner,我会怎么办呢?
jdk实现的源码分析
成员变量
private final String prefix;
private final String delimiter;
private final String suffix;
/*
* StringBuilder value -- at any time, the characters constructed from the
* prefix, the added element separated by the delimiter, but without the
* suffix, so that we can more easily add elements without having to jigger
* the suffix each time.
*/
private StringBuilder value;
/*
* By default, the string consisting of prefix+suffix, returned by
* toString(), or properties of value, when no elements have yet been added,
* i.e. when it is empty. This may be overridden by the user to be some
* other value including the empty String.
*/
private String emptyValue;
构造函数
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();
// !!!构造时就直接将emptyValue拼接好了。
this.emptyValue = this.prefix + this.suffix;
}
为什么要一开始就构造好呢?如果我想直接自定义emptyValue直接用构造函数初始化不是更方便吗?是因为绝大多数场景下都不会自定义emptyValue的场景吗?不对啊,感觉这个场景非常必要啊。。。
添加元素
public StringJoiner add(CharSequence newElement) {
prepareBuilder().append(newElement);
return this;
}
private StringBuilder prepareBuilder() {
// 从构造函数和类变量的声明可以看出,没有添加元素前stringbuilder是没有初始化的
if (value != null) {
// 已经有元素存在的情况下,添加元素前先将分隔符添加进去
value.append(delimiter);
} else {
// 没有元素存在的情况下先把前缀加进去
value = new StringBuilder().append(prefix);
}
return value;
}
可以看出再添加元素的过程中就已经把前缀和分割字符什么的都处理好了,全部都在stringbuilde中了,唯一没有处理的就是后缀。为什么?这样做tostring什么的时候真的超级方便的有木有。。。。。
关键的toString
public String toString() {
if (value == null) {
// 这里如果没有自定义空值就是前缀+后缀咯。。
return emptyValue;
} else {
// 为什么不直接value.toString()+suffix?????
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;
}
}
}
为什么不直接value.toString()+suffix?答案在merge方法
merge
public StringJoiner merge(StringJoiner other) {
Objects.requireNonNull(other);
if (other.value != null) {
final int length = other.value.length();
// 下面这段注释是说避免merge(this)时受影响,为什么?
// 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;
}
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());
}
没什么好说的,记住length不只是add的元素的length,还有前后缀。
总结
emptyValue这个一定要构造时就生成吗?用户想有自己的默认值还需要先构造实例再注入吗。。。。这个觉得还是有点奇怪
public StringJoiner setEmptyValue(CharSequence emptyValue) {
// 注意这个Objects.requireNonNull方法是return的第一个参数。。。
this.emptyValue = Objects.requireNonNull(emptyValue,
"The empty value must not be null").toString();
return this;
}
作者:炫迈哥
jianshu.com/p/469fe8fdd3be
最近有不少老铁在后台留言说,想进大厂,但是算法不好。最近我整理了一份刷题实录,这份刷题实录,也让我进了心仪的大厂。现在开放分享给大家。希望对大家有所帮助。 任何的算法题,如同写作文一样,都有一些模板可以套用的。比如面试常考的DP(动态规划),难的是一些关键点是否能想清楚。比如你能写出动态转移方程,这题基本上就可以AC了。 整个刷题实录内容,包括 双子针、动态规划、二分查找、贪心算法、深度优先搜索、字符串、递归、字典树、排序、链表等相关专题内容。图文并茂,附有刷题答案源码。 他对知识点的讲解非常详细,比如这里的贪心算法,排版非常精美: 目前上述内容已打包成完整电子书,具体获取方式如下:
点击下面公众号卡片,关注「程序猿进阶」 公众号;
在「程序猿进阶」公众号后台回复关键词「115」获取下载地址。
最近有不少老铁在后台留言说,想进大厂,但是算法不好。最近我整理了一份刷题实录,这份刷题实录,也让我进了心仪的大厂。现在开放分享给大家。希望对大家有所帮助。 任何的算法题,如同写作文一样,都有一些模板可以套用的。比如面试常考的DP(动态规划),难的是一些关键点是否能想清楚。比如你能写出动态转移方程,这题基本上就可以AC了。 整个刷题实录内容,包括 双子针、动态规划、二分查找、贪心算法、深度优先搜索、字符串、递归、字典树、排序、链表等相关专题内容。图文并茂,附有刷题答案源码。 他对知识点的讲解非常详细,比如这里的贪心算法,排版非常精美: 目前上述内容已打包成完整电子书,具体获取方式如下:
点击下面公众号卡片,关注「程序猿进阶」 公众号;
在「程序猿进阶」公众号后台回复关键词「115」获取下载地址。