google guava Joiner 使用连接字符串
一、引用maven依赖
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>
二、代码示列
package cn.fraudmetrix.guava;
import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import java.util.List;
/**
* google guava Joiner 使用连接字符串
*/
public class JoinerTest {
public static void main(String[] args) {
String one = joinString("my", "name", null, "is", "Joiner","string");
System.out.println(one);
List test = Lists.newArrayList("my", "name", null, "is", "Joiner","list");
System.out.println(joinList(test));
}
/**
* 连接字符串
*
* @param args 字符串
* @return 连接结果
*/
private static String joinString(String... args) {
Joiner joiner = Joiner.on(",").skipNulls();
return joiner.join(args);
}
/**
* 连接字符串数组集合
*
* @param iterable 集合
* @return 连接结果
*/
private static String joinList(Iterable iterable) {
Joiner joiner = Joiner.on(",").skipNulls();
return joiner.join(iterable);
}
}
三、输出结果
my,name,is,Joiner,string
my,name,is,Joiner,list