Eclipse CollectionsはあなたのJava開発を楽しくさせるような、
最高のJavaコレクションフレームワークです。
今すぐお好みのビルドツールを使ってあなたのJavaプロジェクトに導入してみてください!
<dependency> <groupId>org.eclipse.collections</groupId> <artifactId>eclipse-collections-api</artifactId> <version>10.4.0</version> </dependency> <dependency> <groupId>org.eclipse.collections</groupId> <artifactId>eclipse-collections</artifactId> <version>10.4.0</version> </dependency>
implementation 'org.eclipse.collections:eclipse-collections-api:10.4.0' implementation 'org.eclipse.collections:eclipse-collections:10.4.0'
<dependency org="org.eclipse.collections" name="eclipse-collections-api" rev="10.4.0" /> <dependency org="org.eclipse.collections" name="eclipse-collections" rev="10.4.0" />
コレクションから直接呼び出せる豊富なAPIを使って、Java 8のラムダ式やメソッド参照の簡潔さを最大限に活用することができます。
boolean anyPeopleHaveCats =
this.people
.anySatisfyWith(Person::hasPet, PetType.CAT);
int countPeopleWithCats =
this.people
.countWith(Person::hasPet, PetType.CAT);
MutableList<Person> peopleWithCats =
this.people
.selectWith(Person::hasPet, PetType.CAT)
boolean anyPeopleHaveCats =
this.people
.anySatisfy(person -> person.hasPet(PetType.CAT));
int countPeopleWithCats =
this.people
.count(person -> person.hasPet(PetType.CAT));
MutableList<Person> peopleWithCats =
this.people
.select(person -> person.hasPet(PetType.CAT));
boolean anyPeopleHaveCats =
this.people
.stream()
.anyMatch(person -> person.hasPet(PetType.CAT));
long countPeopleWithCats =
this.people
.stream()
.filter(person -> person.hasPet(PetType.CAT))
.count();
List<Person> peopleWithCats =
this.people
.stream()
.filter(person -> person.hasPet(PetType.CAT))
.collect(Collectors.toList());
Eclipse Collectionsのファクトリメソッドを活用して、様々な種類のコレクション型を可変性やプリミティブ型を指定して簡単につくることができます。
//可変Listをempty(), of(), with()メソッドを使用して初期化できます
MutableList<String> mutableListEmpty =
Lists.mutable.empty();
MutableList<String> mutableListOf =
Lists.mutable.of("One", "One", "Two", "Three");
MutableList<String> mutableListWith =
Lists.mutable.with("One", "One", "Two", "Three");
//List以外にもSet、Bag、Stack、Map、Multimap等、様々なコンテナ型が利用できます
MutableSet<String> mutableSet =
Sets.mutable.with("One", "One", "Two", "Three");
MutableBag<String> mutableBag =
Bags.mutable.with("One", "One", "Two", "Three");
MutableStack<String> mutableStack =
Stacks.mutable.with("One", "One", "Two", "Three");
MutableMap<String, String> mutableMap =
Maps.mutable.with("key1", "value1", "key2", "value2", "key3", "value3");
MutableMultimap<String, String> multimapWithList =
Multimaps.mutable.list.with("key1", "value1-1", "key1", "value1-2", "key2","value2-1");
MutableBiMap<String, String> mutableBiMap =
BiMaps.mutable.with("key1", "value1", "key2", "value2", "key3", "value3");
//不変Listをempty(), of(), with()メソッドを使用して初期化できます
ImmutableList<String> immutableListEmpty =
Lists.immutable.empty();
ImmutableList<String> immutableListOf =
Lists.immutable.of("One", "One", "Two", "Three");
ImmutableList<String> immutableListWith =
Lists.immutable.with("One", "One", "Two", "Three");
//List以外にもSet、Bag、Stack、Map、Multimap等、様々なコンテナ型が利用できます
ImmutableSet<String> immutableSet =
Sets.immutable.with("One", "One", "Two", "Three");
ImmutableBag<String> immutableBag =
Bags.immutable.with("One", "One", "Two", "Three");
ImmutableStack<String> immutableStack =
Stacks.immutable.with("One", "One", "Two", "Three");
ImmutableMap<String, String> immutableMap =
Maps.immutable.with("key1", "value1", "key2", "value2", "key3", "value3");
ImmutableMultimap<String, String> immutableMultimapWithList =
Multimaps.immutable.list.with("key1", "value1-1", "key1", "value1-2", "key2","value2-1");
ImmutableBiMap<String, String> immutableBiMap =
BiMaps.immutable.with("key1", "value1", "key2", "value2", "key3", "value3");
//不変、可変なList、Set、Bag、Stack、Mapが8個のプリミティブ型全てに対して利用できます
MutableIntList intList =
IntLists.mutable.of(1, 2, 3);
MutableLongList longList =
LongLists.mutable.of(1L, 2L, 3L);
MutableCharList charList =
CharLists.mutable.of('a', 'b', 'c');
MutableShortList shortList =
ShortLists.mutable.of((short)1, (short)2, (short)3);
MutableByteList byteList =
ByteLists.mutable.of((byte)1, (byte)2, (byte)3);
MutableBooleanList booleanList =
BooleanLists.mutable.of(true, false);
MutableFloatList floatList =
FloatLists.mutable.of(1.0f, 2.0f, 3.0f);
MutableDoubleList doubleList =
DoubleLists.mutable.of(1.0, 2.0, 3.0);
//IntIntervalを用いて範囲指定したintのコレクションをつくることができます
IntInterval oneTo10 =
IntInterval.fromTo(1, 10); // 1から10までのint
// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
IntInterval oneTo10By3 =
IntInterval.fromToBy(1, 10, 3); // 1から10までの3つおきのint
// [1, 4, 7, 10]
IntInterval oddsFrom1To10 =
IntInterval.oddsFromTo(1, 10); // 1から10までの奇数int
// [1, 3, 5, 7, 9]
IntInterval evensFrom1To10 =
IntInterval.evensFromTo(1, 10); // 1から10までの偶数int
// [2, 4, 6, 8, 10]
Eclipse CollectionsはSet、Map、全てのプリミティブコレクションにおいて、メモリ効率の良い実装になっています。
Eclipse Collections Kataを使うと Eclipse Collectionsの使用法を楽しく学ぶことができます。 Eclipse Collections Kataでは、Eclipse Collectionsを使って フェイルした状態のユニットテストをひとつひとつパスしていきます。 日本の武道の「型(形)」のように繰り返し練習することで基本的な使用法を学んでみましょう。
@Test
public void getFirstNamesOfAllPeople()
{
MutableList<Person> people = this.people;
MutableList<String> firstNames = null; //Replace null, with a transformation method on MutableList.
MutableList<String> expectedFirstNames =
Lists.mutable.with("Mary", "Bob", "Ted", "Jake", "Barry", "Terry", "Harry", "John");
Assert.assertEquals(expectedFirstNames, firstNames);
}
@Test
public void getFirstNamesOfAllPeople()
{
MutableList<Person> people = this.people;
MutableList<String> firstNames = people.collect(Person::getFirstName);
MutableList<String> expectedFirstNames =
Lists.mutable.with("Mary", "Bob", "Ted", "Jake", "Barry", "Terry", "Harry", "John");
Assert.assertEquals(expectedFirstNames, firstNames);
}
Eclipse Collectionsは、2004年にゴールドマン・サックス社内製のCaramelと呼ばれるコレクションフレームワークから始まりました。
このフレームワークは進化し、2012年に
GS CollectionsとしてGitHub上にオープンソースで公開されました。
GS Collectionsは2012年のJVM Summitや2014年のJavaOneなど、さまざまな
カンファレンス
にて発表されました。
また、2014年にはJava 8、Scala、GS Collectionsそれぞれの実装における並列遅延実行処理のパフォーマンス比較が
QCon New Yorkにて発表され、大きな注目を浴びました。
メディアにおいては、GS Collections開発者の
インタビュー記事
や、InfoQのGS Collections活用法を紹介した記事(日本語版Part1
・Part2)などが掲載されています。
10余年にわたり、同コレクションフレームワークには同社の40人余りもの開発者が携わってきました。
2015年、オープンソースプロジェクトとしての特徴を最大限に活かすため、GS CollectionsはEclipse Foundationに移管され、Eclipse Collectionsとして生まれ変わりました。
コミュニティからのコントリビューションを受け付ける、完全にオープンなフレームワークになったのです。
ライブラリの詳細なリファレンスガイドはこちら: Reference Guide.
ソースコードはGitHubにホストされています: Eclipse Collections.