Guava工具類使用介紹

最近常用到guava工具的東西,每次都要百度查詢使用方法,現統一記錄一下方便本身使用。sql

參考文章:Google guava工具類的介紹和使用Guava工具類學習數據結構

引入依賴

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>28.0-jre</version>
</dependency>

<<最新依賴版本號點擊這裏獲取>>app

集合

建立集合

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

不可變集合

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

Multimap

Multimap的特色其實就是能夠包含有幾個重複Key的value,你能夠put進入多個不一樣value可是相同的key,可是又不是讓後面覆蓋前面的內容。

它的業務場景:當你須要構造像Map<K, List<V>>或者Map<K, Set<V>>這樣比較複雜的集合類型的數據結構,來作相應的業務邏輯處理。那Multimap在合適不過。工具

傳統作法:學習

//Item就是封裝的對象
Map<String,List<Item>> map = new HashMap<>();
for (Item item : list){
  List<Item> tmp = map.get(item.getName());
  if (null == tmp){
      tmp = new ArrayList<>();
      map.put(item.getName(),tmp);
  }
  tmp.add(item);
}

如今:ui

Multimap<String,Item> multiMap = ArrayListMultimap.create();
for (Item item : list){
    multiMap.put(item.getName(),item);
}

相似的還有:google

MultiSet: 無序+可重複   count()方法獲取單詞的次數  加強了可讀性+操做簡單
建立方式:  Multiset<String> set = HashMultiset.create();

Multimap: key-value  key能夠重複  
建立方式: Multimap<String, String> teachers = ArrayListMultimap.create();

BiMap: 雙向Map(Bidirectional Map) 鍵與值都不能重複
建立方式:  BiMap<String, String> biMap = HashBiMap.create();

Table: 雙鍵的Map Map--> Table-->rowKey+columnKey+value  //和sql中的聯合主鍵有點像
建立方式: Table<String, String, Integer> tables = HashBasedTable.create();

將集合轉換爲特定規則的字符串

List<String> list = new ArrayList<String>();
list.add("aa");
list.add("bb");
list.add("cc");
String result = Joiner.on("-").join(list);
//result爲  aa-bb-cc

把map集合轉換爲特定規則的字符串

Map<String, Integer> map = Maps.newHashMap();
map.put("xiaoming", 12);
map.put("xiaohong",13);
String result = Joiner.on(",").withKeyValueSeparator("=").join(map);
// result爲 xiaoming=12,xiaohong=13

將String轉換爲特定的集合

//use guava
String str = "1-2-3-4-5-6";
List<String> list = Splitter.on("-").splitToList(str);
//list爲  [1, 2, 3, 4, 5, 6]

String str = "1-2-3-4-  5-  6   ";  
List<String> list = Splitter.on("-").omitEmptyStrings().trimResults().splitToList(str);
System.out.println(list);
//[1, 2, 3, 4, 5, 6]  能夠忽略中間的空格

將String轉換爲map

String str = "xiaoming=11,xiaohong=23";
Map<String,String> map = Splitter.on(",").withKeyValueSeparator("=").split(str);

set的交集, 並集, 差集

HashSet setA = newHashSet(1, 2, 3, 4, 5);  
HashSet setB = newHashSet(4, 5, 6, 7, 8);  
   
SetView union = Sets.union(setA, setB);  
System.out.println("union:");  
for (Integer integer : union)  
    System.out.println(integer);           //union:12345867
   
SetView difference = Sets.difference(setA, setB);  
System.out.println("difference:");  
for (Integer integer : difference)  
    System.out.println(integer);        //difference:123
   
SetView intersection = Sets.intersection(setA, setB);  
System.out.println("intersection:");  
for (Integer integer : intersection)  
    System.out.println(integer);  //intersection:45

map的交集,並集,差集

MapDifference differenceMap = Maps.difference(mapA, mapB);  
differenceMap.areEqual();  
Map entriesDiffering = differenceMap.entriesDiffering();  
Map entriesOnlyOnLeft = differenceMap.entriesOnlyOnLeft();  
Map entriesOnlyOnRight = differenceMap.entriesOnlyOnRight();  
Map entriesInCommon = differenceMap.entriesInCommon();

MoreObjects

Person person = new Person("aa",11);
String str = MoreObjects.toStringHelper("Person").add("age", person.getAge()).toString();
System.out.println(str);  
//輸出Person{age=11}

字符串鏈接器Joiner

StringBuilder stringBuilder = new StringBuilder("hello");
// 字符串鏈接器,以|爲分隔符,同時去掉null元素
Joiner joiner1 = Joiner.on("|").skipNulls();
// 構成一個字符串foo|bar|baz並添加到stringBuilder
stringBuilder = joiner1.appendTo(stringBuilder, "foo", "bar", null, "baz");
System.out.println(stringBuilder); // hellofoo|bar|baz
相關文章
相關標籤/搜索