Eclipse Collections cheat sheet

Overview

As for the explanation of Eclipse Collections itself, I think there are many other good articles, so here I will upload it as a reference that you can refer to immediately when you think "I want to do XX". For the time being, I've listed the ones I use most often, but I may add them if they come out.

environment

Ver.8.0.0

pom.xml


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

Initialization

Empty list

empty


Lists.mutable.empty();

From this point onward, immutable (ImmutableList) can be used in addition to mutable (MutableList), unless otherwise specified. ImmutableList does not have methods such as add and cannot be changed.

empty


Lists.immutable.empty();

Initialize all together

of


Lists.mutable.of(first, second, third);

Initialize based on Iterable

You can convert java.util.List etc. to ImmutableList etc.

ofall


Lists.immutable.ofAll(hogeList);

Frequent pattern collection

Extract only elements that satisfy the conditions

select


		ListIterable<String> hogeList = Lists.mutable.of("hoge", "fuga", "piyo");
		assertThat(
				hogeList
					.select(s -> s.contains("g"))
					.makeString()
				, is("hoge, fuga")
			);

I want to check if there is an element that meets the conditions

ʻAnySatisfy` returns true if there is at least one element that meets the condition.

anySatisfy


		ListIterable<String> hogeList = Lists.mutable.of("hoge", null, "fuga");
		assertThat(
				hogeList.anySatisfy(s -> s == null)
				, is(true)
			);

I want to make sure that all the elements do not meet the conditions

noneSatisfy returns true if there are no elements that meet the condition.

noneSatisfy


		ListIterable<String> hogeList = Lists.mutable.of("hoge", "fuga", "piyo");
		assertThat(
				hogeList.noneSatisfy(s -> s.equals("hogera"))
				, is(true)
			);

I want to get if there is even one element that meets the conditions

detect retrieves only the first one of the elements that meet the condition.

detect


		ListIterable<String> hogeList = Lists.mutable.of(null, "hoge", "fuga");
		assertThat(
				hogeList.detect(s -> s != null).toString()
				, is("hoge")
			);

Convert the element to a new List

collect


		ListIterable<String> hogeList = Lists.mutable.of("hogehoge", "fuga", "piyopi");
		assertThat(
				hogeList
					.collect(s -> s.length())
					.makeString()
				, is("8, 4, 6")
			);

Deduplication

Eliminate duplication and leave only unique elements.

distinct


		ListIterable<String> hogeList = Lists.mutable.of("hoge", "fuga", "piyo", "fuga", "piyo");
		assertThat(
				hogeList
					.distinct()
					.makeString()
				, is("hoge, fuga, piyo")
			);

It is also possible to specify the criteria to judge that it is unique. At this time, only the first one of the duplicate elements remains in the list of results.

distinct


		ListIterable<String> hogeList = Lists.mutable.of("hoge", "fuga", "piyo", "fugafuga", "piyopiyo");
		assertThat(
				hogeList
					.distinct(HashingStrategies.fromFunction(s -> s.length()))
					.makeString()
				, is("hoge, fugafuga")
			);

Sorting

sortThis


		MutableList<String> hogeList = Lists.mutable.of("hoge", "fuga", "piyo");
		assertThat(
				hogeList
					.sortThis()
					.makeString()
				, is("fuga, hoge, piyo")
			);

It is also possible to specify the sorting criteria and to sort in descending order.

sortThisBy


		MutableList<String> hogeList = Lists.mutable.of("hogehoge", "fuga", "piyopi");
		assertThat(
				hogeList
					.sortThisBy(s -> s.length())
					.reverseThis()
					.makeString()
				, is("hogehoge, piyopi, fuga")
			);

repetition

If you want to repeat the process and then continue writing, there is also tap.

each


		ListIterable<String> hogeList = Lists.mutable.of("hoge", "fuga", "piyo");
		hogeList
			.each(s ->
				System.out.println(s)
			);
		//Execution result
		//hoge
		//fuga
		//piyo

With forEachWithIndex, you can loop with index.

forEachWithIndex


		ListIterable<String> hogeList = Lists.mutable.of("hoge", "fuga", "piyo");
		hogeList
			.forEachWithIndex((string, index) ->
				System.out.println(String.valueOf(index).concat(string))
			);
		//Execution result
		//0hoge
		//1fuga
		//2piyo

Aggregate

With groupBy, you can group by the specified value. It comes back in the type ListMultimap and can be retrieved in various ways, but I like to use it as Pair (discussed below) with the keyMultiValuePairsView () method.

groupBy


		ListIterable<String> hogeList = Lists.mutable.of("hoge", "hogera", "fuga", "fugafuga", "piyo", "piyopiyo");
		hogeList
			.groupBy(s -> s.length())
			.keyMultiValuePairsView()
			.each(p ->{
				System.out.println(p.getOne() + " -> " + p.getTwo());
			});
		//Execution result
		//4 -> [hoge, fuga, piyo]
		//6 -> [hogera]
		//8 -> [fugafuga, piyopiyo]

total

sumOfInt


		ListIterable<String> hogeList = Lists.mutable.of("hoge", "fuga", "piyo");
		assertThat(
				hogeList
					.sumOfInt(s -> s.length())
				, is(12L)
			);

I want to get only one element

First one

If there are no elements, null is returned.

getFirst


		ListIterable<String> hogeList = Lists.mutable.of("hoge", "fuga", "piyo");
		assertThat(
				hogeList
					.select(s -> s.contains("g"))
					.getFirst()
				, is("hoge")
			);

Last one

Again, null is returned if there are no elements.

getLast


		ListIterable<String> hogeList = Lists.mutable.of("hoge", "fuga", "piyo");
		assertThat(
				hogeList
					.select(s -> s.contains("g"))
					.getLast()
				, is("fuga")
			);

Element that should be only one

If you should be able to narrow down the elements to only one with select etc., use getOnly. IllegalStateException is thrown if there is more than one element.

getOnly


		ListIterable<String> hogeList = Lists.mutable.of("hoge", "fuga", "piyo");
		assertThat(
				hogeList
					.select(s -> s.contains("i"))
					.getOnly()
				, is("piyo")
			);

Conversion from standard collection

The frameworks used in the project often use standard collections such as java.util.List. In such a case, if you want to use Eclipse Collections, you will have to convert it to the Eclipse Collections type once with ʻofAll` etc. as shown below, but it will be a little disturbing if the number increases.

Tend to be like this


List<String> hogeList = Arrays.asList("konna", "fuuni", "shiteita");
ImmutableList<String> fugaList = Lists.immutable.ofAll(hogeList);
fugaList.select(s -> s.length > 4);

In such a case, you can use the method of eclipse collestions in one shot by using ListAdapter. If it's ʻArray instead of List, there's also ʻArrayAdapter.

This was good


List<String> hogeList = Arrays.asList("kore", "de", "yokatta");
ListAdapter.adapt(hogeList)
           .select(s -> s.length > 4);

There are also ListIterate and ʻArrayIterate` that don't even require adapt.

ListIterate


List<String> hogeList = Arrays.asList("adapt", "mo", "shinai");
ListIterate.select(hogeList, s -> s.length > 4);

Other datasets

I want to keep two values as a set

It is used when you want to have data as a pair of key and value. Two types of instances can be created from Tuples. Pair<T1, T2> Click here for different types.

Pair


Pair<String, Object> keyValuePair = Tuples.pair("key", value);
String key = keyValuePair.getOne();
Object value = keyValuePair.getTwo();

Twin If you want to work with values of the same type, you can use this.

Twin


Twin<String> keyValueTwin = Tuples.twin("key", "valiue");
String key = keyValueTwin.getOne();
String value = keyValueTwin.getTwo();

Exception handling

If a check exception occurs in lambda, the code that was written concisely will be ruined. In such a case, statically import the following and use the throwing method.

import static org.eclipse.collections.impl.block.factory.Procedures.*;
import static org.eclipse.collections.impl.block.factory.Functions.*;
import static org.eclipse.collections.impl.block.factory.Predicates.*;

If an exception occurs during throwing, it will wrap it in a RuntimeException and throw it again. Now I can write it concisely.

throwing


		hogeList
			.each(throwing(Fuga::piyo)
			;

Recommended Posts

Eclipse Collections cheat sheet
[Eclipse] Shortcut key cheat sheet
Java cheat sheet
JMeter cheat sheet
Kotlin cheat sheet
[Docker cheat sheet]
Mockito + PowerMock cheat sheet
Rails Tutorial cheat sheet
Spring Boot2 cheat sheet
SCSS notation cheat sheet
Oreshiki docker-compose cheat sheet
Docker command cheat sheet
Java Stream API cheat sheet
What's cool about Eclipse Collections What's not cool
C # cheat sheet for Java engineers
Competitive programming private cheat sheet (Java)
javac, jar, java command cheat sheet
Getting Started with Doma-Criteria API Cheat Sheet
[Java] Data type / string class cheat sheet
Basic cheat sheet by language (Ruby, PHP)