官方教程
https://wizardforcel.gitbooks.io/guava-tutorial/content/1.html
Maven 依赖
1 2 3 4 5
| <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>30.1.1-jre</version> </dependency>
|
使用
创建集合
1 2 3 4 5 6 7 8 9 10 11
| List<String> list = Lists.newArrayList(); List<Integer> list = Lists.newArrayList(1, 2, 3);
List<Integer> reverse = Lists.reverse(list); System.out.println(reverse);
List<List<Integer>> partition = Lists.partition(list, 10);
Map<String, String> map = Maps.newHashMap(); Set<String> set = Sets.newHashSet();
|
Multimap 一个 key 可以映射多个 value 的 HashMap
1 2 3 4 5 6 7
| Multimap<String, Integer> map = ArrayListMultimap.create(); map.put("key", 1); map.put("key", 2); Collection<Integer> values = map.get("key"); System.out.println(map);
Map<String, Collection<Integer>> collectionMap = map.asMap();
|
BiMap 一种连 value 也不能重复的 HashMap
1 2 3 4 5 6 7
| BiMap<String, String> biMap = HashBiMap.create();
biMap.put("key","value"); System.out.println(biMap);
BiMap<String, String> inverse = biMap.inverse(); System.out.println(inverse);
|
Table 一种有两个 key 的 HashMap
1 2 3 4 5 6 7 8 9 10 11
| Table<Integer, String, String> table = HashBasedTable.create(); table.put(18, "男", "yideng"); table.put(18, "女", "Lily"); System.out.println(table.get(18, "男"));
Map<String, String> row = table.row(18); System.out.println(row);
Map<Integer, String> column = table.column("男"); System.out.println(column);
|
Multiset 一种用来计数的 Set
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| Multiset<String> multiset = HashMultiset.create(); multiset.add("apple"); multiset.add("apple"); multiset.add("orange"); System.out.println(multiset.count("apple"));
Set<String> set = multiset.elementSet(); System.out.println(set);
Iterator<String> iterator = multiset.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); }
multiset.setCount("apple", 5);
|