New Features of Eclipse Collections 10.0 — Part 1

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

Summary

1. MultiReaderList/Bag/Set Interfaces

MultiReaderList/Set/Bag Interfaces
@Test
public void multiReaderList()
{
MultiReaderList<String> list =
Lists.multiReader.with("1", "2", "3");

list.withWriteLockAndDelegate(backingList -> {
Iterator<String> iterator = backingList.iterator();
iterator.next();
iterator.remove();
});
Assert.assertEquals(Lists.mutable.with("2", "3"), list);
}

2. Stream for Primitive Lists

@Test
public void primitiveListToPrimitiveStream()
{
IntStream intStream1 =
IntLists.mutable.with(1, 2, 3, 4, 5)
.primitiveStream();
IntStream intStream2 =
IntLists.immutable.with(1, 2, 3, 4, 5)
.primitiveStream();

LongStream longStream1 =
LongLists.mutable.with(1L, 2L, 3L, 4L, 5L)
.primitiveStream();
LongStream longStream2 =
LongLists.immutable.with(1L, 2L, 3L, 4L, 5L)
.primitiveStream();

DoubleStream doubleStream1 =
DoubleLists.mutable.with(1.0, 2.0, 3.0, 4.0, 5.0)
.primitiveStream();
DoubleStream doubleStream2 =
DoubleLists.immutable.with(1.0, 2.0, 3.0, 4.0, 5.0)
.primitiveStream();
}

3. toMap supports passing a target Map

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

Map<String, Integer> map =
list.toMap(String::valueOf,
each -> each,
new LinkedHashMap<>());

Map<String, Integer> expected = new LinkedHashMap<>();
expected.put("1", 1);
expected.put("2", 2);
expected.put("3", 3);
expected.put("4", 4);
expected.put("5", 5);

Assert.assertEquals(expected, map);
}

4. MutableMapIterable removeAllKeys

@Test
public void removeAllKeys()
{
MutableMap<Integer, String> map =
Maps.mutable.with(1, "1", 2, "2", 3, "3");

map.removeAllKeys(Sets.mutable.with(1, 3));

Assert.assertEquals(Maps.mutable.with(2, "2"), map);
}

5. RichIterable toBiMap

@Test
public void toBiMap()
{
MutableBiMap<String, Integer> expected =
BiMaps.mutable.with("1", 1, "2", 2, "3", 3);

MutableBiMap<String, Integer> biMap =
Lists.mutable.with(1, 2, 3).toBiMap(String::valueOf, i -> i);

Assert.assertEquals(expected, biMap);
}

6. MultiMap collectKeyMultiValues

@Test
public void collecKeyMultiValues()
{
MutableListMultimap<String, String> multimap =
Multimaps.mutable.list.with(
"nj", "Monmouth",
"nj", "Bergen",
"nj", "Union");

MutableBagMultimap<String, String> transformed =
multimap.collectKeyMultiValues(
String::toUpperCase,
String::toUpperCase);

Assert.assertEquals(Multimaps.mutable.bag.with(
"NJ", "MONMOUTH",
"NJ", "BERGEN",
"NJ", "UNION"), transformed);
}

7. fromStream on Collection Factories

@Test
public void fromStreamOnCollectionFactories()
{
MutableList<Integer>
list = Lists.mutable.fromStream(Stream.of(1, 2, 3, 4, 5));
Assert.assertEquals(
Lists.mutable.with(1, 2, 3, 4, 5), list);

MutableSet<Integer> set =
Sets.mutable.fromStream(Stream.of(1, 2, 3, 4, 5));
Assert.assertEquals(
Sets.mutable.with(1, 2, 3, 4, 5), set);

MutableBag<Integer> bag =
Bags.mutable.fromStream(Stream.of(1, 2, 3, 4, 5));
Assert.assertEquals(
Bags.mutable.with(1, 2, 3, 4, 5), bag);

MutableStack<Integer> stack =
Stacks.mutable.fromStream(Stream.of(1, 2, 3, 4, 5));
Assert.assertEquals(
Stacks.mutable.with(1, 2, 3, 4, 5), stack);
}

8. LazyIterate cartesianProduct

@Test
public void cartesianProduct()
{
MutableList<Integer> numbers = Lists.mutable.with(1, 2, 3);
MutableList<String> letters = Lists.mutable.with("A", "B", "C");

MutableList<Pair<String, Integer>> pairs =
LazyIterate.cartesianProduct(letters, numbers).toList();

MutableList<Pair<String, Integer>> expected =
Lists.mutable.with(
Tuples.pair("A", 1),
Tuples.pair("A", 2),
Tuples.pair("A", 3),
Tuples.pair("B", 1),
Tuples.pair("B", 2),
Tuples.pair("B", 3),
Tuples.pair("C", 1),
Tuples.pair("C", 2),
Tuples.pair("C", 3));

Assert.assertEquals(expected, pairs);
}

9. Primitive Maps updateValues

@Test
public void updateValues()
{
MutableIntIntMap map = IntIntMaps.mutable.empty()
.withKeyValue(1, 5)
.withKeyValue(2, 3)
.withKeyValue(3, 10);

map.updateValues((k, v) -> v + 1);

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

Assert.assertEquals(expected, map);
}

10. MutableMultimap getIfAbsentPutAll

@Test
public void getIfAbsentPutAll()
{
MutableListMultimap<Integer, Integer> multimap =
Multimaps.mutable.list.with(2, 1);

ImmutableList<Integer> defaultValue =
Lists.immutable.with(1, 2, 3);
MutableList<Integer> oneValue =
multimap.getIfAbsentPutAll(1, defaultValue);
MutableList<Integer> twoValue =
multimap.getIfAbsentPutAll(2, defaultValue);

Assert.assertEquals(defaultValue, oneValue);
Assert.assertEquals(Lists.mutable.with(1), twoValue);
}

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