New Features of Eclipse Collections 10.0 — Part 3

Examples of the final six new features in the latest major release of the Eclipse Collections library.

Image for post
Image for post
Image for post
Image for post
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

9.2 Version

private void rehashAndGrow()
{
this.rehash(this.table.length << 1);
}

10.0 Version

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

https://github.com/eclipse/eclipse-collections/issues/737

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)

Example usage:

@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!

I hope you enjoy all of the new features in Eclipse Collections 10.0!

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

Written by

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

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store