New Features of Eclipse Collections 10.0 — Part 2

Donald Raab
Jul 22 · 4 min read
Ten new features in Eclipse Collections 10.0

Summary

11. Bag.collectWithOccurrences

@Test
public void collectWithOccurences()
{
MutableBag<String> source =
Bags.mutable.with("1", "2", "2", "3", "3", "3");

MutableBag<ObjectIntPair<String>> targetBag =
source.collectWithOccurrences(PrimitiveTuples::pair);

MutableBag<ObjectIntPair<String>> expected =
Bags.mutable.with(
PrimitiveTuples.pair("1", 1),
PrimitiveTuples.pair("2", 2),
PrimitiveTuples.pair("3", 3));

Assert.assertEquals(expected, targetBag);
}

12. Reduce / reduceIfEmpty for Primitive Iterables

@Test
public void reducePrimitiveIterables()
{
MutableIntList list =
IntLists.mutable.with(1, 2, 3, 4, 5);

long sum = list.reduce(Long::sum);

Assert.assertEquals(15L, sum);
Assert.assertEquals(list.sum(), sum);

Verify.assertThrows(
NoSuchElementException.class,
()-> IntLists.mutable.empty().reduce(Long::sum));
}
@Test
public void reduceIfEmptyPrimitiveIterables()
{
MutableIntList list =
IntLists.mutable.with(1, 2, 3, 4, 5);

long sum = list.reduceIfEmpty(Long::sum, 0L);

Assert.assertEquals(15L, sum);
Assert.assertEquals(list.sum(), sum);

Assert.assertEquals(0L,
IntLists.mutable.empty()
.reduceIfEmpty(Long::sum, 0L));
}

13. New <T1><T2>To<T1>Function for primitives

@Test
public void newLongTypeToLongFunctions()
{
MutableIntList intList =
IntLists.mutable.with(1, 2, 3, 4, 5);

LongIntToLongFunction sumFunction1 = Long::sum;
long sum1 = intList.reduceIfEmpty(sumFunction1, 0L);

Assert.assertEquals(15L, sum1);

MutableByteList byteList =
ByteLists.mutable.with((byte) 1, (byte) 2, (byte) 3);

LongByteToLongFunction sumFunction2 = Long::sum;
long sum2 = byteList.reduceIfEmpty(sumFunction2, 0L);

Assert.assertEquals(6L, sum2);
}

14. Of/withInitialCapacity to Primitive Maps

@Test
public void ofAndWithInitialCapacity()
{
MutableIntIntMap map1 =
IntIntMaps.mutable.ofInitialCapacity(100);

MutableIntIntMap map2 =
IntIntMaps.mutable.withInitialCapacity(100);

Assert.assertEquals(map1, map2);
}

15. RichIterable.countByEach

@Test
public void countByEach()
{
MutableList<Class<?>> classes =
Lists.mutable.with(
RichIterable.class,
MutableList.class,
ImmutableList.class);

Bag<String> methodNames =
classes.countByEach(each ->
ArrayAdapter.adapt(each.getMethods())
.collect(Method::getName));

Assert.assertEquals(8,
methodNames.occurrencesOf("countByEach"));
Assert.assertEquals(16,
methodNames.occurrencesOf("groupByEach"));
Assert.assertEquals(2,
methodNames.occurrencesOf("sortThis"));
}

16. UnifiedSetWithHashingStrategy.addOrReplace

@Test
public void addOrReplace()
{
UnifiedSetWithHashingStrategy<Integer> set =
new UnifiedSetWithHashingStrategy<>(
HashingStrategies.defaultStrategy());
Integer one = new Integer(1);
set.addOrReplace(one);
Assert.assertSame(one, set.get(1));
set.add(new Integer(1));
Assert.assertSame(one, set.get(1));
Integer otherOne = new Integer(1);
set.addOrReplace(otherOne);
Integer integer = set.get(otherOne);
Assert.assertSame(otherOne, integer);
Assert.assertNotSame(one, integer);
}

17. UnmodifiableMutableOrderedMap

@Test
public void unmodifiableMutableOrderedMap()
{
MutableOrderedMap<Object, Object> map =
OrderedMaps.adapt(new LinkedHashMap<>())
.asUnmodifiable();

Verify.assertInstanceOf(
UnmodifiableMutableOrderedMap.class,
map);
}

18. WithAllKeyValues on mutable primitive maps

@Test
public void withAllKeyValues()
{
MutableIntIntMap map =
IntIntMaps.mutable.empty()
.withAllKeyValues(
Lists.mutable.with(
PrimitiveTuples.pair(1, 1),
PrimitiveTuples.pair(2, 2),
PrimitiveTuples.pair(3, 3)
))
.withAllKeyValues(
Lists.mutable.with(
PrimitiveTuples.pair(4, 4),
PrimitiveTuples.pair(5, 5)
));

MutableIntIntMap expected = IntIntMaps.mutable.empty()
.withKeyValue(1, 1)
.withKeyValue(2, 2)
.withKeyValue(3, 3)
.withKeyValue(4, 4)
.withKeyValue(5, 5);

Assert.assertEquals(expected, map);
}

19. Create Primitive Map from Iterable

@Test
public void createPrimitiveMapFromIterable()
{
Iterable<Integer> integers = Interval.oneTo(5);
MutableIntIntMap map =
IntIntMaps.mutable.from(
integers,
key -> key,
value -> value);

MutableIntIntMap expected = IntIntMaps.mutable.empty()
.withKeyValue(1, 1)
.withKeyValue(2, 2)
.withKeyValue(3, 3)
.withKeyValue(4, 4)
.withKeyValue(5, 5);

Assert.assertEquals(expected, map);
}

20. HashingStrategySets.of/withInitialCapacity

@Test
public void hashingStrategySetsOfOrWithInitialCapacity()
{
MutableSet<Object> set1 =
HashingStrategySets.mutable.ofInitialCapacity(
HashingStrategies.defaultStrategy(),
100);

MutableSet<Object> set2 =
HashingStrategySets.mutable.withInitialCapacity(
HashingStrategies.defaultStrategy(),
100);

Assert.assertEquals(set1, set2);
}

Stay Tuned!

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.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade