Skip to content

mohrezaei/eclipse-collections

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Eclipse Collections is a comprehensive collections library for Java. The library enables productivity and performance by delivering an eloquent, elegant and efficient set of APIs and types. The iteration protocol was inspired by the Smalltalk collection framework, and the collections are compatible with the Java Collection Framework types.

Why Eclipse Collections?

  • Productivity
    • Rich, functional, and fluent APIs with great symmetry
    • List, Set, Bag, Stack, Map, Multimap, BiMap, Interval Types
    • Readable, Mutable, and Immutable Types
    • Mutable and Immutable Collection Factories
    • Adapters and Utility classes for JCF Types
  • Performance
    • Memory Efficient Containers
    • Optimized Eager, Lazy and Parallel APIs
    • Primitive Collections for all primitive types
  • Actively developed since 2005

Learn Eclipse Collections

Acquiring Eclipse Collections

Maven

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

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

Gradle

compile 'org.eclipse.collections:eclipse-collections-api:9.2.0'
compile 'org.eclipse.collections:eclipse-collections:9.2.0'

Some Quick Examples

Eclipse Collections puts iteration methods directly on the container types. Here's several code examples that demonstrate the simple and flexible style of programming with Eclipse Collections.

First, we will define a simple class named Person to hold the first and last names of three people.

public class Person
{
    private final String firstName;
    private final String lastName;

    public Person(String firstName, String lastName)
    {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public String getFirstName()
    {
        return this.firstName;
    }

    public String getLastName()
    {
        return this.lastName;
    }
    
    public boolean lastNameEquals(String name)
    {
        return name.equals(this.lastName);
    }
}

Now we will create three instances of the Person class..

Person person1 = new Person("Sally", "Smith");
Person person2 = new Person("Ted", "Watson");
Person person3 = new Person("Mary", "Williams");

Collect (aka map, transform)

Now we will create a MutableList with the three people, collect their names, and output them to a comma delimited String.

MutableList<Person> people = Lists.mutable.with(person1, person2, person3);
MutableList<String> lastNames = people.collect(person -> person.getLastName());
Assert.assertEquals("Smith, Watson, Williams", lastNames.makeString());

The lambda in the example above can also be replaced with a method reference.

MutableList<String> lastNames = people.collect(Person::getLastName);

Eclipse Collections has support for both Mutable and Immutable collections, and the return types of methods are covariant. While the collect method on a MutableList returned a MutableList, the collect method on an ImmutableList will return an ImmutableList. Here we use the same Lists factory to create an ImmutableList.

ImmutableList<Person> people = Lists.immutable.with(person1, person2, person3);
ImmutableList<String> lastNames = people.collect(Person::getLastName);
Assert.assertEquals("Smith, Watson, Williams", lastNames.makeString());

Eclipse Collections has a lazy API as well, which is available by calling the method asLazy. The method collect will now return a LazyIterable. The LazyIterable that is returned does not evaluate anything until the call to a terminal method is made. In this case, the call to makeString will force the LazyIterable to collect the last names.

LazyIterable<String> lastNames = people.asLazy().collect(Person::getLastName);
Assert.assertEquals("Smith, Watson, Williams", lastNames.makeString());

Select / Reject (aka filter / filter not)

We can find all of the people with the last name "Smith" using the method named select.

MutableList<Person> people = Lists.mutable.with(person1, person2, person3);
MutableList<Person> smiths = people.select(person -> person.lastNameEquals("Smith"));
Assert.assertEquals("Smith", smiths.collect(Person::getLastName).makeString());

If we want to use a method reference, we can use the method selectWith.

MutableList<Person> smiths = people.selectWith(Person::lastNameEquals, "Smith");
Assert.assertEquals("Smith", smiths.collect(Person::getLastName).makeString());

We can find all the people who do not have a last name of "Smith" using the method named reject.

MutableList<Person> notSmiths = people.reject(person -> person.lastNameEquals("Smith"));
Assert.assertEquals("Watson, Williams", notSmiths.collect(Person::getLastName).makeString());

If we want to use a method reference, we can use the method rejectWith.

MutableList<Person> notSmiths = people.rejectWith(Person::lastNameEquals, "Smith");
Assert.assertEquals("Watson, Williams", notSmiths.collect(Person::getLastName).makeString());

How to Contribute

We welcome contributions! We accept contributions via pull requests here in GitHub. Please see How To Contribute to get started.

Additional information

About

The Eclipse Collections Project

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 97.1%
  • HTML 1.8%
  • Scala 1.1%