グアバコレクションクックブック
1. 前書き
このクックブックの記事は、Guavaスタイルのコレクションを使用するためにsmall and focused recipes and code snippetsに編成されています。
形式はa growing list of code examplesの形式であり、追加の説明は必要ありません。これは、開発中にAPIの一般的な使用法に簡単にアクセスできるようにすることを目的としています。
2. レシピ
List
–note:これはJavaでの非共変の生成されたコレクションの回避策です**
class CastFunction implements Function {
@Override
public final T apply(final F from) {
return (T) from;
}
}
List originalList = Lists.newArrayList();
List theList = Lists.transform(originalList,
new CastFunction());
グアバなしのより簡単な代替–2つのキャスト操作を含む
List originalList = Lists.newArrayList();
List theList = (List) (List extends Number>) originalList;
コレクションにイテラブルを追加する
Iterable iter = Lists.newArrayList();
Collection collector = Lists.newArrayList();
Iterables.addAll(collector, iter);
カスタムマッチングルールに従って、コレクションに要素が含まれているかどうかを確認します
Iterable theCollection = Lists.newArrayList("a", "bc", "def");
boolean contains = Iterables.any(theCollection, new Predicate() {
@Override
public boolean apply(final String input) {
return input.length() == 1;
}
});
assertTrue(contains);
検索を使用した代替ソリューション
Iterable theCollection = Sets.newHashSet("a", "bc", "def");
boolean contains = Iterables.find(theCollection, new Predicate() {
@Override
public boolean apply(final String input) {
return input.length() == 1;
}
}) != null;
assertTrue(contains);
セットにのみ適用可能な代替ソリューション
Set theCollection = Sets.newHashSet("a", "bc", "def");
boolean contains = !Sets.filter(theCollection, new Predicate() {
@Override
public boolean apply(final String input) {
return input.length() == 1;
}
}).isEmpty();
assertTrue(contains);
何も見つからない場合のIterables.findのNoSuchElementException
Iterable theCollection = Sets.newHashSet("abcd", "efgh", "ijkl");
Predicate inputOfLengthOne = new Predicate() {
@Override
public boolean apply(final String input) {
return input.length() == 1;
}
};
String found = Iterables.find(theCollection, inputOfLengthOne);
–これはthe NoSuchElementException exceptionをスローします:
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:デフォルトの戻り値を引数として取るan overloaded find methodがあり、目的の動作のためにnullで呼び出すことができます。
String found = Iterables.find(theCollection, inputOfLengthOne, null);
コレクションからすべてのnull値を削除します
List values = Lists.newArrayList("a", null, "b", "c");
Iterable withoutNulls = Iterables.filter(values, Predicates.notNull());
不変のリスト/セット/マップを直接作成する
ImmutableList immutableList = ImmutableList.of("a", "b", "c");
ImmutableSet immutableSet = ImmutableSet.of("a", "b", "c");
ImmutableMap imuttableMap =
ImmutableMap.of("k1", "v1", "k2", "v2", "k3", "v3");
標準コレクションから不変のリスト/セット/マップを作成する
List muttableList = Lists.newArrayList();
ImmutableList immutableList = ImmutableList.copyOf(muttableList);
Set muttableSet = Sets.newHashSet();
ImmutableSet immutableSet = ImmutableSet.copyOf(muttableSet);
Map muttableMap = Maps.newHashMap();
ImmutableMap imuttableMap = ImmutableMap.copyOf(muttableMap);
ビルダーを使用した代替ソリューション
List muttableList = Lists.newArrayList();
ImmutableList immutableList =
ImmutableList. builder().addAll(muttableList).build();
Set muttableSet = Sets.newHashSet();
ImmutableSet immutableSet =
ImmutableSet. builder().addAll(muttableSet).build();
Map muttableMap = Maps.newHashMap();
ImmutableMap imuttableMap =
ImmutableMap. builder().putAll(muttableMap).build();
3. その他のグアバ料理本
Guavaは、包括的で非常に便利なライブラリです。クックブック形式でカバーされているAPIがさらにいくつかあります。
楽しい。
4. 今後
冒頭で述べたように、私はこのdifferent format – the cookbookを実験しており、Guavaコレクションを1か所で使用するという単純な一般的なタスクを収集しようとしています。 このフォーマットの焦点はシンプルさとスピードであるため、ほとんどのレシピにはno additional explanation other than the code example itselfがあります。
最後に、これをa living documentと見なしています。レシピと例に遭遇したときに、それらを追加し続けます。 コメントで詳細を提供してください。クックブックに組み込む予定です。
これらすべての例とコードスニペットcan be found over on GitHubの実装–これはMavenベースのプロジェクトであるため、そのままインポートして実行するのは簡単です。