概要
Guava は Google のコア Java ライブラリで、新しいコレクション型、不変コレクション、グラフライブラリ、および並行処理、IO、ハッシュ、プリミティブ型、文字列などのユーティリティを備えており、Java プロジェクトで広く使用され、JRE と Android 環境の両方をサポートしています。
README プレビュー
# Guava: Google Core Libraries for Java\n\n[](https://github.com/google/guava/releases/latest)\n[](https://github.com/google/guava/actions/workflows/ci.yml)\n[](https://www.bestpractices.dev/projects/7197)\n\n\n\nGuava is a set of core Java libraries from Google that includes new collection\ntypes (such as multimap and multiset), immutable collections, a graph library,\nand utilities for concurrency, I/O, hashing, primitives, strings, and more! It\nis widely used on most Java projects within Google, and widely used by many\nother companies as well.\n\n\n\nGuava comes in two flavors:\n\n* The JRE flavor requires JDK 1.8 or higher.\n* If you need support for Android, use\n [the Android flavor](https://github.com/google/guava/wiki/Android). You can\n find the Android Guava source in the [`android` directory].\n\n[`android` directory]: https://github.com/google/guava/tree/master/android\n\n## Adding Guava to your build\n\nGuava's Maven group ID is `com.google.guava`, and its artifact ID is `guava`.\nGuava provides two different "flavors": one for use on a (Java 8+) JRE and one\nfor use on Android or by any library that wants to be compatible with Android.\nThese flavors are specified in the Maven version field as either `33.6.0-jre` or\n`33.6.0-android`. For more about depending on Guava, see\n[using Guava in your build].\n\nTo add a dependency on Guava using Maven, use the following:\n\n```xml\n\n com.google.guava\n guava\n 33.6.0-jre\n \n 33.6.0-android\n\n```\n\nTo add a dependency using Gradle:\n\n```gradle\ndependencies {\n // Pick one:\n\n // 1. Use Guava in your implementation only:\n implementation("com.google.guava:guava:33.6.0-jre")\n\n // 2. Use Guava types in your public API:\n api("com.google.guava:guava:33.6.0-jre")\n\n // 3. Android - Use Guava in your implementation only:\n implementation("com.google.guava:guava:33.6.0-android")\n\n // 4. Android - Use Guava types in your public API:\n api("com.google.guava:guava:33.6.0-android")\FAQ (5)
informationalGuavaのMaps.newHashMapWithExpectedSizeおよびSets.newHashSetWithExpectedSizeに相当するJDKのものは何ですか?
Java 19以降を使用し、標準の静的ファクトリメソッドを呼び出してください:
HashMap<K, V> map = HashMap.newHashMap(expectedSize);
HashSet<E> set = HashSet.newHashSet(expectedSize);
これらは以下を置き換えるものです:
Maps.newHashMapWithExpectedSize(expectedSize);
Sets.newHashSetWithExpectedSize(expectedSize);
引数は想定されるエントリ数であり、内部テーブルの容量を受け取る new HashMap<>(capacity) とは異なります。Java 18以前では、Guavaのメソッドを引き続き使用するか、初期容量を手動で計算してください。
トラブル対応CopyOnWriteArrayList の Guava Lists.transform ビューをストリーム処理する際に発生する ConcurrentModificationException を修正する方法
これは 33.5.0-jre を含むバージョンにおける Guava のバグです。Lists.transform() は AbstractList ビューを返しますが、その spliterator() は背後のリストに委譲しないため、背後のスプリッテレータの CONCURRENT 特性が失われ、代わりに fail-fast のスプリッテレータが使用されます。
上流の修正では、TransformingRandomAccessList と TransformingSequentialList で spliterator() をオーバーライドし、CollectSpliterators.map(fromList.spliterator(), ...) を使用するようにしています。利用可能になったら、issue #8165 の修正を含む Guava リリースにアップグレードしてください。
それまでは、元の CopyOnWriteArrayList をストリーム化し、そのストリーム上でマッピングを適用してください:
// 次の代わりに:
Lists.transform(cow, function).stream().forEach(...);
// 次を使用:
cow.stream().map(function).forEach(...);
あるいは、トラバーサルがソースのイテラブルに委譲される Iterables.transform(cow, function) を使用するか、for-each ループで反復処理してください:
for (Integer value : Iterables.transform(cow, function)) {
// ...
}
CopyOnWriteArrayList のイテレータとスプリッテレータは弱い整合性(weakly consistent)を持つため、ソースリストを直接トラバースしても ConcurrentModificationException はスローされません。
informationalGuavaのFluentIterable.toSortedList(Comparator)によるソートは安定ですか?
はい。FluentIterable.toSortedList(Comparator) は Ordering.from(comparator).immutableSortedCopy(...) に委譲し、それがさらに ImmutableList.sortedCopyOf(...) に委譲します。これらのメソッドは安定ソートであることがドキュメントに記載されているため、比較結果が等しい要素は元の出現順序を保持します。toSortedList にその記述がなかったのは単なる Javadoc の欠落に過ぎません。この保証が明示的にドキュメント化されている最新の Guava バージョンを使用してください。
トラブル対応guava-gwt の unpack-dependencies 中に発生する Guava のビルドエラー「Artifact has not been packaged yet」を修正するにはどうすればよいですか?
例えば、Guavaプロジェクトのルートから、packageではなくinstallを指定してMavenを実行します:
mvn clean install -DskipTests
guava-gwtモジュールはmaven-dependency-plugin:unpack-dependenciesを使用しており、これにはリアクターの依存関係がローカルMavenリポジトリにインストールされている必要があります。mvn packageのみを実行すると、MDEP-98が発生します。それ以前のモジュールがすでにインストールされている場合は、次のコマンドで再開できます:
mvn install -rf :guava-gwtトラブル対応JDK 9 または 10.0.1 で Guava 33.7.0-jre を使用してコンパイルする際に jdk.zipfs の JarFileSystem で発生する NullPointerException を修正するにはどうすればよいですか?
Guava 33.7.0-jre はマニフェストに Multi-Release: true を宣言していますが、META-INF/versions/ を含んでいないため、古い jdk.zipfs バージョンのバグを誘発します。確認済みの回避策は、Guava 33.6.0-jre にダウングレードすることです。
Maven:
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>33.6.0-jre</version>
</dependency>
Gradle:
implementation 'com.google.guava:guava:33.6.0-jre'
古い JAR に期待されるマルチリリースファイルが含まれていることを確認してください:
unzip -l guava-33.6.0-jre.jar | grep META-INF/versions
修正された Guava リリースが使用可能になるまで、JDK 9.x または JDK 10.0.1 でコンパイルする際は Guava 33.7.0-jre を避けてください。