Google Code:Google guava工具類快速使用

Google Code:Google guava工具類快速使用

什麼是工具類

工具類 就是封裝平常用的方法,不需要你重複造輪子,節省開發人員時間,提高工作效率。谷歌作為大公司,當然會從日常的工作中提取中很多高效率的方法出來。

Google guava工具類特點

Guava是對Java API的補充,對Java開發中常用功能進行更優雅的實現,使得編碼更加輕鬆,代碼容易理解。Guava使用了多種設計模式,同時經過了很多測試,得到了越來越多開發團隊的青睞。Java最新版本的API採納了Guava的部分功能,但依舊無法替代。* 高效設計良好的API,被Google的開發者設計,實現和使用* 遵循高效的java語法實踐* 使代碼更刻度,簡潔,簡單* 節約時間,資源,提高生產力 Guava工程包含了若干被Google的 Java項目廣泛依賴 的核心庫,例如:1. 集合 [collections]2. 緩存 [caching]3. 原生類型支持 [primitives support]4. 併發庫 [concurrency libraries]5. 通用註解 [common annotations]6. 字符串處理 [string processing]7. I/O 等等。

Guava 使用(引入maven依賴)

pom.xml

 com.google.guava guava 19.0 

備註:版本大家可以自選

Guava 集合示例

普通的Collection集合

List list = Lists.newArrayList();Set set = Sets.newHashSet();Map map = Maps.newHashMap();

Guava的不可變集合創建

ImmutableList iList = ImmutableList.of("a", "b", "c");ImmutableSet iSet = ImmutableSet.of("e1", "e2");ImmutableMap iMap = ImmutableMap.of("k1", "v1", "k2", "v2");

immutable 不可變對象特點

1.在多線程操作下,是線程安全的。2.所有不可變集合會比可變集合更有效的利用資源。3.中途不可改變

Map-List 對比

普通寫法

1. Map> map = new HashMap>();2. List list = new ArrayList();3. list.add(1);4. list.add(2);5. map.put("test", list); System.out.println(map.get("test"));#需要5步,執行結果[1, 2]

Guava寫法

1. Multimap mapM = ArrayListMultimap.create();2. mapM.put("test",1);3. mapM.put("test",2); System.out.println(mapM.get("test"));#需要3步,執行結果[1, 2]

備註:執行結果都一樣,但是代碼少了近一半哦~~~

Guava 字符串連接器Joiner

連接多個字符串並追加到StringBuilder

StringBuilder stringBuilder = new StringBuilder("嗨,");// 字符串連接器,以|為分隔符,同時去掉null元素Joiner joiner1 = Joiner.on("|").skipNulls();// 構成一個字符串jim|jack|kevin並添加到stringBuilderstringBuilder = joiner1.appendTo(stringBuilder, "jim", "jack", null, "kevin");System.out.println(stringBuilder);  

執行結果:嗨,jim|jack|kevin

將Map轉化為字符串

Map testMap = Maps.newLinkedHashMap(); testMap.put("Cookies", "12332"); testMap.put("Content-Length", "30000"); testMap.put("Date", "2018.07.04"); testMap.put("Mime", "text/html");  // 用:分割鍵值對,並用#分割每個元素,返回字符串 String returnedString = Joiner.on("#").withKeyValueSeparator(":").join(testMap); System.out.println(returnedString);

執行結果:Cookies:12332#Content-Length:30000#Date:2018.07.04#Mime:text/html

將字符串轉化為Map

// 接上一個,內部類的引用,得到分割器,將字符串解析為mapSplitter.MapSplitter ms = Splitter.on("#").withKeyValueSeparator(':'); Map ret = ms.split(returnedString); for (String it2 : ret.keySet()) { System.out.println(it2 + " -> " + ret.get(it2)); }

執行結果:

Cookies -> 12332

Content-Length -> 30000

Date -> 2018.07.04

Mime -> text/html

字符串工具類Strings

System.out.println(Strings.isNullOrEmpty("")); // trueSystem.out.println(Strings.isNullOrEmpty(null)); // trueSystem.out.println(Strings.isNullOrEmpty("hello")); // false// 將null轉化為"" System.out.println(Strings.nullToEmpty(null)); // "" // 從尾部不斷補充T只到總共8個字符,如果源字符串已經達到或操作,則原樣返回。類似的有padStartSystem.out.println(Strings.padEnd("hello", 8, 'T')); // helloTTT 

字符匹配器CharMatcher

空白替換

// 空白回車換行對應換成一個#,一對一換String stringWithLinebreaks = "hello world\r\r\ryou are here\n\ntake it\t\t\teasy";String s6 = CharMatcher.BREAKING_WHITESPACE.replaceFrom(stringWithLinebreaks,'#');System.out.println(s6); 

執行結果:hello#world###you#are#here##take#it###easy

連續空白縮成一個字符

// 將所有連在一起的空白回車換行字符換成一個#,倒塌String tabString = " hello \n\t\tworld you\r\nare here ";String tabRet = CharMatcher.WHITESPACE.collapseFrom(tabString, '#');System.out.println(tabRet); 

執行結果: #hello#world#you#are#here#

去掉前後空白和縮成一個字符

// 在前面的基礎上去掉字符串的前後空白,並將空白換成一個#String trimRet = CharMatcher.WHITESPACE.trimAndCollapseFrom(tabString, '#');System.out.println(trimRet);

執行結果: hello#world#you#are#here

保留數字

String letterAndNumber = "1234abcdABCD56789";// 保留數字String number = CharMatcher.JAVA_DIGIT.retainFrom(letterAndNumber);System.out.println(number);

執行結果:123456789

備註

好了,就介紹到這裡了,大家經常用的工具類Guava完全可以滿足,讓代碼更簡潔高效。

參考文獻

Getting Started With Google Guava

關注我們

更多精彩內容請關注“IT實戰聯盟”哦~~~


分享到:


相關文章: