Google Guava,善用已经造好的轮子

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

和 Apache Commons 有点儿类似,它也是包含了一系列的比如字符串、集合、反射、数学计算等的操作封装,还可以用作 JVM 缓存。

举几个例子说明:

1、new 各种对象

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

// 不可变集合
ImmutableList immutableList = ImmutableList.of("1", "2", "3");

2、列表转符号分隔的字符串

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

> 1-2-3

 3、求交集、并集、差集等

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

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

你可能感兴趣的:(Google Guava,善用已经造好的轮子)