JVM Advent

The JVM Programming Advent Calendar

HIDDEN TREASURES OF ECLIPSE COLLECTIONS – 2020 EDITION

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.

  1. notEmpty(): When you want to check if a RichIterable is not empty you can use notEmpty(). This is a convenience method so that you do not have to use the negation of isEmpty(). 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 a RichIterable you can use the notEmpty() method from the utility class Iterate.
    @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));
    }
  2.  minOptional(): When you want to find the min value in a RichIterable you can use the min() API. However, min() will throw an exception if the RichIterable is empty. You can use minOptional() to protect against these exceptions. By default minOptional() will use natural order comparator, if you want a different comparator you can use the overloaded minOptional() API by simply passing in the required Comparator.
    @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));
    }
  3. maxOptional(): When you want to find the max value in a RichIterable you can use the max() API. However, max() will throw an exception if the RichIterable is empty. You can use maxOptional() to protect against these exceptions. By default maxOptional() will use natural order comparator, if you want a different comparator you can use the overloaded maxOptional() API by simply passing in the required Comparator.
    @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.

  4. unionAll(): When you want to do a union of a number of sets you can use unionAll(). By default unionAll() API will return a new Set which contains all the elements as a result of the union operation. In case you need to gather the result of the unionAll() in a predefined Set you can use unionAllInto().
    @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 Set specific operations available which are covered in detail in this blog.

  5. flip(): When you want to flip the keys and values of a MapIterable you can use flip(). The flip() API returns a Multimap as a result of flipping the keys to values and values to keys. Since in the original MapIterable the keys are unique, the output of flip() is a SetMultimap or a SortedSetMultimap depending on the original MapIterable.
    The name flip was chosen instead of invert or transpose because, you cannot get the original MapIterable if you called flip() 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 ListSet and Map. It also has additional data structures like MultimapBag and an entire Primitive Collections hierarchy. Each of our collections have a fluent and rich API for commonly required iteration patterns.

Author: Nikhil Nanivadekar

Lead Eclipse Collections: eclipse.org/collections, Java Champion. I enjoy hiking, skiing, reading. All opinions stated by me are my own.

Next Post

Previous Post

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

© 2021 JVM Advent | Powered by Jetbrains LogoJetBrains & steinhauer.software Logosteinhauer.software

Theme by Anders Norén

%d bloggers like this: