{{announcement.body}}
{{announcement.title}}

JVM Advent Calendar: Hidden Treasures of Eclipse Collections

DZone 's Guide to

JVM Advent Calendar: Hidden Treasures of Eclipse Collections

There are a few hidden treasures within Eclipse collections.

· Java Zone ·
Free Resource

There are a few hidden treasures within Eclipse collections.

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]


1. 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.

Java




x


1
@Test
2
public void countBy()
3
{
4
    MutableList<String> strings =
5
            Lists.mutable.with("A", "B", "C", "A", "B", "A");
6
    Bag<String> stringToCount = strings.countBy(each -> each);
7
    assertEquals(3, stringToCount.occurrencesOf("A"));
8
    assertEquals(2, stringToCount.occurrencesOf("B"));
9
    assertEquals(1, stringToCount.occurrencesOf("C"));
10
    assertEquals(3, stringToCount.sizeDistinct());
11
    assertEquals(6, stringToCount.size());
12
}


2. 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 select(!someBooleanCondition).

Java




x
14


 
1
@Test
2
public void reject()
3
{
4
    MutableList<Integer> numbers =
5
            Lists.mutable.with(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
6
    MutableList<Integer> odds = numbers.reject(num -> num % 2 == 0);
7
    // reject pattern used to find odd numbers.
8
    // Notice there is no negation in the predicate.
9
    assertEquals(Lists.mutable.with(1, 3, 5, 7, 9), odds);
10
 
           
11
    MutableList<Integer> oddsUsingSelect =
12
            numbers.select(num -> num % 2 != 0);
13
    assertEquals(odds, oddsUsingSelect);
14
}



3. 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.

Java




x
17


1
@Test
2
public void makeString()
3
{
4
    MutableList<Integer> nums = Lists.mutable.with(1, 2, 3);
5
    assertEquals("[1, 2, 3]", nums.toString());
6
    // Notice the difference: toString() vs makeString().
7
    // the ", " delimiter is used by default
8
    assertEquals("1, 2, 3", nums.makeString());
9
 
           
10
    // Delimiter of choice can be passed
11
    assertEquals("1;2;3", nums.makeString(";"));
12
 
           
13
    MutableList<Integer> singleElement = Lists.mutable.with(1);
14
    // Delimiter is not used for size = 1
15
    assertEquals("1", singleElement.makeString());
16
    assertEquals("1", singleElement.makeString(";"));
17
}



4. zip(): When you want to stitch two OrderedIterables together, you can use zip(). The zip() API operates on two OrderedIterables and stitches them such that you get an OrderedIterable of 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 OrderedIterable.

Java




x
37


 
1
@Test
2
public void zip()
3
{
4
    MutableList<Integer> nums = Lists.mutable.with(1, 2, 3);
5
    MutableList<String> strings =
6
            Lists.mutable.with("A", "B", "C");
7
    assertEquals(
8
            Lists.mutable.with(Tuples.pair(1, "A"),
9
                               Tuples.pair(2, "B"),
10
                               Tuples.pair(3, "C")),
11
            nums.zip(strings));
12
    assertEquals(
13
            Lists.mutable.with(Tuples.pair("A", 1),
14
                               Tuples.pair("B", 2),
15
                               Tuples.pair("C", 3)),
16
            strings.zip(nums));
17
 
           
18
    MutableList<Integer> numsSmallerSize =
19
            Lists.mutable.with(1);
20
    assertEquals(
21
            Lists.mutable.with(Tuples.pair(1, "A")),
22
            numsSmallerSize.zip(strings));
23
    assertEquals(
24
            Lists.mutable.with(Tuples.pair("A", 1)),
25
            strings.zip(numsSmallerSize));
26
 
           
27
    MutableList<String> stringsSmallerSize =
28
            Lists.mutable.with("A", "B");
29
    assertEquals(
30
            Lists.mutable.with(Tuples.pair(1, "A"),
31
                               Tuples.pair(2, "B")),
32
            nums.zip(stringsSmallerSize));
33
    assertEquals(
34
            Lists.mutable.with(Tuples.pair("A", 1),
35
                               Tuples.pair("B", 2)),
36
            stringsSmallerSize.zip(nums));
37
}



5. 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 Predicate returns true for all elements, then corresponds() returns true. If the size of OrderedIterables is not equal or the Predicate returns false for any element, then corresponds() returns false.

Java
x
21
 
1
@Test
2
public void corresponds()
3
{
4
    MutableList<Integer> lhs1 = Lists.mutable.with(1, 2, 3);
5
    MutableList<Integer> rhs1 = Lists.mutable.with(1, 2, 3);
6
    assertTrue(lhs1.corresponds(rhs1, Integer::equals));
7
8
    MutableList<Integer> lhs2 = Lists.mutable.with(1, 2, 3);
9
    MutableList<Integer> rhs2 = Lists.mutable.with(2, 4, 6);
10
    assertTrue(
11
            lhs2.corresponds(rhs2,
12
                             (lhs, rhs) -> rhs == 2 * lhs));
13
    assertFalse(
14
            lhs2.corresponds(rhs2,
15
                             (lhs, rhs) -> rhs == lhs * lhs));
16
    assertFalse(lhs2.corresponds(rhs2, Integer::equals));
17
18
    MutableList<Integer> lhs3 = Lists.mutable.with(1, 2);
19
    MutableList<Integer> rhs3 = Lists.mutable.with(1, 2, 3);
20
    assertFalse(lhs3.corresponds(rhs3, Integer::equals));
21
}

Eclipse Collections Resources:

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!

This post was originally published as part of the Java Advent series. If you like it, please spread the word by sharing it on Twitter, Facebook, etc.!

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 contribute@javaadvent.com!

Further Reading

The Best of Java Collections [Tutorials]

Eclipse Collections by Example: Filtering

JVM Advent Calendar 2018: API Design of Eclipse Collections

Topics:
java ,eclipse ,collections

Published at DZone with permission of Nikhil Nanivadekar . See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}