学习官网:http://ifeve.com/google-guava/
目录
1. 基本工具
1.1 前置条件
1.2 排序: Guava强大的“流畅风格比较器”
2. 集合
2.1 新集合类型
2.2 集合工具类
让使用Java语言变得更舒适
让方法中的条件检查更简单。Preconditions类提供了一系列检查参数的方法。 相似Bean Validation,并且Bean Validation注解在方法声明处,更加清晰。Preconditions可以用在方法内,使代码看起来更整洁。
Preconditions.checkArgument(boolean);
链式调用可以自己创建不同场景下适用的比较器,可以运用比较器获取想要的结果
可见:guava-Ordering排序器
Guava对JDK集合的扩展
1. Multiset
是一种Collection类型,并履行了Collection接口相关的契约 public interface Multiset
Multiset multiset = HashMultiset.create();// 创建
multiset.add(10); // multiset:[10]
multiset.add(3);// 添加元素 // multiset:[3,10]
multiset.add(6, 3);// 添加多个给定元素 // multiset:[3,6,6,6,10]
multiset.remove(6);// 删除元素 // multiset:[3,6,6,10]
multiset.remove(3, 2);// 删除多个给定元素 // multiset:[6,6,10]
multiset.remove(6, 2); // multiset:[10]
multiset.setCount(2, 2);// 设置给定元素在Multiset中的计数 // multiset:[2,2,10]
Integer c = multiset.count(2);// 获取指定元素的个数 // 2
创建底层实现源码:
public static HashMultiset create() {
return new HashMultiset();
}
private HashMultiset() {
super(new HashMap());
}
Multiset
2. Multimap
可看作 “键-单个值映射”的集合(a -> 1 a -> 2)或者“键-值集合映射”的映射(a -> [1, 2, 4] b -> 3)。不会有任何键映射到空集合。
Multimap multimap = ArrayListMultimap.create();
multimap.put(2, "a");// 键-单个值 // {2=[a]}
multimap.putAll(3, Lists.newArrayList("b", "d", "k"));// 键-值集合 // {2=[a], 3=[d, b, k]}
Collection
创建底层源码:
public static ArrayListMultimap create() {
return new ArrayListMultimap<>();
}
private ArrayListMultimap() {
this(12, DEFAULT_VALUES_PER_KEY);
}
private ArrayListMultimap(int expectedKeys, int expectedValuesPerKey) {
super(Platform.>newHashMapWithExpectedSize(expectedKeys));
checkNonnegative(expectedValuesPerKey, "expectedValuesPerKey");
this.expectedValuesPerKey = expectedValuesPerKey;
}
static Map newHashMapWithExpectedSize(int expectedSize) {
return Maps.newHashMapWithExpectedSize(expectedSize);
}
public static HashMap newHashMapWithExpectedSize(int expectedSize) {
return new HashMap<>(capacity(expectedSize));
}
Multimap
get(key)总是返回非null、但是可能空的集合。Multimap没有为相应的键花费内存创建了集合,而只是提供一个集合视图方便你为键增加映射值,如下实现:WrappedCollection
Collection get(@Nullable K key);
// (List)类型转换,不是真正的List集合
public List get(@Nullable K key) {
return (List) super.get(key);
}
// 生成一个修饰的集合,该集合与提供的键的多重映射中的值保持一致
public Collection get(@Nullable K key) {
Collection collection = map.get(key);
if (collection == null) {
collection = createCollection(key);
}
return wrapCollection(key, collection);
}
集合接口 | JDK/Guava | 对应的Guava工具类 | 提供方法 |
Collection | JDK | Collections2 | |
List | JDK | Lists | newArrayList();// 静态工厂方法 |
Set | JDK | Sets | |
SortedSet | JDK | Sets | |
Map | JDK | Maps | newHashMap(); newHashMapWithExpectedSize(10); uniqueIndex(list, func);// 根据func获取到list中每项唯一属性值生成map,见代码1 |
SortedMap | JDK | Maps | |
Queue | JDK | Queues | |
Multiset | Guava | Multisets | |
Multimap | Guava | Multimaps | |
BiMap | Guava | Maps | |
Table | Guava | Tables |
代码1:
List list = Lists.newArrayList("hi", "nihaoya", "hello");
ImmutableMap uniqueIndexMap = Maps.uniqueIndex(list, new Function() {
@Nullable
@Override
public Integer apply(@Nullable String input) {
return Objects.requireNonNull(input).length();
}
});// uniqueIndexMap:{2:"hi",7:"nihaoya",5:"hello"}