1. Guava是什么?
guava就是类库,是java api的增强与扩展,里面有大量的方法供我们使用,使用之前需要引入包
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>28.2-jre</version>
</dependency>
2. 字符串处理
2.1 joiner
joiner 连接器,将集合里元素组合成一个字符串
public class JoinerTest {
List<String> list1 = Arrays.asList("google", "baidu", "sohu");
List<String> list2 = Arrays.asList("aa", "bb", "cc", null);
Map<Integer, String> map;
@Test
public void testJoiner(){
// list转换成String,以#分隔
String result = Joiner.on("#").join(list1);
// 断言
assertThat(result, equalTo("google#baidu#sohu"));
}
@Test
public void testJoiner2(){
map = new HashMap<Integer, String>();
map.put(1, "a");
map.put(2, "b");
map.put(3, "c");
// map转换成String,以#分隔,key和value之间用:分隔
String result = Joiner.on("#").withKeyValueSeparator(":").join(map);
System.out.println(result);
// 断言
assertThat(result, equalTo("1:a#2:b#3:c"));
}
@Test
public void testJoinerWithNoValue(){
// list转换成String,以#分隔,跳过空格
String result = Joiner.on("#").skipNulls().join(list2);
// 断言
assertThat(result, equalTo("aa#bb#cc"));
}
@Test
public void testJoinerWithNoValueUseDefaultValue(){
// list转换成String,以#分隔,遇到null替换成"DEFAULT"
String result = Joiner.on("#").useForNull("DEFAULT").join(list2);
// 断言
assertThat(result, equalTo("aa#bb#cc#DEFAULT"));
}
@Test
public void testJoinerAppenedTo(){
// list转换成String,以#分隔,遇到null替换成"DEFAULT"
StringBuilder builder = new StringBuilder();
StringBuilder resultBuilder = Joiner.on("#").useForNull("DEFAULT").appendTo(builder, list2);
// 断言 判断同一个实例
assertThat(resultBuilder, sameInstance(builder));
assertThat(resultBuilder.toString(), equalTo("aa#bb#cc#DEFAULT"));
assertThat(builder.toString(), equalTo("aa#bb#cc#DEFAULT"));
}
}
https://github.com/wangwjt/guava/blob/master/src/test/java/JoinerTest.java
2.2 splitter
splitter拆分
public class SplitterTest {
/**
* 分隔字符串
*/
@Test
public void testSplitter(){
// 将字符串按指定字符分割
List<String> result = Splitter.on("@").splitToList("aa@bb@cc@dd@ee");
// 断言判断不空
assertThat(result, notNullValue());
// 断言判断list长度为5
assertThat(result.size(), equalTo(5));
// 断言判断各个元素
assertThat(result.get(0), equalTo("aa"));
assertThat(result.get(1), equalTo("bb"));
assertThat(result.get(2), equalTo("cc"));
assertThat(result.get(3), equalTo("dd"));
assertThat(result.get(4), equalTo("ee"));
}
/**
* 分隔字符串(忽略分隔后元素null的情况)
*/
@Test
public void testSplitter2(){
// 将字符串按指定字符分割 omitEmptyStrings()忽略掉null
List<String> result = Splitter.on("@").omitEmptyStrings().splitToList("aa@bb@cc@dd@ee@@@");
// 断言判断不空
assertThat(result, notNullValue());
// 断言判断list长度为5
assertThat(result.size(), equalTo(5));
// 断言判断各个元素
assertThat(result.get(0), equalTo("aa"));
assertThat(result.get(1), equalTo("bb"));
assertThat(result.get(2), equalTo("cc"));
assertThat(result.get(3), equalTo("dd"));
assertThat(result.get(4), equalTo("ee"));
}
/**
* 按指定长度截取元素
*/
@Test
public void testSplitter3(){
// 按2位截取放入list
List<String> list = Splitter.fixedLength(2).splitToList("abcdefg");
// 断言 判断不空
assertThat(list, notNullValue());
// 断言 判断长度
assertThat(list.size(), equalTo(4));
// 断言 判断元素
assertThat(list.get(0), equalTo("ab"));
assertThat(list.get(1), equalTo("cd"));
assertThat(list.get(2), equalTo("ef"));
}
/**
* 按指定符号截取
*/
@Test
public void testSplitter4(){
// 按指定符号截取
List<String> list = Splitter.on("#").limit(3).splitToList("aa#bb#cc#dd#ee#ff#gg");
// 断言 判断不空
assertThat(list, notNullValue());
// 断言 判断长度
assertThat(list.size(), equalTo(3));
// 断言 判断元素
assertThat(list.get(0), equalTo("aa"));
assertThat(list.get(1), equalTo("bb"));
assertThat(list.get(2), equalTo("cc#dd#ee#ff#gg"));
}
/**
* 按正则表达式规则截取
* trimResults()去空格
* omitEmptyStrings()忽略空
*/
@Test
public void testSplitter5(){
// 按正则表达式截取
List<String> list = Splitter.onPattern("\\|").trimResults().omitEmptyStrings().splitToList("aa|bb|cc|dd|ee|ff|gg| | ||");
// 断言 判断不空
assertThat(list, notNullValue());
// 断言 判断长度
assertThat(list.size(), equalTo(7));
}
/**
* 拆出来的放到map里
*/
@Test
public void testSplitter6(){
// 按正则表达式截取
Map<String, String> map = Splitter.onPattern("\\|").trimResults().omitEmptyStrings().withKeyValueSeparator("=").split("aa=6| |cc=dd|||ff=8|| | ||");
// 断言 判断不空
assertThat(map, notNullValue());
// 断言 判断长度
assertThat(map.size(), equalTo(3));
}
}
https://github.com/wangwjt/guava/blob/master/src/test/java/SplitterTest.java
3. 集合
3.1 Lists
public class ListTest {
/**
* 使用Lists.newArrayList构造list
*/
@Test
public void test1(){
// 用Lists.newArrayList方式创建对象
ArrayList<String> list = Lists.newArrayList("aa", "bb");
System.out.println(list);
}
/**
* 构造笛卡尔积
*/
@Test
public void test2(){
// 构造笛卡尔积
List<List<String>> lists = Lists.cartesianProduct(Lists.newArrayList("1", "2"),
Lists.newArrayList("a", "b"));
assertThat(lists.toString(), equalTo("[[1, a], [1, b], [2, a], [2, b]]"));
}
/**
* 逆序
*/
@Test
public void test3(){
ArrayList<String> list = Lists.newArrayList("a", "b", "c");
List<String> reverse = Lists.reverse(list);
assertThat(reverse.toString(), equalTo("[c, b, a]"));
}
/**
* 分组
*/
@Test
public void test4(){
ArrayList<String> list = Lists.newArrayList("a", "b", "c", "d", "e", "f", "g");
// 分组
List<List<String>> partition = Lists.partition(list, 3);
assertThat(partition.toString(), equalTo("[[a, b, c], [d, e, f], [g]]"));
}
}
https://github.com/wangwjt/guava/blob/master/src/test/java/ListTest.java
3.2 Sets
public class SetTest {
@Test
public void test1(){
// 创建set
HashSet<String> set = Sets.newHashSet("1", "2", "3");
assertThat(set.size(), equalTo(3));
ArrayList<String> list = Lists.newArrayList("a", "b", "b", "c");
// 由list数据创建set(去重)
HashSet<String> set2 = Sets.newHashSet(list);
assertThat(set2.size(), equalTo(3));
// 笛卡尔集
Set<List<String>> set3 = Sets.cartesianProduct(Sets.newHashSet("1", "2"), Sets.newHashSet("1", "3"));
assertThat(set3.toString(), equalTo("[[1, 1], [1, 3], [2, 1], [2, 3]]"));
// 产生子集
HashSet<String> set4 = Sets.newHashSet("1", "2", "3");
Set<Set<String>> combinations = Sets.combinations(set4, 2);
/**
* [1, 2]
* [1, 3]
* [2, 3]
*/
combinations.forEach(System.out::println);
}
/**
* 两个集合的差集
*/
@Test
public void test2(){
HashSet<Integer> set5 = Sets.newHashSet(1, 2, 3);
HashSet<Integer> set6 = Sets.newHashSet(1, 4, 5);
// 差集:在set5里不在set6里
Sets.SetView<Integer> difference = Sets.difference(set5, set6);
// [2, 3]
System.out.println(difference);
}
/**
* 两个集合的交集
*/
@Test
public void test3(){
HashSet<Integer> set7 = Sets.newHashSet(1, 2, 3);
HashSet<Integer> set8 = Sets.newHashSet(1, 4, 5);
Sets.SetView<Integer> intersection = Sets.intersection(set7, set8);
// [1]
System.out.println(intersection);
}
/**
* 两个集合的并集
*/
@Test
public void test4(){
HashSet<Integer> set1 = Sets.newHashSet(1, 2, 3);
HashSet<Integer> set2 = Sets.newHashSet(1, 4, 5);
Sets.SetView<Integer> intersection = Sets.union(set1, set2);
// [1, 2, 3, 4, 5]
System.out.println(intersection);
}
}
https://github.com/wangwjt/guava/blob/master/src/test/java/SetTest.java
3.3 MultiMap
特点:key可以相同,保存不同value。
业务场景:当需要构造像Map
public class MultimapTest {
/**
* ArrayListMultimap用法
* 注意:同一个key下,不同值可以同时存在
*/
@Test
public void test1(){
ArrayListMultimap<Integer, String> map = ArrayListMultimap.create();
map.put(1, "a");
map.put(1, "a");
map.put(1, "b");
map.put(1, "c");
map.put(2, "d");
map.put(3, "d");
map.put(3, "e");
// {1=[a, a, b, c], 2=[d], 3=[d, e]}
System.out.println(map);
}
/**
* HashMultimap用法
* 注意:同一个key下,不同值"不"可以同时存在
*/
@Test
public void test2(){
HashMultimap<Integer, String> map = HashMultimap.create();
map.put(1, "a");
map.put(1, "a");
map.put(1, "b");
map.put(1, "c");
map.put(2, "d");
map.put(3, "d");
map.put(3, "e");
// {1=[a, b, c], 2=[d], 3=[d, e]}
System.out.println(map);
}
}
https://github.com/wangwjt/guava/blob/master/src/test/java/MultimapTest.java
未完 待整理