New Features of Eclipse Collections 10.0 — Part 3

Donald Raab
Jul 29 · 3 min read
The final six new features in Eclipse Collections 10.0

Summary

21. RichIterable.getAny

@Test
public void getAny()
{
Interval interval = Interval.fromTo(100, 1);
Integer anyList =
Lists.mutable.withAll(interval).getAny();
Integer anySet =
Sets.mutable.withAll(interval).getAny();
Integer anyBag =
Bags.mutable.withAll(interval).getAny();
Integer anyStack =
Stacks.mutable.withAll(interval).getAny();
Assert.assertEquals(Integer.valueOf(100), anyList);
Assert.assertEquals(Integer.valueOf(1), anySet);
Assert.assertEquals(Integer.valueOf(1), anyBag);
Assert.assertEquals(Integer.valueOf(1), anyStack);
}

22. Revamp and standardize resize/rehash for primitive hash structures

private void rehashAndGrow()
{
this.rehash(this.table.length << 1);
}
private void rehashAndGrow()
{
int max = this.maxOccupiedWithData();
int newCapacity = Math.max(max, smallestPowerOfTwoGreaterThan((this.occupiedWithData + 1) << 1));
if (this.occupiedWithSentinels > 0 && (max >> 1) + (max >> 2) < this.occupiedWithData)
{
newCapacity <<= 1;
}
this.rehash(newCapacity);
}

23. Factory methods to convert Iterable to Primitive Collections

@Test
public void convertFromIterableToPrimitiveCollection()
{
Iterable<Integer> iterable = Interval.oneTo(5);
IntInterval intInterval = IntInterval.oneTo(5);
MutableIntList mIntList =
IntLists.mutable.withAll(iterable);
ImmutableIntList iIntList =
IntLists.immutable.withAll(iterable);
Assert.assertEquals(intInterval, mIntList);
Assert.assertEquals(intInterval, iIntList);
MutableIntSet mIntSet =
IntSets.mutable.withAll(iterable);
ImmutableIntSet iIntSet =
IntSets.immutable.withAll(iterable);
Assert.assertEquals(intInterval.toSet(), mIntSet);
Assert.assertEquals(intInterval.toSet(), iIntSet);
MutableIntBag mIntBag =
IntBags.mutable.withAll(iterable);
ImmutableIntBag iIntBag =
IntBags.immutable.withAll(iterable);
Assert.assertEquals(intInterval.toBag(), mIntBag);
Assert.assertEquals(intInterval.toBag(), iIntBag);
}

24. ImmutableSortedBagMultimapFactory in Multimaps

25. Map factory method that takes a Map parameter

@Test
public void mapFactoryThatTakesMapAsParameter()
{
MutableMap<Integer, Integer> mutableSource =
Maps.mutable.with(1, 1, 2, 2, 3, 3);
ImmutableMap<Integer, Integer> immutableSource =
Maps.immutable.with(1, 1, 2, 2, 3, 3);
Assert.assertEquals(mutableSource, immutableSource); MutableMap<Integer, Integer> mutableOf =
Maps.mutable.ofMap(mutableSource);
MutableMap<Integer, Integer> mutableWith =
Maps.mutable.withMap(mutableSource);
Assert.assertEquals(mutableSource, mutableOf);
Assert.assertEquals(immutableSource, mutableWith);
MutableMap<Integer, Integer> mutableOfMI =
Maps.mutable.ofMapIterable(immutableSource);
MutableMap<Integer, Integer> mutableWithMI =
Maps.mutable.withMapIterable(immutableSource);
Assert.assertEquals(immutableSource, mutableOfMI);
Assert.assertEquals(immutableSource, mutableWithMI);
}

26. Wildcard type in MultableMultimap.putAllPairs & add methods

boolean add(Pair<? extends K, ? extends V> keyValuePair)
boolean putAllPairs(Pair<? extends K, ? extends V>... pairs)
boolean putAllPairs(Iterable<? extends Pair<? extends K, ? extends V>> pairs)
@Test
public void wildcardInMutableMultimapPutAllPairs()
{
MutableListMultimap<CharSequence, CharSequence> multimap =
Multimaps.mutable.list.empty();
multimap.add(Tuples.pair("1-5", "1"));
multimap.putAllPairs(
Tuples.pair("1-5", "2"),
Tuples.pair("1-5", "3"));
multimap.putAllPairs(
Lists.mutable.with(
Tuples.pair("1-5", "4"),
Tuples.pair("1-5", "5")));
Multimap<String, String> expected =
Interval.oneTo(5)
.collect(Object::toString)
.groupBy(e -> "1-5");
Assert.assertEquals(expected, multimap);
}

That’s all folks!

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