Guava项目涵括了好几个Google开发的JAVA项目所依赖的核心库,像: collections, caching, primitives support, concurrency libraries, common annotations, string processing, I/O等. 项目开发需要,所以学习一下。
PART1. Basic Utilities
第一. 对于null的case
1. Optional类的使用
Optional<Integer> possible = Optional.of(5);
possible.isPresent(); // returns true
possible.get(); // returns 5
Optional b = Optional.of(null);
b.get(); //fail fast, throw NullPointerException
b. isPresent();//fail fast, throw NullPointerException
Optional po = Optional.fromNullable(null);
po.isPresent(); //判断是否含有非null的值, false
Optional po = Optional.fromNullable(“22”);
po.isPresent(); // true
2. Strings类的使用
emptyToNull(String)
isNullOrEmpty(String)
nullToEmpty(String)
第二. Preconditions
checkArgument(boolean) // if true, do nothing, if false, throw IllegalArgumentException
checkNotNull(T) // if null, throw NullPointerException, else return T
checkElementIndex(int index, int size) // if index <0 or index >= size, throw IndexOutOfBoundsException, else return index
checkPositionIndex(int index, int size) // similar to checkElementIndex
第三. Ordering
class Foo {
@Nullable String sortedBy;
int notSortedBy;
}
Ordering<Foo> ordering = Ordering.natural().nullsFirst().onResultOf(new Function<Foo, String>() {
public String apply(Foo foo) {
return foo.sortedBy;
}
});
第四. Object common methods
Objects.equal("a", "a"); // returns true
Objects.equal(null, "a"); // returns false
Objects.equal("a", null); // returns false
Objects.equal(null, null); // returns true
此功能在JDK 7 上已经提供!
PART2. Collections
第一. Immutable Collection
| Interface | JDK or Guava? | Immutable Version |
| Collection | JDK | ImmutableCollection |
| List | JDK | ImmutableList |
| Set | JDK | ImmutableSet |
| SortedSet/NavigableSet | JDK | ImmutableSortedSet |
| Map | JDK | ImmutableMap |
| SortedMap | JDK | ImmutableSortedMap |
| Multiset | Guava | ImmutableMultiset |
| SortedMultiset | Guava | ImmutableSortedMultiset |
| Multimap | Guava | ImmutableMultimap |
| ListMultimap | Guava | ImmutableListMultimap |
| SetMultimap | Guava | ImmutableSetMultimap |
| BiMap | Guava | ImmutableBiMap |
| ClassToInstanceMap | Guava | ImmutableClassToInstanceMap |
| Table | Guava | ImmutableTable |
第二. New collection types
Multiset:
Multiset<String> wordsMultiset = HashMultiset.create();
wordsMultiset.add("11");
wordsMultiset.add("22");
wordsMultiset.add("33");
wordsMultiset.add("22");
wordsMultiset.add("22");
System.out.println(wordsMultiset.count("22")); //3
System.out.println(wordsMultiset.count("00")); //0
System.out.println(wordsMultiset.size()); //5
System.out.println(wordsMultiset.elementSet().size()); //3
SortedMultiset
Multimap
Multimap<String, String> myMultimap = ArrayListMultimap.create();
myMultimap.put("Fruits", "Bannana");
myMultimap.put("Fruits", "Apple");
myMultimap.put("Fruits", "Pear");
myMultimap.put("Vegetables", "Carrot");
System.out.println(myMultimap.size()); // 4
Collection<string> fruits = myMultimap.get("Fruits");
System.out.println(fruits); // [Bannana, Apple, Pear]
myMultimap.remove("Fruits","Pear");
System.out.println(myMultimap.get("Fruits")); // [Bannana, Pear]
// Remove all values for a key
myMultimap.removeAll("Fruits");
System.out.println(myMultimap.get("Fruits")); // [] (Empty Collection!)
BiMap: A BiMap<K, V> is a Map<K, V> that guarantees that values are unique
BiMap<String, String> map = HashBiMap.create();
map.put("1", "2");
//map.put("3", "2"); // if have this action, will throw IllegalArgumentException
map.put("1", "3"); // this cation will cause key:1, value 3 override the first put
RangeSet
RangeSet<Integer> rangeSet = TreeRangeSet.create();
rangeSet.add(Range.closed(1, 10)); // {[1, 10]}
rangeSet.add(Range.closedOpen(11, 15)); // {[1, 10], [11, 15)}
rangeSet.add(Range.open(15, 20)); // {[1, 10], [11, 20)}
rangeSet.add(Range.openClosed(0, 0)); // {[1, 10], [11, 20)}
rangeSet.remove(Range.open(5, 10)); // {[1, 5], [10, 10], [11, 20)}
RangeMap
RangeMap<Integer, String> rangeMap = TreeRangeMap.create();
rangeMap.put(Range.closed(1, 10), "foo"); // {[1, 10] => "foo"}
rangeMap.put(Range.open(3, 6), "bar"); // {[1, 3] => "foo", (3, 6) => "bar", [6, 10] => "foo"}
rangeMap.put(Range.open(10, 20), "foo"); // {[1, 3] => "foo", (3, 6) => "bar", [6, 10] => "foo", (10, 20) => "foo"}
rangeMap.remove(Range.closed(5, 11)); // {[1, 3] => "foo", (3, 5) => "bar", (11, 20) => "foo"}
第三. Utility Classes
Lists(以两个方法为例子)
List<Integer> countUp = Ints.asList(1, 2, 3, 4, 5);
List<Integer> countDown = Lists.reverse(theList); // {5, 4, 3, 2, 1}
List<List<Integer>> parts = Lists.partition(countUp, 2); // {{1, 2}, {3, 4}, {5}}
Sets(以三个方法为例子)
Set<String> set1 = ImmutableSet.of("one", "two", "three", "six", "seven", "eight");
Set<String> set2 = ImmutableSet.of("two", "three", "five", "seven");
System.out.println(Sets.union(set1, set2));//[one, two, three, six, seven, eight, five]
System.out.println(Sets.difference(set1, set2));//[one, six, eight]
System.out.println(Sets.intersection(set1, set2));//[two, three, seven]
Maps
Map<String, Integer> left = ImmutableMap.of("a", 1, "b", 2, "c", 3);
Map<String, Integer> right = ImmutableMap.of("b", 2, "c", 4, "d", 5);
MapDifference<String, Integer> diff = Maps.difference(left, right);
diff.entriesInCommon(); // {"b" => 2}
diff.entriesDiffering(); // {"c" => (3, 4)}
diff.entriesOnlyOnLeft(); // {"a" => 1}
diff.entriesOnlyOnRight(); // {"d" => 5}
Multisets
Multimaps
本文介绍了Google Guava库的核心功能,包括对null的处理、预条件检查、集合操作及实用工具类等内容。通过实例展示了如何使用Optional类避免空指针异常,如何利用Preconditions进行参数验证,以及Guava提供的多种集合类型如Multiset、Multimap和BiMap等的使用方法。
603

被折叠的 条评论
为什么被折叠?



