Java Advent Calendar 2017 Day 11 article.
It's called Nogamicho. I live between Java and AWS.
Do you know Eclipse Collections? Developed by Goldman Sachs in 2004, it became OSS and was transferred to the Eclipse Foundation in 2015.
What is Eclipse Collections? Because some of the contents of are organized in other materials I will focus on what is cool and what is not.
As mentioned above, the general content will be reprinted, so I will omit it. Please refer to the reference materials listed below.
Java 9 also released a factory method for collections, which is immutable.
Eclipse Collections allows you to initialize collections such as List, Set, Map with a unified API.
MutableList<String> list = Lists.mutable.of();
MutableSet<String> set = Sets.mutable.of();
MutableMap<String, String> map = Maps.mutable.of();
//with can be used instead of of
MutableList<String> list = Lists.mutable.with();
//The calling method is almost the same for immutable
ImmutableList<String> list = Lists.immutable.of();
Eclipse Collenctions supports the following data structures:
data structure | Corresponding primitive |
---|---|
List | |
Set | |
SortedSet | Ordered Set |
Bag | Duplicate Set |
SortedBag | Ordered Bag |
Map | |
BiMap | Bidirectional map |
SortedMap | Ordered Map |
Multimap | The value corresponding to the key is a collection. List, Set,Selectable from Bag |
Stack | |
Pair | Tuple format data |
PrimitiveCollection StreamAPI also has Primitive Streams such as IntStream and LongStream, but it can only handle List-like as a data structure. There are Primitive collections for all the collections in the previous section.
It makes a noticeable difference when processing large amounts of data with more memory efficiency than using wrapper classes.
The problem with Java8 was that method references weren't available for methods that take Predicate as an argument. In Eclipse Collections, some methods have a with suffix, which makes good use of method references.
//Writing in Java 8
boolean result = this.people.stream().anyMatch(person -> person.hasPet(PetType.CAT));
//Rewritten to Eclipse Collections
boolean result = this.people.anySatisfy(person -> person.hasPet(PetType.CAT));
//When using with
boolean result = this.people.anySatisfyWith(Person::hasPet, PetType.CAT);
(Quoted from Reference Material 2)
The problem with the Stream API was that I couldn't write the process of "converting all the values in the Map" in a concise manner. MutableMap of Eclipse Collections has a method called keyValuesView, which makes it easy to process all at once.
Maps.mutable.of("Key1", 1, "Key2", 2, "Key3", 3, "Key4", 4)
.keyValuesView() // RichIterable<Pair<String, Integer>>
.collect((k, v) -> Tuples.pair(k, v * 2))// RichIterable<Pair<String, Integer>>
.toMap();
These are the methods that existed before Optional was developed. Calling these methods on an empty collection throws a NoSuchElementException. An empty check is mandatory when using.
I wrote that, but in Eclipse Collections 8.2, RichIterable # minOptional and RichIterable # maxOptional were added. No weaknesses ...?
I didn't write what I thought was good subjectively. There are still few articles on Eclipse Collections on Qiita, so if you are using it, let's do missionary work together!