Google guava 集合 簡介
前言
Guava是一個Goolge開源的Java通用library,包含多種被google的java專案廣泛使用的核心庫,例如:集合、快取 、字串處理、I/O、算術運算、反射、多執行序等工具。本文將介紹如何導入guava lib 及 guava collection的使用。
環境
IDE: intelliJ
JDK: 1.8
Framework: Spring-boot.2.1.10.RELEASE
Build Tool: Maven 3
Server: tomcat8
導入library
Maven
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>28.1-jre</version>
<!-- or, for Android: -->
<version>28.1-android</version>
</dependency>
Gradle
dependencies {
// Pick one:
// 1. Use Guava in your implementation only:
implementation("com.google.guava:guava:28.1-jre")
// 2. Use Guava types in your public API:
api("com.google.guava:guava:28.1-jre")
// 3. Android - Use Guava in your implementation only:
implementation("com.google.guava:guava:28.1-android")
// 4. Android - Use Guava types in your public API:
api("com.google.guava:guava:28.1-android")
}
實作範例
ImmutableList
當我們的集合會被不可信任的函數調用時,一定會擔心集合內的資料被竄改,
Guava collection 為各集合提供了三種建立不可變集合的方法,讓你不用擔心資料被更動。
1. copyOf : 從原集合複製出不可變更的集合
List<String> animalList = new ArrayList<>();
animalList.add("cat");
animalList.add("dog");
ImmutableList<String> imutableUserList = ImmutableList.copyOf(animalList);
2. of : 直接建立不可變集合
ImmutableList<String> imutableUserList = ImmutableList.of("cat", "dog");
3. build : 另外一種直接建立的方法
ImmutableList<String> imutableUserList = ImmutableList.<String>builder()
.add("cat")
.add("dog")
.build();
若嘗試修改集合中的資料
ImmutableList<String> imutableUserList = ImmutableList.copyOf(animalList);
imutableUserList.add("bird");
會跳出 java.lang.UnsupportedOperationException
List
1. 過濾 : 依參數條件過濾集合
ArrayList<String> list = Lists.newArrayList("java", "ptt", "javaScript", "python", "json");
Collection<String> result = Collections2.filter(list, (e) -> e.endsWith("t"));
result.forEach(System.out::println);
2. 拆分 : 將list依照設定大小拆分
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
// 以size:2 拆分
List<List<Integer>> listPartitionList = Lists.partition(list, 2);
System.out.println("原 List:");
System.out.println(list);
System.out.println("拆分後:");
for (List<Integer> partition: listPartitionList) {
System.out.println(partition);
}
Set
Set<Integer> set1 = Sets.newHashSet(1, 2, 3);
Set<Integer> set2 = Sets.newHashSet(3, 4, 5);
1. 交集
Sets.SetView<Integer> intersection = Sets.intersection(set1, set2);
intersection.forEach(System.out::println);
2. 差集
Sets.SetView<Integer> difference = Sets.difference(set1, set2);
difference.forEach(System.out::println);
3. 並集
Sets.SetView<Integer> union = Sets.union(set1, set2);
union.forEach(System.out::println);
以下介紹guava加入的幾種新集合類型:
Multiset
與一般set不同,元素可重複
String str = "have have a nice nice day";
String[] ss = str.split(" ");
HashMultiset<String> multiset = HashMultiset.create();
for (String s : ss) {
multiset.add(s);
}
Set<String> set = multiset.elementSet();
// 查看相同元素的個數
set.forEach(s -> System.out.println(s + " : " + multiset.count(s)));
BiMap
key與value可互為鍵值,但鍵與值不可重複
BiMap<String, String> biMap = HashBiMap.create();
biMap.put("cat", "fish");
biMap.put("dog", "meat");
biMap.put("rabbit", "grass");
// 可使用 inverse 將 key、value 反轉
String key = biMap.inverse().get("fish");
System.out.println("key:" + key);
Table
擁有"row"和"column"兩個鍵值的Map
Table<Object, Object, Object> table = HashBasedTable.create();
table.put("Felix", "java", 95);
table.put("Eric", "python", 90);
table.put("Beef", "Android", 89);
table.put("Owen", "PHP", 88);
Set<Table.Cell<Object, Object, Object>> cells = table.cellSet();
cells.forEach(c -> System.out.println(c.getRowKey() + "-" + c.getColumnKey() + " : " + c.getValue()));
結語:
Guava Collection 提供了許多便捷的API可供使用,可以輕鬆的解決以往java collection內較難處理的情境,
若專案評估適用,加入Guava library絕對能讓工程師在開發上更為順暢。
參考:
https://github.com/google/guava
https://wizardforcel.gitbooks.io/guava-tutorial/content/1.html