Eclipse Collections

- 满足你所有需求的集合包 -


Eclipse Collections入门须知

Eclipse Collections 是最完善的Java集合框架, 它会为你的Java 编程带来无限的乐趣。

现在就使用你最喜欢的构建工具把它运用在你的Java项目中吧!

<dependency>
  <groupId>org.eclipse.collections</groupId>
  <artifactId>eclipse-collections-api</artifactId>
  <version>11.1.0</version>
</dependency>

<dependency>
  <groupId>org.eclipse.collections</groupId>
  <artifactId>eclipse-collections</artifactId>
  <version>11.1.0</version>
</dependency>
implementation 'org.eclipse.collections:eclipse-collections-api:11.1.0'
implementation 'org.eclipse.collections:eclipse-collections:11.1.0'
<dependency org="org.eclipse.collections" name="eclipse-collections-api" rev="11.1.0" />
<dependency org="org.eclipse.collections" name="eclipse-collections" rev="11.1.0" />

简单明了而且功能强大的APIs

您可以直接调用集合中丰富的API让Java 8 Lambda表达式和方法引用发挥到极致。

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());

拥有不同种类的容器,其中包括
可变和不可变集合,原始类型集合, bimaps, multimaps 和 bags

通过Eclipse Collections的工厂方法,您可以轻松地创建各种集合类型。

//使用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");

//不同容器的示例
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");
//使用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");

//不同容器的示例
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");
//全部8种原始类型都有相应的可变与不可变的Lists, Sets, Bags, Stacks 和 Maps
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创建指定范围的整数集合
IntInterval oneTo10 =
  IntInterval.fromTo(1, 10); // 从 1 到 10 的整数
// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
IntInterval oneTo10By3 =
  IntInterval.fromToBy(1, 10, 3); // 从 1 到 10 间隔为3的整数
// [1, 4, 7, 10]
IntInterval oddsFrom1To10 =
  IntInterval.oddsFromTo(1, 10); // 从 1 到 10 的奇数
// [1, 3, 5, 7, 9]
IntInterval evensFrom1To10 =
  IntInterval.evensFromTo(1, 10); // 从 1 到 10 的偶数
// [2, 4, 6, 8, 10]

使用高效的容器来降低内存空间

Eclipse Collections拥有内存优化的Sets, Maps和原始类型集合。


通过Kata学习Eclipse Collections

请参考Eclipse Collections Kata, 一种学习Eclipse Collections常用方法的有趣途径。 我们给这个kata练习定制了一系列失败的单元测试。 你的任务是运用Eclipse Collections提供的方法让单元测试通过。

@Test
public void getFirstNamesOfAllPeople()
{
  MutableList<Person> people = this.people;
  MutableList<String> firstNames = null; //用MutableList上的转化方法来替代null。
  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的历史

Eclipse Collections起源于2004年在高盛公司内部开发的一个名为Caramel的集合框架。 自那以后,这个框架不断发展并在2012年开源成为GitHub中一个叫做 GS Collections的项目。

许多 会议中都有介绍GS Collections。 包括2012的JVM Summit和2014的JavaOne。 此外,在2014年的 QCon New York上还介绍了在Java 8,Scala和GS Collections的平行惰性实现之间的性能比较。 关于GS Collections的文章 (第一部分 / 第二部分) 已经刊登在InfoQ.com, 文中通过实例展示了集合框架的一些功能,并且还向GS Collections的创始人进行了采访。

多年来,大约40多名来自同一家公司的开发人员对集合框架做出了贡献。

为了最大限度地发挥开源项目的最佳特质,GS Collections已经被迁移到Eclipse Foundation,并在2015年被重新命名为Eclipse Collections。 现在这个框架完全向大家开放,接受贡献!

参考指南

GitHub上有Eclipse Collections的详细参考指南: 参考指南.

源代码

在GitHub上可以找到Eclipse Collections的源代码.