连接
比如,有这样一个集合[1,2,3,4,5,7,null]
,想把这个集合转换成以#
分割的字符串,并过滤掉集合中的空元素
List<Integer> eleList = Arrays.asList(1, 2, 3, 4, 5, 6, 7, null);
String joinStr = Joiner.on("#") //分隔符
.skipNulls() //过滤null元素
.join(eleList);//要分割的集合
System.out.println(joinStr);
运行结果:1#2#3#4#5#6#7
分割
有这样一个字符串"1,2,3,4,5,6,7"
,要把这个字符串以,
分割,并放到一个集合里面
String str="1,2,3,4,5,6,7";
List<String> stringList = Splitter.on(",")
.trimResults()
.splitToList(str);
System.out.println(stringList);
运行结果:[1, 2, 3, 4, 5, 6, 7]
主要对url的param的编码
Map<String, String> of = ImmutableMap.of("id", "123", "name", "green");
String join = Joiner.on("&").withKeyValueSeparator("=").join(of);
System.out.println(join);
运行结果:id=123&name=green
String str="id=123&name=green";
Map<String, String> split = Splitter.on("&")
.withKeyValueSeparator("=")
.split(str);
System.out.println(split);
运行结果:{id=123, name=green}
Preconditions.checkNotNul(T)//验证对象是否为null
Preconditions.checkArgument //验证表达式是否成立
原文:https://www.cnblogs.com/hitechr/p/10473094.html