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 and 2019. Please refer to the resources at the end of the blog for more information about the framework.
notEmpty(): When you want to check if aRichIterableis not empty you can usenotEmpty(). This is a convenience method so that you do not have to use the negation ofisEmpty(). It helps keep the code fluent and intuitive and avoids code readability issues which can be caused due to negations. In case you do not have aRichIterableyou can use thenotEmpty()method from the utility classIterate.@Test public void notEmpty() { RichIterable<String> richIterable = Lists.mutable.with("A"); Assert.assertTrue(richIterable.notEmpty()); List<String> list = Arrays.asList("A"); Assert.assertTrue(Iterate.notEmpty(list)); }-
minOptional(): When you want to find the min value in aRichIterableyou can use themin()API. However,min()will throw an exception if theRichIterableis empty. You can useminOptional()to protect against these exceptions. By defaultminOptional()will use natural order comparator, if you want a different comparator you can use the overloadedminOptional()API by simply passing in the requiredComparator.@Test public void minOptional() { RichIterable<Integer> nums = Lists.mutable.with(1, 2, 3); Assert.assertEquals( Integer.valueOf(1), nums.minOptional() .orElse(0)); Assert.assertEquals( Integer.valueOf(3), nums.minOptional(Comparator.reverseOrder()) .orElse(0)); RichIterable<Integer> empty = Lists.mutable.empty(); Assert.assertEquals( Integer.valueOf(0), empty.minOptional() .orElse(0)); Assert.assertEquals( Integer.valueOf(0), empty.minOptional(Comparator.reverseOrder()) .orElse(0)); } maxOptional(): When you want to find the max value in aRichIterableyou can use themax()API. However,max()will throw an exception if theRichIterableis empty. You can usemaxOptional()to protect against these exceptions. By defaultmaxOptional()will use natural order comparator, if you want a different comparator you can use the overloadedmaxOptional()API by simply passing in the requiredComparator.@Test public void maxOptional() { RichIterable<Integer> nums = Lists.mutable.with(1, 2, 3); Assert.assertEquals( Integer.valueOf(3), nums.maxOptional() .orElse(0)); Assert.assertEquals( Integer.valueOf(1), nums.maxOptional(Comparator.reverseOrder()) .orElse(0)); RichIterable<Integer> empty = Lists.mutable.empty(); Assert.assertEquals( Integer.valueOf(0), empty.maxOptional() .orElse(0)); Assert.assertEquals( Integer.valueOf(0), empty.maxOptional(Comparator.reverseOrder()) .orElse(0)); }There are many other ways to find min and max using Eclipse Collections that are covered in detail in this blog.
unionAll(): When you want to do a union of a number of sets you can useunionAll(). By defaultunionAll()API will return a newSetwhich contains all the elements as a result of the union operation. In case you need to gather the result of theunionAll()in a predefinedSetyou can useunionAllInto().@Test public void unionAll() { Set<Integer> set1 = Sets.mutable.with(1); Set<Integer> set2 = Sets.mutable.with(2); Set<Integer> set3 = Sets.mutable.with(3); MutableSet<Integer> union = Sets.unionAll(set1, set2, set3); Assert.assertEquals( Sets.mutable.with(1, 2, 3), union); }@Test public void unionAllInto() { Set<Integer> set1 = Sets.mutable.with(1); Set<Integer> set2 = Sets.mutable.with(2); Set<Integer> set3 = Sets.mutable.with(3); Set<Integer> target = Sets.mutable.with(0); Sets.unionAllInto(target, set1, set2, set3); Assert.assertEquals( Sets.mutable.with(0, 1, 2, 3), target); }There are many other
Setspecific operations available which are covered in detail in this blog.flip(): When you want to flip the keys and values of aMapIterableyou can useflip(). Theflip()API returns aMultimapas a result of flipping the keys to values and values to keys. Since in the originalMapIterablethe keys are unique, the output offlip()is aSetMultimapor aSortedSetMultimapdepending on the originalMapIterable.
The name flip was chosen instead of invert or transpose because, you cannot get the originalMapIterableif you calledflip()twice.@Test public void flip() { MapIterable<String, Integer> map = Maps.mutable.<String, Integer>empty() .withKeyValue("A", 1) .withKeyValue("B", 1) .withKeyValue("AA", 2) .withKeyValue("BB", 2); MutableSetMultimap<Integer, String> expected = Multimaps.mutable.set.empty(); expected.putAll(1, Lists.mutable.with("A", "B")); expected.putAll(2, Lists.mutable.with("AA", "BB")); Assert.assertEquals( expected, map.flip()); }
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 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.
steinhauer.software