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 have published similar blogs in Java Advent Calendars of 2018, 2019, 2020, and 2021. Please refer to the resources at the end of the blog for more information about the framework.
toSortedList(Comparator)
: If you need to sort aRichIterable
and convert it to a sortedList
, you can use thetoSortedList(Comparator)
API onRichIterable
. This API creates a sorted list where the elements are sorted using the comparator.
It is important to remember the difference between sorted and ordered. Lists are by default ordered.
Note: Since this is ato
API, it means a newList
will be created.@Test public void toSortedListComparator() { var list = Lists.mutable.with(3, 1, 2); var sortedList = list.toSortedList(Comparator.naturalOrder()); Assertions.assertEquals( Lists.mutable.with(1, 2, 3), sortedList, "Sorted List by Natural Order"); }
toSortedListBy(Function)
: In addition to the API provided above, sometimes, you need to sort the list as a result of a transformation or a mapping function. In these casestoSortedListBy(Function)
can be used.
Note: Since this is ato
API, it means a newList
will be created.@Test public void toSortedListBy() { var list = Lists.mutable.with(-3, 2, -1); var sortedList = list.toSortedListBy(Math::abs); Assertions.assertEquals( Lists.mutable.with(-1, 2, -3), sortedList, "Sorted List by absolute value"); }
- Fused API for
collect
+makeString()
: Eclipse Collections provides themakeString()
API to create aString
representation of a collection. However, there are times when each element needs to be transformed before creating aString
representation. Eclipse Collections provides an overloadedmakeString()
API which takes aFunction
, start , separator, and end.@Test public void collectMakeString() { var list = Lists.mutable.with(-3, 2, -1); var stringRep = list.makeString(Math::abs, "[", ":", "]"); Assertions.assertEquals( "[3:2:1]", stringRep, "String representation of absolute values"); }
containsBy()
: The regularcontains()
API performs an.equals()
check between a value and elements of a collection. However, there are times when you need to evaluate the presence of a value after applying aFunction
to the elements. You can usecontainsBy()
for such a use case. Advantage of thecontainsBy()
operation is that you will not have to first transform all the elements of theRichIterable
and then performcontains()
for a value. However, the disadvantage is that it is a O(n) operation. Please weigh the pros and cons w.r.t. your use case before using this API.
Note: Since the function needs to be applied to each element, this is an O(n) operation.@Test public void containsBy() { var set = Sets.mutable.with(-1, -2, -3); // containsBy is a O(n) operation even for a Set. var containsBy = set.containsBy(Math::abs, 1); Assertions.assertTrue(containsBy); var list = Lists.mutable.with(-1, -2, -3); Assertions.assertFalse(list.containsBy(Math::abs, -1)); }
swap()
: If you need to swap the indexes of two elements in aMutablePrimitiveList
then you can use theswap()
API. This API is currently only available on theMutablePrimitiveList
hierarchy.
Note: This is an in-place operation.@Test public void swap() { var intList = IntLists.mutable.with(1, 2, 3); // Swap the elements at index 0 and index 2 intList.swap(0, 2); var expected = IntLists.mutable.with(3, 2, 1); Assertions.assertEquals(expected, intList); }
Eclipse Collections Resources:
Eclipse Collections comes with it’s 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 have a fluent and rich API for commonly required iteration patterns.
- Website
- Source code on GitHub (Make sure to star the Repository)
- Contribution Guide
- Reference Guide
- Hidden Treasures of Eclipse Collections 2020 Edition
- Hidden Treasures of Eclipse Collections 2019 Edition
- Hidden Treasures of Eclipse Collections 2018 Edition
Author: Nikhil Nanivadekar
Lead Eclipse Collections: eclipse.org/collections, Java Champion. I enjoy hiking, skiing, reading. All opinions stated by me are my own.