之前分享了vavr,今天在分享一个同类框架eclipse-collections
官方文档:http://www.eclipse.org/collections/
代码语言:javascript复制<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>
体验下,这是java8 Stream
的:
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
:
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));
简短了原本的代码