学习官网:http://ifeve.com/google-guava/
目录
1. 基本工具
让使用Java语言变得更舒适
1.1 前置条件
让方法中的条件检查更简单。Preconditions类提供了一系列检查参数的方法。 相似Bean Validation,并且Bean Validation注解在方法声明处,更加清晰。Preconditions可以用在方法内,使代码看起来更整洁。
Preconditions.checkArgument(boolean);
1.2 排序: Guava强大的“流畅风格比较器”
链式调用可以自己创建不同场景下适用的比较器,可以运用比较器获取想要的结果
2. 集合
Guava对JDK集合的扩展
2.1 新集合类型
1. Multiset
是一种Collection类型,并履行了Collection接口相关的契约 public interface Multiset<E> extends Collection<E> {}
Multiset<Integer> 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 <E> HashMultiset<E> create() {
return new HashMultiset<E>();
}
private HashMultiset() {
super(new HashMap<E, Count>());
}
Multiset<E>不是Map<E, Integer>,虽然Map可能是某些Multiset实现的一部分。准确来说Multiset是一种Collection类型,并履行了Collection接口相关的契约。关于Multiset和Map的显著区别还包括:
- Multiset中的元素计数只能是正数。任何元素的计数都不能为负,也不能是0。elementSet()和entrySet()视图中也不会有这样的元素。
- multiset.size()返回集合的大小,等同于所有元素计数的总和。对于不重复元素的个数,应使用elementSet().size()方法。(因此,add(E)把multiset.size()增加1)
- multiset.iterator()会迭代重复元素,因此迭代长度等于multiset.size()。
- Multiset支持直接增加、减少或设置元素的计数。setCount(elem, 0)等同于移除所有elem。
- 对multiset 中没有的元素,multiset.count(elem)始终返回0。
2. Multimap
可看作 “键-单个值映射”的集合(a -> 1 a -> 2)或者“键-值集合映射”的映射(a -> [1, 2, 4] b -> 3)。不会有任何键映射到空集合。
Multimap<Integer, Object> multimap = ArrayListMultimap.create();
multimap.put(2, "a");// 键-单个值 // {2=[a]}
multimap.putAll(3, Lists.newArrayList("b", "d", "k"));// 键-值集合 // {2=[a], 3=[d, b, k]}
Collection<Object> key2 = multimap.get(2);// 以集合形式返回键所对应的值 // key2:["a"]
Collection<Object> key3 = multimap.get(3);// key3:["d","b","k"]
multimap.remove(3, "k");// 删除键到值的映射 // {2=[a], 3=[d, b]}
multimap.removeAll(2);// 清除键对应的所有值 // {3=[d, b]}
创建底层源码:
public static <K, V> ArrayListMultimap<K, V> create() {
return new ArrayListMultimap<>();
}
private ArrayListMultimap() {
this(12, DEFAULT_VALUES_PER_KEY);
}
private ArrayListMultimap(int expectedKeys, int expectedValuesPerKey) {
super(Platform.<K, Collection<V>>newHashMapWithExpectedSize(expectedKeys));
checkNonnegative(expectedValuesPerKey, "expectedValuesPerKey");
this.expectedValuesPerKey = expectedValuesPerKey;
}
static <K, V> Map<K, V> newHashMapWithExpectedSize(int expectedSize) {
return Maps.newHashMapWithExpectedSize(expectedSize);
}
public static <K, V> HashMap<K, V> newHashMapWithExpectedSize(int expectedSize) {
return new HashMap<>(capacity(expectedSize));
}
Multimap<K, V>不是Map<K,Collection<V>>
get(key)总是返回非null、但是可能空的集合。Multimap没有为相应的键花费内存创建了集合,而只是提供一个集合视图方便你为键增加映射值,如下实现:WrappedCollection
Collection<V> get(@Nullable K key);
// (List<V>)类型转换,不是真正的List集合
public List<V> get(@Nullable K key) {
return (List<V>) super.get(key);
}
// 生成一个修饰的集合,该集合与提供的键的多重映射中的值保持一致
public Collection<V> get(@Nullable K key) {
Collection<V> collection = map.get(key);
if (collection == null) {
collection = createCollection(key);
}
return wrapCollection(key, collection);
}
2.2 集合工具类
集合接口 | 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<String> list = Lists.newArrayList("hi", "nihaoya", "hello");
ImmutableMap<Integer, String> uniqueIndexMap = Maps.uniqueIndex(list, new Function<String, Integer>() {
@Nullable
@Override
public Integer apply(@Nullable String input) {
return Objects.requireNonNull(input).length();
}
});// uniqueIndexMap:{2:"hi",7:"nihaoya",5:"hello"}