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

In this blog I will cover the final six of the twenty six new features listed in the Eclipse Collections 10.0 Release Summary Blog. Part one of the feature blog series covered the first ten features in Eclipse Collections 10.0. Part two covered the second ten features.

21. RichIterable.getAny

The method getAny will return the first element return from a Collection without any guarantee of order. This method is meant to be used as a better alternative to getFirst which has been deprecated on RichIterable for non-ordered Collections.

@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

This item is more an improvement than a user feature but I thought it was notable enough to give a shoutout as it impacts all of our primitive hash structures. The bulk of the change was in the rehashAndGrow method for 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

Before Eclipse Collections 10.0, if you wanted to convert an Iterable of boxed primitive types to a primitive Collection (e.g. Integer -> int), you would have to first convert the Iterable to a Collection or a Stream. In the case of a Stream, you could then use one of the stock primitive Collectors in Collectors2 to convert to a primitive Collection. Now you can use the following factory methods to convert from Iterable of some boxed value like Integer to a primitive Collection. This works for all of the boxed primitive types, across all supported primitive Collection types.

@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

We’ve been missing an ImmutableSortedBagMultimapFactory for a while now on the Multimaps class. The feature was added, but there is currently a naming issue that I discovered while blogging about this feature and have raised an issue to fix.

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

25. Map factory method that takes a Map parameter

You can now construct a MutableMap using another Map or MapIterable as a parameter on the Maps factory class.

@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

This feature was a generics change made to add(Pair) and the two putAllPairs methods in MutableMultimap. The Pair parameters both now use ? extends.

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’ve now covered all of the features in Eclipse Collections 10.0.

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.

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