# learn-google-guava-demo **Repository Path**: baicaixiaozhan/learn-google-guava-demo ## Basic Information - **Project Name**: learn-google-guava-demo - **Description**: 学习 google guava - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-11-11 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ## Google guava ### 介绍 > **Guava is a set of core Java libraries from Google that includes new collection types (such as multimap and multiset), immutable collections, a graph library, and utilities for concurrency, I/O, hashing, caching, primitives, strings, and more! It is widely used on most Java projects within Google, and widely used by many other companies as well.** 提供两套使用: - `jdk 8+` - `jdk 7` 或者 `Android` ```xml com.google.guava guava 30.0-jre 30.0-android ``` --- ### Basic Utilities #### Preconditions | 方法 | 描述 | 失败时引发异常 | | ------------------------ | --------------------------------------------- | -------------------------- | | `checkArgument(boolean)` | `is true`检查,可用于检查方法参数 | `IllegalArgumentException` | | `checkNotNull(T)` | `value` 非空检查 | `NullPointerException` | | `checkState(boolean)` | `is true`检查,可用于检查对象是否处于某一状态 | `IllegalStateException` | | ... | ... | ... | #### `Optional` | 创建方法 | 描述 | | -------------------------- | ------------------------------------------------------------ | | `Optional.of(T)` | 使`Optiona`l包含给定的非`null`值,或者在`null`上快速失败(`fail-fast`)。 | | `Optional.absent()` | 返回没有包含引用的`Optional`实例。类似 `Java 8 Optional.empty` | | `Optional.fromNullable(T)` | 如果`T`为非`null`,则返回包含该引用的`Optional`实例;否则为`false` 返回`absent()`。类似 `Java 8 Optional.ofNullable` | | 查询(消费)方法 | 描述 | | --------------------- | ------------------------------------------------------------ | | `boolean isPresent()` | `Optional` 包含非空实例返回`true`,反之`false`。 | | `T get()` | 实例`T`存在就返回 `T `实例,反之抛出 `IllegalStateException`,`Java 8 Optional.get()`抛出`NoSuchElementException`。 | | `T or(T)` | 返回包含的实例(如果存在),否则返回`T`。 | | `T orNull()` | 返回包含的实例(如果存在),否则返回`null`。类似`Java 8 Optional.orElse(null)` | | `Set asSet()` | 返回一个包含此Optional中的实例的不可变单例`set`(如果存在),否则返回一个空的不可变`set`。 | #### `Object methods` - `MoreObjects.toStringHelper` - `equals` - `hashCode` ### `Strings` - `Splitter` - `Joiner` - `CaseFormat` | 格式 | 示例 | | ------------------ | ------------------ | | `LOWER_CAMEL` | `lowerCamel` | | `LOWER_HYPHEN` | `lower-hyphen` | | `LOWER_UNDERSCORE` | `lower_underscore` | | `UPPER_CAMEL` | `UpperCamel` | | `UPPER_UNDERSCORE` | `UPPER_UNDERSCORE` | ```java // returns "constantName" CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, "CONSTANT_NAME")); ``` ### `Collections` - `Immutable Collections` <===> `Collections.unmodifiable*` - 新集合类型(扩展) - `Multiset` - 类似`ArrayList`,可重复,不包含排序; - 类似`Map`, `E`表示元素,`Integer`表示元素的数量;**`Multiset Is Not A Map`** - `MultiMap` - 类似`Map>`或者`Map>`,将键映射到值的集合,但是每个键可以与多个值关联。 - **`Multimap Is Not A Map`** - `Multimap.get(key)`只会返回视图、集合视图,不会返回`null` - 可通过`asMap`转换成`Map>` ###### Implementations | 实现 | Keys 行为模式 | Values 行为模式 | | ----------------------- | --------------- | --------------- | | `ArrayListMultimap` | `HashMap` | `ArrayList` | | `HashMultimap` | `HashMap` | `HashSet` | | `LinkedListMultimap` | `LinkedHashMap` | `LinkedList` | | `LinkedHashMultimap` | `LinkedHashMap` | `LinkedHashSet` | | `TreeMultimap` | `TreeMap` | `TreeSet` | | `ImmutableListMultimap` | `ImmutableMap` | `ImmutableList` | | `ImmutableSetMultimap` | `ImmutableMap` | `ImmutableSet` | - `BiMap` - 实现`keys`和`values`交互映射,`get(keys) => values `、`get(values) => keys` - `Table` - 可解决类似`Map>`场景问题 `Collections Utilites` 对`JDK Collections Framework`的扩展 | 接口 | JDK / Guava | 对应 Guava 工具扩展类 | | ------------ | ----------- | --------------------- | | `Collection` | JDK | `Collections2` | | `List` | JDK | `Lists` | | `Set` | JDK | `Sets` | | `SortedSet` | JDK | `Sets` | | `Map` | JDK | `Maps` | | `SortedMap` | JDK | `Maps` | | `Queue` | JDK | `Queues` | | `Multiset` | Guava | `Multisets` | | `Multimap` | Guava | `Multimaps` | | `BiMap` | Guava | `Maps` | | `Table` | Guava | `Tables` | ### `Functional programming`