グアバコレクションクックブック
1. 序章
このクックブックの記事は、Guavaスタイルのコレクションを使用するための小さくて焦点を絞ったレシピとコードスニペットにまとめられています。
形式は、コード例の増加するリストの形式であり、追加の説明は必要ありません。これは、開発中にAPIの一般的な使用法に簡単にアクセスできるようにすることを目的としています。
2. レシピ
リストをダウンキャスト
– note :これはJavaの非共変生成コレクションの回避策です。
class CastFunction<F, T extends F> implements Function<F, T> {
@Override
public final T apply(final F from) {
return (T) from;
}
}
List<TypeParent> originalList = Lists.newArrayList();
List<TypeChild> theList = Lists.transform(originalList,
new CastFunction<TypeParent, TypeChild>());
Guavaなしのよりシンプルな代替–2つのキャスト操作を含む
List<Number> originalList = Lists.newArrayList();
List<Integer> theList = (List<Integer>) (List<? extends Number>) originalList;
コレクションにイテラブルを追加する
Iterable<String> iter = Lists.newArrayList();
Collection<String> collector = Lists.newArrayList();
Iterables.addAll(collector, iter);
カスタムマッチングルールに従って、コレクションに要素が含まれているかどうかを確認します
Iterable<String> theCollection = Lists.newArrayList("a", "bc", "def");
boolean contains = Iterables.any(theCollection, new Predicate<String>() {
@Override
public boolean apply(final String input) {
return input.length() == 1;
}
});
assertTrue(contains);
検索を使用した代替ソリューション
Iterable<String> theCollection = Sets.newHashSet("a", "bc", "def");
boolean contains = Iterables.find(theCollection, new Predicate<String>() {
@Override
public boolean apply(final String input) {
return input.length() == 1;
}
}) != null;
assertTrue(contains);
セットにのみ適用可能な代替ソリューション
Set<String> theCollection = Sets.newHashSet("a", "bc", "def");
boolean contains = !Sets.filter(theCollection, new Predicate<String>() {
@Override
public boolean apply(final String input) {
return input.length() == 1;
}
}).isEmpty();
assertTrue(contains);
何も見つからない場合のIterables.findのNoSuchElementException
Iterable<String> theCollection = Sets.newHashSet("abcd", "efgh", "ijkl");
Predicate<String> inputOfLengthOne = new Predicate<String>() {
@Override
public boolean apply(final String input) {
return input.length() == 1;
}
};
String found = Iterables.find(theCollection, inputOfLengthOne);
–これにより、NoSuchElementException例外がスローされます。
java.util.NoSuchElementException
at com.google.common.collect.AbstractIterator.next(AbstractIterator.java:154)
at com.google.common.collect.Iterators.find(Iterators.java:712)
at com.google.common.collect.Iterables.find(Iterables.java:643)
– solution :デフォルトの戻り値を引数として取るオーバーロードされたfindメソッドがあり、目的の動作のためにnullで呼び出すことができます。
String found = Iterables.find(theCollection, inputOfLengthOne, null);
コレクションからすべてのnull値を削除します
List<String> values = Lists.newArrayList("a", null, "b", "c");
Iterable<String> withoutNulls = Iterables.filter(values, Predicates.notNull());
不変のリスト/セット/マップを直接作成する
ImmutableList<String> immutableList = ImmutableList.of("a", "b", "c");
ImmutableSet<String> immutableSet = ImmutableSet.of("a", "b", "c");
ImmutableMap<String, String> imuttableMap =
ImmutableMap.of("k1", "v1", "k2", "v2", "k3", "v3");
標準コレクションから不変のリスト/セット/マップを作成する
List<String> muttableList = Lists.newArrayList();
ImmutableList<String> immutableList = ImmutableList.copyOf(muttableList);
Set<String> muttableSet = Sets.newHashSet();
ImmutableSet<String> immutableSet = ImmutableSet.copyOf(muttableSet);
Map<String, String> muttableMap = Maps.newHashMap();
ImmutableMap<String, String> imuttableMap = ImmutableMap.copyOf(muttableMap);
ビルダーを使用した代替ソリューション
List<String> muttableList = Lists.newArrayList();
ImmutableList<String> immutableList =
ImmutableList.<String> builder().addAll(muttableList).build();
Set<String> muttableSet = Sets.newHashSet();
ImmutableSet<String> immutableSet =
ImmutableSet.<String> builder().addAll(muttableSet).build();
Map<String, String> muttableMap = Maps.newHashMap();
ImmutableMap<String, String> imuttableMap =
ImmutableMap.<String, String> builder().putAll(muttableMap).build();
3. その他のグアバ料理本
Guavaは、包括的で非常に便利なライブラリです。クックブック形式でカバーされているAPIがさらにいくつかあります。
楽しみ。
4. 今後
冒頭で述べたように、私はこの異なる形式であるクックブックを試し、Guavaコレクションを1か所で使用するという単純な一般的なタスクを収集しようとしています。 この形式の焦点は単純さとスピードであるため、ほとんどのレシピにはコード例自体以外の追加の説明はありません。
最後に–私はこれを生きているドキュメントとして見ています–レシピと例に出くわすたびに、それらを追加し続けます。 コメントでもっと自由に提供してください、そして私はそれらをクックブックに組み込むことを考えます。
これらすべての例とコードスニペットの実装は、GitHub にあります。これはMavenベースのプロジェクトであるため、そのままインポートして実行するのは簡単です。