jar 包依赖
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>28.0-jre</version>
</dependency>
Joiner && Splitter
public class AnswerApp {
// 定义连接器, 可支持正则表达式
public static final Joiner JOINER = Joiner.on(",").useForNull("default");
public static final Joiner.MapJoiner MAP_JOINER = Joiner.on(",").withKeyValueSeparator("@");
// 定义分割器, 可支持正则表达式
// trimResults: 去掉元素中的空格 omitEmptyStrings: 忽略空字符 limit: 限制分割的个数
public static final Splitter SPLIT = Splitter.on(",").trimResults().omitEmptyStrings().limit(10);
// 定义固定长度分割器
public static final Splitter FIXLENGTH_SPLIT = Splitter.fixedLength(5);
public static final Splitter.MapSplitter MAP_SPLIT = Splitter.on(",").withKeyValueSeparator("@");
// 注意这里的固定长度5代表 key@value 的长度为 5
public static final Splitter.MapSplitter FIXLENGTH_MAP_SPLIT = Splitter.fixedLength(5).withKeyValueSeparator("@");
public static void main(String[] args) {
List<String> list = Lists.newArrayList("answer", "", "AnswerAIL", null);
System.out.println(JOINER.join(list));
System.out.println();
Map<String, String> map = ImmutableMap.of("name", "AnswerAIL", "email", "[email protected]");
System.out.println(MAP_JOINER.join(map));
System.out.println();
for (String str: SPLIT.split(" answer, ,,AnswerAIL ,code")) {
System.out.print(str + ">");
}
System.out.println("\n");
for (String str: FIXLENGTH_SPLIT.split("aaaaabbbbbccccc")) {
System.out.print(str + ">");
}
System.out.println("\n");
Map<String, String> params = MAP_SPLIT.split("name@AnswerAIL,address@pt");
System.out.println(params);
System.out.println();
System.out.println(FIXLENGTH_MAP_SPLIT.split("aa@a1bb@b2"));
}
}
程序运行结果
answer,,AnswerAIL,default
name@AnswerAIL,email@[email protected]
answer>AnswerAIL>code>
aaaaa>bbbbb>ccccc>
{name=AnswerAIL, address=pt}
{aa=a1, bb=b2}
Ints
public class AnswerApp {
public static void main(String[] args) {
// 快速转集合
List<Integer> nums = Ints.asList(1, 2, 3, 4, 5);
System.out.println(nums.size()); // 5
System.out.println();
// 集合转数组
int[] array = Ints.toArray(nums);
// 数组合并
int[] concat = Ints.concat(array, new int[]{3, 4, 5});
System.out.println(concat.length); // 8
System.out.println();
// 数组中是否包含某元素
System.out.println(Ints.contains(concat, 3)); // true
System.out.println();
// 数组元素最大值/最小值
System.out.println(Ints.max(new int[]{1, 5, 3})); // 5
System.out.println(Ints.min(new int[]{1, 5, 3})); // 1
System.out.println();
// 元素 3 在集合中出现的下标索引
System.out.println(Ints.indexOf(concat, 3)); // 2
// 元素 3 在集合中最后出现的下标索引
System.out.println(Ints.lastIndexOf(concat, 3)); // 5
}
}
Multiset 无序可重复的集合
public static void main(String[] args) {
Multiset multiset = HashMultiset.create();
multiset.add("a");
multiset.add("a");
multiset.add("b");
// 往集合中新增 n 个 元素
multiset.add("c", 5);
// 查看集合的大小
System.out.println(multiset.size()); // 8
// 查看集合内不同元素的个数(会对元素进行去重)
System.out.println(multiset.elementSet().size()); // 3
// 查看集合内某个元素的个数
System.out.println(multiset.count("c")); // 5
}
Multimap 实现 key-value 一对多
public static void main(String[] args) {
// Multimap 的实现类有: ArrayListMultimap/HashMultimap/LinkedHashMultimap/TreeMultimap/ImmutableMultimap/...
// Multimap 实现 key-value 一对多的情况
ArrayListMultimap<String, String> arrayListMultimap = ArrayListMultimap.create();
arrayListMultimap.put("name", "answer");
arrayListMultimap.put("name", "AnswerAIL");
arrayListMultimap.put("address", "pt");
// 2
System.out.println(arrayListMultimap.get("name").size());
// {address=[pt], name=[answer, AnswerAIL]}
System.out.println(arrayListMultimap.asMap());
}
BiMap
- find key by value: biMap.get()
- find value by key: biMap.inverse().get()
public static void main(String[] args) {
BiMap<String, String> biMap = HashBiMap.create();
biMap.put("userName", "Answer");
biMap.put("userName", "AnswerAIL");
// 会抛异常, 不允许 value 重复
// biMap.put("loginName", "AnswerAIL");
// 强制覆盖
biMap.forcePut("loginName", "AnswerAIL");
// 由于 value=AnswerAIL 被强制 loginName 覆盖, 所以 userName 对应的 value=null
System.out.println(biMap.get("userName")); // null
// 如果 key 不存在, 则使用默认 value
System.out.println(biMap.getOrDefault("email", "[email protected]"));
// 根据 value 查找 key
System.out.println(biMap.inverse().get("AnswerAIL")); // loginName
}
Table<Row, Column, Value>
public static void main(String[] args) {
Table<String, String, Integer> table = HashBasedTable.create();
table.put("iris", "语文", 88);
table.put("steven", "语文", 90);
table.put("james", "语文", 85);
table.put("iris", "数学", 98);
table.put("steven", "数学", 93);
table.put("james", "数学", 95);
table.put("iris", "英语", 90);
table.put("steven", "英语", 87);
table.put("james", "英语", 86);
System.out.println(table.row("iris"));
System.out.println(table.column("数学"));
System.out.println(table.get("iris", "数学"));
System.out.println();
System.out.println(table.containsRow("iris"));
System.out.println(table.containsColumn("数学"));
System.out.println(table.containsValue(100));
System.out.println();
System.out.println(table.rowKeySet());
System.out.println(table.rowMap());
System.out.println(table.columnKeySet());
System.out.println(table.columnMap());
}
程序运行时输出
{语文=88, 数学=98, 英语=90}
{iris=98, steven=93, james=95}
98
true
true
false
[iris, steven, james]
{iris={语文=88, 数学=98, 英语=90}, steven={语文=90, 数学=93, 英语=87}, james={语文=85, 数学=95, 英语=86}}
[语文, 数学, 英语]
{语文={iris=88, steven=90, james=85}, 数学={iris=98, steven=93, james=95}, 英语={iris=90, steven=87, james=86}}
本地缓存的实现