1.Joiner用處
2.Jointer的常用方法
3.Joiner用法實例
package string;
import java.util.Map;
import com.google.common.base.Joiner;
import com.google.common.collect.Maps;
public class Jointer {
public static void useMethod() {
// 將字符串數組之間用符號合併
String[] strs = { "as", "technology", "scales", "NoC" };
Joiner joiner = Joiner.on(",");
System.out.println(joiner.join(strs));
// 跳過null的合併
String resultSkipNull = Joiner.on(".").skipNulls()
.join("http", null, "Journal");
System.out.println(resultSkipNull);
// 將null替換爲其他字符的合併
// 此處,將null替換爲NULL
String resultWithNull = Joiner.on("&").useForNull("NULL")
.join("http", null, "complete");
System.out.println(resultWithNull);
// Map的處理,將map中的null替換爲DD,然後將key與value用符號"$"分割
Joiner.MapJoiner map_joiner = Joiner.on(";").useForNull("DD")
.withKeyValueSeparator("--");
Map map = Maps.newHashMap();
map.put("qunzer", "csdn");
map.put(null, "test");
map.put("NoC", "Network-on-Chip");
String mapResult = map_joiner.join(map);
System.out.println(mapResult);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
useMethod();
}
}
http.Journal
http&NULL&complete
DD--test;NoC--Network-on-Chip;qunzer--csdn