JVM Advent

The JVM Programming Advent Calendar

HIDDEN TREASURES OF ECLIPSE COLLECTIONS 2022 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, 2019, 2020, and 2021. Please refer to the resources at the end of the blog for more information about the framework.

  1. toSortedList(Comparator): If you need to sort a RichIterable and convert it to a sorted List, you can use the toSortedList(Comparator) API on RichIterable. 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 a to API, it means a new List 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");
    }
  2. 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 cases  toSortedListBy(Function) can be used.
    Note: Since this is a to API, it means a new List 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");
    }
  3. Fused API for collect + makeString(): Eclipse Collections provides the makeString() API to create a String representation of a collection. However, there are times when each element needs to be transformed before creating a String representation. Eclipse Collections provides an overloaded makeString() API which takes a Function, 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");
    }
  4. containsBy(): The regular contains() 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 a Function to the elements. You can use containsBy() for such a use case. Advantage of the containsBy() operation is that you will not have to first transform all the elements of the RichIterable and then perform contains()  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));
    }
  5. swap(): If you need to swap the indexes of two elements in a MutablePrimitiveList then you can use the swap() API. This API is currently only available on the MutablePrimitiveList 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 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.

© 2024 JVM Advent | Powered by steinhauer.software Logosteinhauer.software

Theme by Anders Norén