guava库中,包含了很好的join和split的功能,
例子如下:
1) 将LIST转换为使用字符串连接的字符串
class="java" name="code">
List<String> names = Lists.newArrayList("John", "Jane", "Adam", "Tom");
String result = Joiner.on(",").join(names);
assertEquals(result, "John,Jane,Adam,Tom");
}
2)使用joiner把MAP转成STRING
@Test
public void whenConvertMapToString_thenConverted() {
Map<String, Integer> salary = Maps.newHashMap();
salary.put("John", 1000);
salary.put("Jane", 1500);
String result = Joiner.on(" , ").withKeyValueSeparator(" = ")
.join(salary);
assertThat(result, containsString("John = 1000"));
assertThat(result, containsString("Jane = 1500"));
}
3 将嵌套的collection join起来
@Test
public void whenJoinNestedCollections_thenJoined() {
List<ArrayList<String>> nested = Lists.newArrayList(
Lists.newArrayList("apple", "banana", "orange"),
Lists.newArrayList("cat", "dog", "bird"),
Lists.newArrayList("John", "Jane", "Adam"));
String result = Joiner.on(";").join(Iterables.transform(nested,
new Function<List<String>, String>() {
@Override
public String apply(List<String> input) {
return Joiner.on("-").join(input);
}
}));
assertThat(result, containsString("apple-banana-orange"));
assertThat(result, containsString("cat-dog-bird"));
assertThat(result, containsString("apple-banana-orange"));
}
4 当使用joiner时候处理null值
有几个对付NULL值的方法,第一个是skipNulls().
@Test
public void whenConvertListToStringAndSkipNull_thenConverted() {
List<String> names = Lists.newArrayList("John", null, "Jane", "Adam", "Tom");
String result = Joiner.on(",").skipNulls().join(names);
assertEquals(result, "John,Jane,Adam,Tom");
}
或者干脆将null值换掉,就是:
@Test
public void whenUseForNull_thenUsed() {
List<String> names = Lists.newArrayList("John", null, "Jane", "Adam", "Tom");
String result = Joiner.on(",").useForNull("nameless").join(names);
assertEquals(result, "John,nameless,Jane,Adam,Tom");
}
5 使用splitter方法把string转换为list
@Test
public void whenCreateListFromString_thenCreated() {
String input = "apple - banana - orange";
List<String> result = Splitter.on("-").trimResults()
.splitToList(input);
assertThat(result, contains("apple", "banana", "orange"));
}
6 使用splitter将string转为map
@Test
public void whenCreateMapFromString_thenCreated() {
String input = "John=first,Adam=second";
Map<String, String> result = Splitter.on(",")
.withKeyValueSeparator("=")
.split(input);
assertEquals("first", result.get("John"));
assertEquals("second", result.get("Adam"));
}
7 guava还支持使用多个分隔符来分割字符,比如
@Test
public void whenSplitStringOnMultipleSeparator_thenSplit() {
String input = "apple.banana,,orange,,.";
List<String> result = Splitter.onPattern("[.|,]")
.omitEmptyStrings()
.splitToList(input);
assertThat(result, contains("apple", "banana", "orange"));
}
8 每隔多少个字符进行分隔
@Test
public void whenSplitStringOnSpecificLength_thenSplit() {
String input = "Hello world";
List<String> result = Splitter.fixedLength(3).splitToList(input);
assertThat(result, contains("Hel", "lo ", "wor", "ld"));
}
9 还可以让splitter在分割多少个字符后停止分隔,比如
@Test
public void whenLimitSplitting_thenLimited() {
String input = "a,b,c,d,e";
List<String> result = Splitter.on(",")
.limit(4)
.splitToList(input);
assertEquals(4, result.size());
assertThat(result, contains("a", "b", "c", "d,e"));
}