In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "how to use StringJoiner in Java8". In daily operation, I believe many people have doubts about how to use StringJoiner in Java8. Xiaobian consulted all kinds of materials and sorted out simple and easy operation methods. I hope to help you answer the doubts about "how to use StringJoiner in Java8"! Next, please follow the small series to learn together!
introduced
Before using the StringJoiner class, if we wanted the data to end up with comma-separated strings, something like this
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()); } }到此,关于"如何使用Java8中的StringJoiner"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.