guava处理字符串与List之间,字符串与map之间的转换<转>
1 import static org.junit.Assert.*; 2 3 import java.util.List; 4 import java.util.Map; 5 6 import org.junit.Test; 7 8 import com.google.common.base.Joiner; 9 import com.google.common.base.Splitter; 10 import com.google.common.collect.Lists; 11 import com.google.common.collect.Maps; 12 13 14 public class Guava { 15 16 /** 17 * list转换为字符串 18 */ 19 @Test 20 public void joinTest(){ 21 List<String> names = Lists.newArrayList("John", "Jane", "Adam", "Tom"); 22 String result = Joiner.on(",").join(names); 23 24 assertEquals(result, "John,Jane,Adam,Tom"); 25 } 26 27 28 /** 29 * map转换为字符串 30 */ 31 @Test 32 public void whenConvertMapToString_thenConverted() { 33 Map<String, Integer> salary = Maps.newHashMap(); 34 salary.put("John", 1000); 35 salary.put("Jane", 1500); 36 String result = Joiner.on(" , ").withKeyValueSeparator(" = ") 37 .join(salary); 38 System.out.println(result); 39 } 40 41 /** 42 * list转String,跳过null 43 */ 44 @Test 45 public void whenConvertListToStringAndSkipNull_thenConverted() { 46 List<String> names = Lists.newArrayList("John", null, "Jane", "Adam", "Tom"); 47 String result = Joiner.on(",").skipNulls().join(names); 48 System.out.println(result); 49 assertEquals(result, "John,Jane,Adam,Tom"); 50 } 51 52 /** 53 * list转String,将null变成其他值 54 */ 55 @Test 56 public void whenUseForNull_thenUsed() { 57 List<String> names = Lists.newArrayList("John", null, "Jane", "Adam", "Tom"); 58 String result = Joiner.on(",").useForNull("nameless").join(names); 59 System.out.println(result); 60 assertEquals(result, "John,nameless,Jane,Adam,Tom"); 61 } 62 63 /** 64 * String to List 65 */ 66 @Test 67 public void whenCreateListFromString_thenCreated() { 68 String input = "apple - banana - orange"; 69 List<String> result = Splitter.on("-").trimResults().splitToList(input); 70 System.out.println(result); 71 //assertThat(result, contains("apple", "banana", "orange")); 72 } 73 74 /** 75 * String to Map 76 */ 77 @Test 78 public void whenCreateMapFromString_thenCreated() { 79 String input = "John=first,Adam=second"; 80 Map<String, String> result = Splitter.on(",") 81 .withKeyValueSeparator("=") 82 .split(input); 83 84 assertEquals("first", result.get("John")); 85 assertEquals("second", result.get("Adam")); 86 } 87 88 /** 89 * 多个字符进行分割 90 */ 91 @Test 92 public void whenSplitStringOnMultipleSeparator_thenSplit() { 93 String input = "apple.banana,,orange,,."; 94 List<String> result = Splitter.onPattern("[.|,]") 95 .omitEmptyStrings() 96 .splitToList(input); 97 System.out.println(result); 98 } 99 100 /** 101 * 每隔多少字符进行分割 102 */ 103 @Test 104 public void whenSplitStringOnSpecificLength_thenSplit() { 105 String input = "Hello world"; 106 List<String> result = Splitter.fixedLength(3).splitToList(input); 107 System.out.println(result); 108 } 109 110 /** 111 * 限制分割多少字后停止 112 */ 113 @Test 114 public void whenLimitSplitting_thenLimited() { 115 String input = "a,b,c,d,e"; 116 List<String> result = Splitter.on(",") 117 .limit(4) 118 .splitToList(input); 119 120 assertEquals(4, result.size()); 121 System.out.println(result); 122 } 123 124 125 126 127 }
转自http://blog.csdn.net/sunhuwh/article/details/41291515