Google Guava,善用已經造好的輪子

官方地址:https://github.com/google/guava

和 Apache Commons 有點兒類似,它也是包含了一系列的比如字符串、集合、反射、數學計算等的操作封裝,還可以用作 JVM 緩存。

舉幾個例子說明:

1、new 各種對象

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

// 不可變集合
ImmutableList<String> immutableList = ImmutableList.of("1", "2", "3");

2、列表轉符號分隔的字符串

List<String> list = new ArrayList<String>();
list.add("1");
list.add("2");
list.add("3");
String result = Joiner.on("-").join(list);

> 1-2-3

 3、求交集、並集、差集等

Set<Integer> set1 = Sets.newHashSet(1, 2, 3, 4, 5, 6);
Set<Integer> set2 = Sets.newHashSet(1,2,3,4);
       
Sets.SetView<Integer> intersection = Sets.intersection(set1, set2);//set1 和 set2 的交集

 原文出處:https://juejin.im/post/5edd9a33e51d45784635b1a8

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章