private final List<String> stringList= Arrays.asList(
"a","b","c"
);
private final List<String> stringList2= Arrays.asList(
"a","b","c",null
);
private final Map<String,String> stringMap=of("hello","Guava","Java","Scala");
private final String targetFilepath="D:\\BaiduNetdiskDownload\\guava-joioner.txt";
@Test
public void testJoinerOnJoiner(){
String result = Joiner.on("#").join(stringList);
assertThat(result,equalTo("a#b"));
}
@Test
public void testJoinerOnJoiner2(){
String result = Joiner.on("#").join(stringList2);
assertThat(result,equalTo("a#b"));
}
@Test
public void testJoinerOnJoiner3(){
String result = Joiner.on("#").skipNulls().join(stringList2);
assertThat(result,equalTo("a#b#c"));
}
@Test
public void testJoinerOnJoiner4(){
String result = Joiner.on("#").useForNull("d").join(stringList2);
assertThat(result,equalTo("a#b#c#d"));
}
@Test
public void testJoinerOnJoiner5(){
final StringBuilder builder=new StringBuilder();
StringBuilder result = Joiner.on("#").useForNull("d").appendTo(builder,stringList2);
assertThat(builder,sameInstance(result));
}
@Test
public void testJoinerOnappendtowriter() throws IOException {
FileWriter fileWriter=null;
try{
fileWriter=new FileWriter(new File(targetFilepath));
Joiner.on("#").useForNull("d").appendTo(fileWriter, stringList2);
fileWriter.flush();
}catch (IOException e){
fail("append to the writer occur fetal error.");
}finally {
fileWriter.close();
}
}
@Test
public void testJoinerOnappendtowriter1() throws IOException {
String collect = stringList2.stream().filter(item -> item != null && !item.isEmpty()).collect(Collectors.joining("#"));
System.out.println(collect);
}
@Test
public void testJoinerOnappendtowriter2() throws IOException {
String collect = stringList2.stream().map(item->item==null||item.isEmpty()?"d":item).collect(Collectors.joining("#"));
System.out.println(collect);
}
@Test
public void testJoinerMap() throws IOException {
String join = Joiner.on("#").withKeyValueSeparator("=").join(stringMap);
System.out.println(join);
}
guava 拼接字符串 List&Map
最新推荐文章于 2024-09-11 20:55:45 发布