Eclipse Collections is the best Java collections framework ever
that brings happiness to your Java development.
Integrate it to your codebase today with your favorite build tools!
<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" />
Maximize the power of Java 8 Lambda expressions and method references with rich APIs directly available on your collections.
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());
You can make use of various container types, leveraging handy factory methods for instantiation.
//Initializing mutable list with empty(), of(), with() method 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"); //Various container types available 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");
//Initializing immutable list with empty(), of(), with() method 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"); //Various container types available 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");
//Mutable and immutable Lists, Sets, Bags, Stacks and Maps are available for all 8 primitive types 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); //You can create a ranged ints with IntInterval IntInterval oneTo10 = IntInterval.fromTo(1, 10); // ints from 1 to 10 // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] IntInterval oneTo10By3 = IntInterval.fromToBy(1, 10, 3); // ints from 1 to 10 step by 3 // [1, 4, 7, 10] IntInterval oddsFrom1To10 = IntInterval.oddsFromTo(1, 10); // odd ints from 1 to 10 // [1, 3, 5, 7, 9] IntInterval evensFrom1To10 = IntInterval.evensFromTo(1, 10); // even ints from 1 to 10 // [2, 4, 6, 8, 10]
Eclipse Collections provides memory efficient implementation of Sets and Maps as well as primitive collections.
Check out the Eclipse Collections Kata, a fun way to help you learn idiomatic Eclipse Collections usage. This particular kata is set up as a series of unit tests which fail. Your task is to make them pass, using 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); }
The origin of Eclipse Collections was started off as a collections framework named Caramel at Goldman Sachs in 2004.
Since then the framework has evolved, and in 2012, it was open sourced to GitHub as a project called
GS Collections.
GS Collections has been presented at number of
conferences
including JVM Summit in 2012 and JavaOne in 2014.
A performance comparison between the parallel lazy implementations of Java 8, Scala and GS Collections was presented at
QCon New York in 2014.
Also articles about GS Collections
(Part1 /
Part2)
have published on InfoQ.com
showing some of the capabilities of the collections framework through examples, and also interviews to the creator of GS Collections.
Over the years, around 40 or so developers from the same company have contributed to the collections framework.
To maximize the best nature of open source project, GS Collections has been migrated to the Eclipse Foundation, re-branded as Eclipse Collections in 2015.
Now the framework is fully open to the community, accepting contributions!
A comprehensive reference guide of the library is available on GitHub: Reference Guide.
Source code can be found on GitHub: Eclipse Collections.