Hidden Treasures of Eclipse Collections - Part 2
There are a few hidden treasures within Eclipse collections.
Join the DZone community and get the full member experience.Join For Free
Eclipse Collections is an open-source Java Collections framework. In this blog, I am going to demonstrate five lesser-known features of the framework. I published a similar blog in last year's Java Advent Calendar. Please refer to the resources at the end of the blog for more information about the framework.
You may also like: The Best of Java Collections [Tutorials]
countBy(): When you want to find the count of a particular object, you can use the
countBy() API to get a Bag. The purpose of a Bag is to maintain the mapping of an object to the count. A Bag can be used to query the count of an item in
O(1) time. Bag also provides additional helpful API which helps with counting. Learn more about Bag data structure in this blog.
reject(): When you want to choose the elements which do not satisfy a predicate, you can use the
reject() API. This API is provided for enhanced readability and to make it intuitive to developers. You can use
reject() in place of using a
select() and negation of a boolean condition. In essence, all the elements which do not return
true for a boolean condition will be chosen when using
reject(). The output of
reject(BooleanCondition) is the same as what you will get by doing a
makeString(): When you want a configurable string representation of a
RichIterable, you can use
makeString(). If you use
makeString() without a delimiter, then a default delimiter of
"comma space" ( ", " ) is used. If you want a specific delimiter, you can pass it to
makeString() and the output string will have the string representation where every element separated by a delimiter. The delimiter is not used if the size of
Iterable is 1.
zip(): When you want to stitch two
OrderedIterables together, you can use
zip() API operates on two
OrderedIterables and stitches them such that you get an
Pair of elements. In the
Pair, the first of
Pair is the element from first
OrderedIterable and the second of
Pair is the element from second
OrderedIterable. In case the
OrderedIterables are of different sizes, then the excess elements from the longer
OrderedIterable are ignored. The output of
zip() is an
OrderedIterable of the same size as the smaller
corresponds(): When you want to find if all elements of two
OrderedIterables are equal according to a
Predicate, you can use the
corresponds() API. The
corresponds() API operates by first checking if the two
OrderedIterables have the same size, if they have the same size then corresponding elements of both the
OrderedIterables are evaluated using the
Predicate passed to
corresponds(). If the size of
OrderedIterables is equal and the
true for all elements, then
true. If the size of
OrderedIterables is not equal or the
false for any element, then
Eclipse Collections comes with its own implementations of List, Set, and Map. It also has additional data structures like Multimap, Bag, and an entire Primitive Collections hierarchy. Each of our collections has a fluent and rich API for commonly required iteration patterns.
Hope you enjoyed!
Want to write for the Java Advent blog? We are looking for contributors to fill all 24 slots and would love to have your contribution! Contact the Java Advent Admin at email@example.com!
Published at DZone with permission of Nikhil Nanivadekar. See the original article here.
Opinions expressed by DZone contributors are their own.