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.
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>
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();
of
Lists.mutable.of(first, second, third);
You can convert java.util.List etc. to ImmutableList etc.
ofall
Lists.immutable.ofAll(hogeList);
select
ListIterable<String> hogeList = Lists.mutable.of("hoge", "fuga", "piyo");
assertThat(
hogeList
.select(s -> s.contains("g"))
.makeString()
, is("hoge, fuga")
);
ʻ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)
);
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)
);
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")
);
collect
ListIterable<String> hogeList = Lists.mutable.of("hogehoge", "fuga", "piyopi");
assertThat(
hogeList
.collect(s -> s.length())
.makeString()
, is("8, 4, 6")
);
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")
);
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")
);
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
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]
sumOfInt
ListIterable<String> hogeList = Lists.mutable.of("hoge", "fuga", "piyo");
assertThat(
hogeList
.sumOfInt(s -> s.length())
, is(12L)
);
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")
);
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")
);
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")
);
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);
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
Twin
Twin<String> keyValueTwin = Tuples.twin("key", "valiue");
String key = keyValueTwin.getOne();
String value = keyValueTwin.getTwo();
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