Eclipse Collections é a melhor biblioteca de coleções de Java
que traz felicidade ao seu desenvolvimento Java.
Integre-o em sua base de código hoje com suas ferramentas de build favoritas!
<dependency> <groupId>org.eclipse.collections</groupId> <artifactId>eclipse-collections-api</artifactId> <version>11.0.0</version> </dependency> <dependency> <groupId>org.eclipse.collections</groupId> <artifactId>eclipse-collections</artifactId> <version>11.0.0</version> </dependency>
implementation 'org.eclipse.collections:eclipse-collections-api:11.0.0' implementation 'org.eclipse.collections:eclipse-collections:11.0.0'
<dependency org="org.eclipse.collections" name="eclipse-collections-api" rev="11.0.0" /> <dependency org="org.eclipse.collections" name="eclipse-collections" rev="11.0.0" />
Maximize o poder das expressões Lambda e referências de métodos do Java 8 com APIs ricas diretamente disponíveis em suas coleções.
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());
Você pode usar vários tipos de recipientes, aproveitando métodos de fábrica úteis para instanciação.
//Inicializando a lista mutável com método 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");
//Vários tipos de recipientes disponíveis
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");
//Inicializando lista imutável com método 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");
//Vários tipos de recipientes disponíveis
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");
//Listas (List) mutáveis e imutáveis, Conjuntos (Sets), Sacos (Bags), Pilhas (Stacks) e Mapas (Maps) estão disponíveis para todos os 8 tipos primitivos
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);
//Você pode criar uma entrada variada com IntInterval
IntInterval oneTo10 =
IntInterval.fromTo(1, 10); // ints de 1 a 10
// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
IntInterval oneTo10By3 =
IntInterval.fromToBy(1, 10, 3); // ints de 1 a 10 passo por 3
// [1, 4, 7, 10]
IntInterval oddsFrom1To10 =
IntInterval.oddsFromTo(1, 10); // odd ints de 1 a 10
// [1, 3, 5, 7, 9]
IntInterval evensFrom1To10 =
IntInterval.evensFromTo(1, 10); // even ints de i a 10
// [2, 4, 6, 8, 10]
Eclipse Collections fornece implementação eficiente de memória de Conjuntos e Mapas bem como coleções de primitivas.
Confira o Eclipse Collections Kata, uma maneira divertida de ajudá-lo a aprender o uso idiomático da Eclipse Collections. Este kata em particular é configurado como uma série de testes unitários que falham. Sua tarefa é fazê-los passar, usando as coleções da Eclipse Collections.
@Test
public void getFirstNamesOfAllPeople()
{
MutableList<Person> people = this.people;
MutableList<String> firstNames = null; //Substitua null, com um método de transformação no 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);
}
A Eclipse Collections foi iniciada como uma biblioteca de coleções chamada Caramel em Goldman Sachs em 2004.
Desde então, a biblioteca evoluiu e, em 2012, teve seu código fonte aberto no GitHub como um projeto chamado
GS Collections.
GS Collections foi apresentado em inúmeras
conferências
incluindo JVM Summit em 2012 e JavaOne em 2014.
Uma comparação de desempenho entre as implementações preguiçosas paralelas das coleções Java 8, Scala e GS foi apresentada em
QCon New York em 2014.
Além disso artigos sobre GS Collections
(Parte 1 /
Parte 2)
foram publicados em InfoQ.com
mostrando algumas das capacidades da biblioteca de coleções através de exemplos, e também entrevistas com o criador do GS Collections.
Ao longo dos anos, cerca de 40 ou mais desenvolvedores da mesma empresa contribuíram para a biblioteca de coleções.
Para maximizar a melhor natureza do projeto de código aberto, a GS Collection foi migrada para Fundação Eclipse, renomeada como Eclipse Collections em 2015.
Agora, a biblioteca está totalmente aberto à comunidade, aceitando contribuições!
Um guia de referência abrangente da biblioteca está disponível no GitHub: Guia de referência (em Inglês).
O código fonte pode ser encontrado no GitHub: Eclipse Collections.