Eclipse Collections 9.2 Released

Donald Raab
May 21, 2018 · 4 min read

New features, more symmetric sympathy and community contributions.

Good symmetry at St. Paul’s

Eclipse Collections is a collections framework for Java. It has optimized List, Set and Map implementations with a rich and fluent API. The library provides additional data structures not found in the JDK like Bags, Multimaps and BiMaps. The framework also provides primitive versions of Lists, Sets, Bags, Stacks and Maps with a rich and fluent API. There is support for both Mutable and Immutable versions of all containers in the library.

There were nine developers who contributed to the Eclipse Collections 9.2.0 release. I want to thank everyone who made a contribution. If this was your first contribution to an open source project and/or Eclipse Collections, congratulations and welcome!

The release notes for Eclipse Collections 9.2.0 are here:

https://github.com/eclipse/eclipse-collections/releases/tag/9.2.0

Features, Features, Features!

Welcome: flatCollectWith

@Test
public void flatCollectWith()
{
MutableList<Integer> list =
Lists.mutable.with(5, 4, 3, 2, 1);
MutableList<Integer> result =
list.flatCollectWith(Interval::fromTo, 1);

MutableList<Integer> expected = mList(
5, 4, 3, 2, 1,
4, 3, 2, 1,
3, 2, 1,
2, 1,
1);
Assert.assertEquals(expected, result);
}

Welcome: toSortedMapBy

@Test
public void toSortedMapBy()
{
MutableList<Integer> list =
Lists.mutable.with(4, 3, 2, 1);
MutableSortedMap<Integer, Integer> map =
list.toSortedMapBy(i -> i % 2, k -> k, v -> v);
Assert.assertEquals(
SortedMaps.mutable.with(
Comparators.byFunction(i -> i % 2),
4, 2, 3, 1),
map);
}

Welcome: Bag.selectDuplicates

@Test
public void selectDuplicates()
{
MutableBag<Integer> bag =
Bags.mutable.with(1, 2, 2, 3, 3, 3);
MutableBag<Integer> duplicates =
bag.selectDuplicates();

Assert.assertEquals(
Bags.mutable.with(2, 2, 3, 3, 3),
duplicates);
}

Welcome: Bag.selectUnique

@Test
public void selectUnique()
{
MutableBag<Integer> bag =
Bags.mutable.with(1, 2, 2, 3, 3, 3);
MutableSet<Integer> unique =
bag.selectUnique();

Assert.assertEquals(
Sets.mutable.with(1),
unique);
}

Welcome: chunk (for primitive collections)

@Test
public void chunk()
{
IntList list = IntInterval.oneTo(10);
RichIterable<IntIterable> chunks = list.chunk(2);

Verify.assertSize(5, chunks);
String string = chunks.makeString("/");
Assert.assertEquals(
"[1, 2]/[3, 4]/[5, 6]/[7, 8]/[9, 10]",
string);
}

Welcome: newEmpty (for primitive collections)

@Test
public void newEmpty()
{
MutableIntList empty1 = IntLists.mutable.empty();
MutableIntList empty2 = empty1.newEmpty();

Assert.assertEquals(empty1, empty2);
Assert.assertNotSame(empty1, empty2);
}

Welcome: OrderedMapAdapter

@Test
public void orderedMapAdapter()
{

MutableList<Integer> keys = Lists.mutable.with(3, 2, 1);
MutableOrderedMap<Object, Object> orderedMap =
OrderedMaps.adapt(
keys.injectInto(
new LinkedHashMap<>(),
(map, each) -> {
map.put(each, each);
return map;
}));

LinkedHashMap<Object, Object> expected = new LinkedHashMap<>();
expected.put(3, 3);
expected.put(2, 2);
expected.put(1, 1);

Assert.assertEquals(expected, orderedMap);
Assert.assertEquals(keys, orderedMap.keysView().toList());
Assert.assertEquals(Interval.fromTo(3, 1), orderedMap.toList());
}

Update: Here’s an example using forEachWithIndex to populate the orderedMap.

@Test
public void orderedMapAdapter()
{
MutableOrderedMap<Integer, Integer> orderedMap =
OrderedMaps.adapt(new LinkedHashMap<>());

MutableList<Integer> keys = Lists.mutable.with(3, 2, 1);
keys.forEachWithIndex(orderedMap::put);

Map<Object, Object> expected = new LinkedHashMap<>();
expected.put(3, 0);
expected.put(2, 1);
expected.put(1, 2);

Assert.assertEquals(expected, orderedMap);
Assert.assertEquals(keys, orderedMap.keysView().toList());
Assert.assertEquals(Interval.zeroTo(2), orderedMap.toList());
}

Welcome: toStringOfItemToCount (for primitive bags)

@Test
public void toStringOfItemToCount()
{
MutableIntBag bag = IntBags.mutable.with(1, 2, 2, 3, 3, 3);
String string = bag.toStringOfItemToCount();

Assert.assertEquals("{1=1, 2=2, 3=3}", string);
}

Welcome: Two new Collectors on Collectors2

@Test
public void countByEach()
{
MutableBag<Integer> bag = Arrays.asList(5, 4, 3, 2, 1)
.stream()
.collect(Collectors2.countByEach(Interval::oneTo));

MutableBag<Integer> expected = Bags.mutable.empty();
expected.addOccurrences(5, 1);
expected.addOccurrences(4, 2);
expected.addOccurrences(3, 3);
expected.addOccurrences(2, 4);
expected.addOccurrences(1, 5);

Assert.assertEquals(expected, bag);
}

And there is more…

Have fun using Eclipse Collections 9.2 and all of the new features in your Java projects!

Eclipse Collections is open for contributions. If you like the library, you can let us know by starring it on GitHub.

Oracle Developers

A community for developers by developers.

Oracle Developers

Aggregation of articles from Oracle engineers, Groundbreaker Ambassadors, Oracle ACEs, and Java Champions on all things Oracle technology. The views expressed are those of the authors and not necessarily of Oracle.

Donald Raab

Written by

Java Champion. Creator of the Eclipse Collections OSS Java library (http://www.eclipse.org/collections/). Inspired by Smalltalk. Opinions are my own.

Oracle Developers

Aggregation of articles from Oracle engineers, Groundbreaker Ambassadors, Oracle ACEs, and Java Champions on all things Oracle technology. The views expressed are those of the authors and not necessarily of Oracle.