1. Guava是什么?

guava就是类库,是java api的增强与扩展,里面有大量的方法供我们使用,使用之前需要引入包

  1. <dependency>
  2. <groupId>com.google.guava</groupId>
  3. <artifactId>guava</artifactId>
  4. <version>28.2-jre</version>
  5. </dependency>

2. 字符串处理

2.1 joiner

joiner 连接器,将集合里元素组合成一个字符串

  1. public class JoinerTest {
  2. List<String> list1 = Arrays.asList("google", "baidu", "sohu");
  3. List<String> list2 = Arrays.asList("aa", "bb", "cc", null);
  4. Map<Integer, String> map;
  5. @Test
  6. public void testJoiner(){
  7. // list转换成String,以#分隔
  8. String result = Joiner.on("#").join(list1);
  9. // 断言
  10. assertThat(result, equalTo("google#baidu#sohu"));
  11. }
  12. @Test
  13. public void testJoiner2(){
  14. map = new HashMap<Integer, String>();
  15. map.put(1, "a");
  16. map.put(2, "b");
  17. map.put(3, "c");
  18. // map转换成String,以#分隔,key和value之间用:分隔
  19. String result = Joiner.on("#").withKeyValueSeparator(":").join(map);
  20. System.out.println(result);
  21. // 断言
  22. assertThat(result, equalTo("1:a#2:b#3:c"));
  23. }
  24. @Test
  25. public void testJoinerWithNoValue(){
  26. // list转换成String,以#分隔,跳过空格
  27. String result = Joiner.on("#").skipNulls().join(list2);
  28. // 断言
  29. assertThat(result, equalTo("aa#bb#cc"));
  30. }
  31. @Test
  32. public void testJoinerWithNoValueUseDefaultValue(){
  33. // list转换成String,以#分隔,遇到null替换成"DEFAULT"
  34. String result = Joiner.on("#").useForNull("DEFAULT").join(list2);
  35. // 断言
  36. assertThat(result, equalTo("aa#bb#cc#DEFAULT"));
  37. }
  38. @Test
  39. public void testJoinerAppenedTo(){
  40. // list转换成String,以#分隔,遇到null替换成"DEFAULT"
  41. StringBuilder builder = new StringBuilder();
  42. StringBuilder resultBuilder = Joiner.on("#").useForNull("DEFAULT").appendTo(builder, list2);
  43. // 断言 判断同一个实例
  44. assertThat(resultBuilder, sameInstance(builder));
  45. assertThat(resultBuilder.toString(), equalTo("aa#bb#cc#DEFAULT"));
  46. assertThat(builder.toString(), equalTo("aa#bb#cc#DEFAULT"));
  47. }
  48. }

https://github.com/wangwjt/guava/blob/master/src/test/java/JoinerTest.java

2.2 splitter

splitter拆分

  1. public class SplitterTest {
  2. /**
  3. * 分隔字符串
  4. */
  5. @Test
  6. public void testSplitter(){
  7. // 将字符串按指定字符分割
  8. List<String> result = Splitter.on("@").splitToList("aa@bb@cc@dd@ee");
  9. // 断言判断不空
  10. assertThat(result, notNullValue());
  11. // 断言判断list长度为5
  12. assertThat(result.size(), equalTo(5));
  13. // 断言判断各个元素
  14. assertThat(result.get(0), equalTo("aa"));
  15. assertThat(result.get(1), equalTo("bb"));
  16. assertThat(result.get(2), equalTo("cc"));
  17. assertThat(result.get(3), equalTo("dd"));
  18. assertThat(result.get(4), equalTo("ee"));
  19. }
  20. /**
  21. * 分隔字符串(忽略分隔后元素null的情况)
  22. */
  23. @Test
  24. public void testSplitter2(){
  25. // 将字符串按指定字符分割 omitEmptyStrings()忽略掉null
  26. List<String> result = Splitter.on("@").omitEmptyStrings().splitToList("aa@bb@cc@dd@ee@@@");
  27. // 断言判断不空
  28. assertThat(result, notNullValue());
  29. // 断言判断list长度为5
  30. assertThat(result.size(), equalTo(5));
  31. // 断言判断各个元素
  32. assertThat(result.get(0), equalTo("aa"));
  33. assertThat(result.get(1), equalTo("bb"));
  34. assertThat(result.get(2), equalTo("cc"));
  35. assertThat(result.get(3), equalTo("dd"));
  36. assertThat(result.get(4), equalTo("ee"));
  37. }
  38. /**
  39. * 按指定长度截取元素
  40. */
  41. @Test
  42. public void testSplitter3(){
  43. // 按2位截取放入list
  44. List<String> list = Splitter.fixedLength(2).splitToList("abcdefg");
  45. // 断言 判断不空
  46. assertThat(list, notNullValue());
  47. // 断言 判断长度
  48. assertThat(list.size(), equalTo(4));
  49. // 断言 判断元素
  50. assertThat(list.get(0), equalTo("ab"));
  51. assertThat(list.get(1), equalTo("cd"));
  52. assertThat(list.get(2), equalTo("ef"));
  53. }
  54. /**
  55. * 按指定符号截取
  56. */
  57. @Test
  58. public void testSplitter4(){
  59. // 按指定符号截取
  60. List<String> list = Splitter.on("#").limit(3).splitToList("aa#bb#cc#dd#ee#ff#gg");
  61. // 断言 判断不空
  62. assertThat(list, notNullValue());
  63. // 断言 判断长度
  64. assertThat(list.size(), equalTo(3));
  65. // 断言 判断元素
  66. assertThat(list.get(0), equalTo("aa"));
  67. assertThat(list.get(1), equalTo("bb"));
  68. assertThat(list.get(2), equalTo("cc#dd#ee#ff#gg"));
  69. }
  70. /**
  71. * 按正则表达式规则截取
  72. * trimResults()去空格
  73. * omitEmptyStrings()忽略空
  74. */
  75. @Test
  76. public void testSplitter5(){
  77. // 按正则表达式截取
  78. List<String> list = Splitter.onPattern("\\|").trimResults().omitEmptyStrings().splitToList("aa|bb|cc|dd|ee|ff|gg| | ||");
  79. // 断言 判断不空
  80. assertThat(list, notNullValue());
  81. // 断言 判断长度
  82. assertThat(list.size(), equalTo(7));
  83. }
  84. /**
  85. * 拆出来的放到map里
  86. */
  87. @Test
  88. public void testSplitter6(){
  89. // 按正则表达式截取
  90. Map<String, String> map = Splitter.onPattern("\\|").trimResults().omitEmptyStrings().withKeyValueSeparator("=").split("aa=6| |cc=dd|||ff=8|| | ||");
  91. // 断言 判断不空
  92. assertThat(map, notNullValue());
  93. // 断言 判断长度
  94. assertThat(map.size(), equalTo(3));
  95. }
  96. }

https://github.com/wangwjt/guava/blob/master/src/test/java/SplitterTest.java

3. 集合

3.1 Lists

  1. public class ListTest {
  2. /**
  3. * 使用Lists.newArrayList构造list
  4. */
  5. @Test
  6. public void test1(){
  7. // 用Lists.newArrayList方式创建对象
  8. ArrayList<String> list = Lists.newArrayList("aa", "bb");
  9. System.out.println(list);
  10. }
  11. /**
  12. * 构造笛卡尔积
  13. */
  14. @Test
  15. public void test2(){
  16. // 构造笛卡尔积
  17. List<List<String>> lists = Lists.cartesianProduct(Lists.newArrayList("1", "2"),
  18. Lists.newArrayList("a", "b"));
  19. assertThat(lists.toString(), equalTo("[[1, a], [1, b], [2, a], [2, b]]"));
  20. }
  21. /**
  22. * 逆序
  23. */
  24. @Test
  25. public void test3(){
  26. ArrayList<String> list = Lists.newArrayList("a", "b", "c");
  27. List<String> reverse = Lists.reverse(list);
  28. assertThat(reverse.toString(), equalTo("[c, b, a]"));
  29. }
  30. /**
  31. * 分组
  32. */
  33. @Test
  34. public void test4(){
  35. ArrayList<String> list = Lists.newArrayList("a", "b", "c", "d", "e", "f", "g");
  36. // 分组
  37. List<List<String>> partition = Lists.partition(list, 3);
  38. assertThat(partition.toString(), equalTo("[[a, b, c], [d, e, f], [g]]"));
  39. }
  40. }

https://github.com/wangwjt/guava/blob/master/src/test/java/ListTest.java

3.2 Sets

  1. public class SetTest {
  2. @Test
  3. public void test1(){
  4. // 创建set
  5. HashSet<String> set = Sets.newHashSet("1", "2", "3");
  6. assertThat(set.size(), equalTo(3));
  7. ArrayList<String> list = Lists.newArrayList("a", "b", "b", "c");
  8. // 由list数据创建set(去重)
  9. HashSet<String> set2 = Sets.newHashSet(list);
  10. assertThat(set2.size(), equalTo(3));
  11. // 笛卡尔集
  12. Set<List<String>> set3 = Sets.cartesianProduct(Sets.newHashSet("1", "2"), Sets.newHashSet("1", "3"));
  13. assertThat(set3.toString(), equalTo("[[1, 1], [1, 3], [2, 1], [2, 3]]"));
  14. // 产生子集
  15. HashSet<String> set4 = Sets.newHashSet("1", "2", "3");
  16. Set<Set<String>> combinations = Sets.combinations(set4, 2);
  17. /**
  18. * [1, 2]
  19. * [1, 3]
  20. * [2, 3]
  21. */
  22. combinations.forEach(System.out::println);
  23. }
  24. /**
  25. * 两个集合的差集
  26. */
  27. @Test
  28. public void test2(){
  29. HashSet<Integer> set5 = Sets.newHashSet(1, 2, 3);
  30. HashSet<Integer> set6 = Sets.newHashSet(1, 4, 5);
  31. // 差集:在set5里不在set6里
  32. Sets.SetView<Integer> difference = Sets.difference(set5, set6);
  33. // [2, 3]
  34. System.out.println(difference);
  35. }
  36. /**
  37. * 两个集合的交集
  38. */
  39. @Test
  40. public void test3(){
  41. HashSet<Integer> set7 = Sets.newHashSet(1, 2, 3);
  42. HashSet<Integer> set8 = Sets.newHashSet(1, 4, 5);
  43. Sets.SetView<Integer> intersection = Sets.intersection(set7, set8);
  44. // [1]
  45. System.out.println(intersection);
  46. }
  47. /**
  48. * 两个集合的并集
  49. */
  50. @Test
  51. public void test4(){
  52. HashSet<Integer> set1 = Sets.newHashSet(1, 2, 3);
  53. HashSet<Integer> set2 = Sets.newHashSet(1, 4, 5);
  54. Sets.SetView<Integer> intersection = Sets.union(set1, set2);
  55. // [1, 2, 3, 4, 5]
  56. System.out.println(intersection);
  57. }
  58. }

https://github.com/wangwjt/guava/blob/master/src/test/java/SetTest.java

3.3 MultiMap

特点:key可以相同,保存不同value。
业务场景:当需要构造像Map>或者Map>这样比较复杂的数据结构时,可以采用Multimap

  1. public class MultimapTest {
  2. /**
  3. * ArrayListMultimap用法
  4. * 注意:同一个key下,不同值可以同时存在
  5. */
  6. @Test
  7. public void test1(){
  8. ArrayListMultimap<Integer, String> map = ArrayListMultimap.create();
  9. map.put(1, "a");
  10. map.put(1, "a");
  11. map.put(1, "b");
  12. map.put(1, "c");
  13. map.put(2, "d");
  14. map.put(3, "d");
  15. map.put(3, "e");
  16. // {1=[a, a, b, c], 2=[d], 3=[d, e]}
  17. System.out.println(map);
  18. }
  19. /**
  20. * HashMultimap用法
  21. * 注意:同一个key下,不同值"不"可以同时存在
  22. */
  23. @Test
  24. public void test2(){
  25. HashMultimap<Integer, String> map = HashMultimap.create();
  26. map.put(1, "a");
  27. map.put(1, "a");
  28. map.put(1, "b");
  29. map.put(1, "c");
  30. map.put(2, "d");
  31. map.put(3, "d");
  32. map.put(3, "e");
  33. // {1=[a, b, c], 2=[d], 3=[d, e]}
  34. System.out.println(map);
  35. }
  36. }

https://github.com/wangwjt/guava/blob/master/src/test/java/MultimapTest.java

未完 待整理