Java之Google Guava使用舉例
阿新 • • 發佈:2020-12-14
簡介:
Guava工程包含了若干被Google的 Java專案廣泛依賴 的核心庫,例如:集合 [collections] 、快取 [caching] 、原生型別支援 [primitives support] 、併發庫 [concurrency libraries] 、通用註解 [common annotations] 、字串處理 [string processing] 、I/O 等等。 所有這些工具每天都在被Google的工程師應用在產品服務中。
中文教程:https://ifeve.com/google-guava/
依賴包:
<dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>29.0-jre</version> </dependency>
原始碼:
package tools.guava; import com.google.common.base.Joiner; import com.google.common.base.Splitter; import com.google.common.collect.ArrayListMultimap;import com.google.common.collect.HashMultimap; import com.google.common.collect.Lists; import com.google.common.collect.Multimap; import com.google.common.primitives.Chars; import com.google.common.primitives.Ints; import org.junit.Test; import java.awt.*; import java.util.List; import java.util.Set;import java.util.function.Supplier; import java.util.logging.Logger; public class MyGuava { private final Logger logger = Logger.getLogger(this.getClass().getName()); /** * 字串拼接 */ @Test public void testGuavaJoiner() { // 快速建立List List<String> list = Lists.newArrayList("a", "b", null, "c"); Joiner joiner = Joiner.on(",").skipNulls(); String join = joiner.join(list); logger.info(join); // a,b,c Joiner joiner2 = Joiner.on(",").useForNull("d"); String join2 = joiner2.join(list); logger.info(join2); // a,b,d,c /** * 跳過NULL元素:skipNulls() * 對於NULL元素使用其他替代:useForNull(String) * */ } /** * 字串分解 */ @Test public void testGuavaSplitter() { // 快速建立List String string = "a,b, ,c,"; Splitter splitter = Splitter.on(",").trimResults().omitEmptyStrings(); //輸出結果: a b c splitter.split(string).forEach(x -> logger.info(x)); } @Test public void testGuavaOpts() { // 快速建立List List<Integer> intList = Ints.asList(1, 2, 3, 4, 5); logger.info(intList.toString()); // 陣列的組裝:1,2,3 logger.info(Ints.join(",", 1, 2, 3)); // 原生型別資料快速合併 int[] newIntArray = Ints.concat(new int[]{1, 2}, new int[]{1, 2, 3}); logger.info(String.valueOf(newIntArray.length)); // 最大、最小值:3,1 logger.info(Ints.max(newIntArray) + "," + Ints.min(newIntArray)); // 是否包含:true logger.info(String.valueOf(Ints.contains(newIntArray, 3))); logger.info(String.valueOf(Chars.contains(new char[]{'a', 'b', 'c'}, 'c'))); // 集合轉換為陣列 int[] intArray = Ints.toArray(intList); } /** * 一對多鍵值對:multimap */ @Test public void testGuavaMultimap() { Multimap<String, String> multimap = ArrayListMultimap.create(); // Multimap<String, String> multimap = HashMultimap.create(); multimap.put("a", "1"); multimap.put("a", "11"); multimap.put("b", "2"); multimap.put("b", "22"); multimap.put("b", "222"); multimap.put("c", "3"); // 查詢 System.out.println(multimap); System.out.println(multimap.get("b")); System.out.println(multimap.get("b").toArray()[0]); System.out.println(((List<String>) multimap.get("b")).get(0)); // 遍歷-方法1 Set<String> keys = multimap.keySet(); for (String key : keys) { System.out.println("遍歷-1:" + multimap.get(key)); } // 遍歷-方法2 multimap.keySet().forEach(x -> System.out.println("遍歷-2:" + multimap.get(x))); // 刪除 multimap.remove("b", "2"); multimap.removeAll("a"); System.out.println(multimap); System.out.println(Lists.newArrayList(multimap.entries())); /** * 執行結果: * {a=[1, 11], b=[2, 22, 222], c=[3]} * [2, 22, 222] * 2 * 遍歷-1:[1, 11] * 遍歷-1:[2, 22, 222] * 遍歷-1:[3] * 遍歷-2:[1, 11] * 遍歷-2:[2, 22, 222] * 遍歷-2:[3] * {b=[22, 222], c=[3]} * [b=22, b=222, c=3] * */ } }