Java8使用StringJoiner看这一篇就够了
点击上方蓝色字体,选择“标星公众号”
优质文章,第一时间送达
Set
catSet = new HashSet<>(2);
@Before
public void init() {
Cat cat = new Cat();
cat.setName("老大");
cat.setAge(1);
catSet.add(cat);
Cat cat2 = new Cat();
cat2.setName("老二");
cat2.setAge(2);
catSet.add(cat2);
}
@Test
public void print1() {
// 给出友好提示
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("[");
Iteratoriterator = catSet.iterator();
int seq = 0;
while (iterator.hasNext()) {
seq++;
stringBuilder.append(seq);
stringBuilder.append(":");
stringBuilder.append(iterator.next().getName());
if (seq != catSet.size()) {
stringBuilder.append(",");
}
}
stringBuilder.append("]");
Assert.assertEquals("[1:老二,2:老大]", stringBuilder.toString());
}
添加元素
@Test
public void whenAddingElements_thenJoinedElements() {
StringJoiner joiner = new StringJoiner(",", PREFIX, SUFFIX);
joiner.add("Red")
.add("Green")
.add("Blue");
assertEquals(joiner.toString(), "[Red,Green,Blue]");
}
@Test
public void whenAddingListElements_thenJoinedListElements() {
ListrgbList = new ArrayList<>();
rgbList.add("Red");
rgbList.add("Green");
rgbList.add("Blue");
StringJoiner rgbJoiner = new StringJoiner(
",", PREFIX, SUFFIX);
for (String color : rgbList) {
rgbJoiner.add(color);
}
assertEquals(rgbJoiner.toString(), "[Red,Green,Blue]");
}
构造
private String PREFIX = "[";
private String SUFFIX = "]";
@Test
public void whenEmptyJoinerWithoutPrefixSuffix_thenEmptyString() {
StringJoiner joiner = new StringJoiner(",");
assertEquals(0, joiner.toString().length());
}
@Test
public void whenEmptyJoinerJoinerWithPrefixSuffix_thenPrefixSuffix() {
StringJoiner joiner = new StringJoiner(
",", PREFIX, SUFFIX);
assertEquals(joiner.toString(), PREFIX + SUFFIX);
}
@Test
public void whenEmptyJoinerWithEmptyValue_thenDefaultValue() {
StringJoiner joiner = new StringJoiner(",");
joiner.setEmptyValue("default");
assertEquals(joiner.toString(), "default");
}
@Test
public void whenEmptyJoinerWithPrefixSuffixAndEmptyValue_thenDefaultValue() {
StringJoiner joiner = new StringJoiner(",", PREFIX, SUFFIX);
joiner.setEmptyValue("default");
assertEquals(joiner.toString(), "default");
}
Joiners组合使用
private String PREFIX = "[";
private String SUFFIX = "]";
@Test
public void whenMergingJoiners_thenReturnMerged() {
StringJoiner rgbJoiner = new StringJoiner(
",", PREFIX, SUFFIX);
StringJoiner cmybJoiner = new StringJoiner(
"-", PREFIX, SUFFIX);
rgbJoiner.add("Red")
.add("Green")
.add("Blue");
cmybJoiner.add("Cyan")
.add("Magenta")
.add("Yellow")
.add("Black");
rgbJoiner.merge(cmybJoiner);
assertEquals(
rgbJoiner.toString(),
"[Red,Green,Blue,Cyan-Magenta-Yellow-Black]");
}
利用Stream API
@Test
public void whenUsedWithinCollectors_thenJoined() {
List rgbList = Arrays.asList("Red", "Green", "Blue");
String commaSeparatedRGB = rgbList.stream()
.map(color -> color.toString())
.collect(Collectors.joining(","));
assertEquals(commaSeparatedRGB, "Red,Green,Blue");
}
改进
@Test
public void print() {
StringJoiner stringJoiner = new StringJoiner(",", "[", "]");
Iteratoriterator = catSet.iterator();
int seq = 0;
while (iterator.hasNext()) {
seq++;
stringJoiner.add(seq + ":" + iterator.next().getName());
}
Assert.assertEquals("[1:老二,2:老大]", stringJoiner.toString());
}
@Test
public void print2() {
ListcatList = new ArrayList<>(catSet);
String result = IntStream.range(0,catList.size()).mapToObj(i->i+":"+ catList.get(i).getName()).collect(Collectors.joining(",", "[", "]"));
Assert.assertEquals("[1:老二,2:老大]", result);
}
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:
https://blog.csdn.net/w605283073/article/details/89527280
粉丝福利:实战springboot+CAS单点登录系统视频教程免费领取
???
?长按上方微信二维码 2 秒 即可获取资料
感谢点赞支持下哈
评论