Eclipse Collections is an open source Java Collections framework. In this blog I am going to demonstrate four lesser known features of the framework. I have published similar blogs in Java Advent Calendars of 2018, 2019, 2020, 2021, and 2022. Please refer to the resources at the end of the blog for more information about the framework.
HashingStrategy
: If you want to define a hashing strategy to have a customhashCode()
andequals()
without overriding the defaulthashCode()
andequals()
then useHashingStrategy
. This is particularly useful in conjunction withUnifiedSetWithHashingStrategy
,UnifiedMapWithHashingStrategy
, andHashBagWithHashingStrategy
. The code examplesHashingStrategy
and Collections withHashingStrategy
are presented together- Collections with
HashingStrategy
: Eclipse Collections offersSet
,Map
, andBag
wherein the uniqueness is determined by a customhashCode()
andequals()
. This is particularly useful when we cannot modify the defaulthashCode()
andequals()
. This is generally useful during data processing before inserting in the database. Hashing Strategies can be used to ensure uniqueness based on a primary key. This is especially useful while using Java Records.// Domain public record Person(String firstName, String lastName) { } Person person1 = new Person("Alex", "Smith"); Person person2 = new Person("John", "Smith"); Person person3 = new Person("John", "Brown"); List<Person> people = Lists.mutable.of(person1, person2, person3); // Uniqueness is defined by Last Name HashingStrategy<Person> LAST_NAME_HASHING_STRATEGY = HashingStrategies.fromFunction(Person::lastName);
@Test public void unifiedSetWithHashingStrategy() { MutableSet<Person> hashingStrategySet = UnifiedSetWithHashingStrategy.newSet( LAST_NAME_HASHING_STRATEGY, people); Assertions.assertEquals( Sets.mutable.with(person1, person3), hashingStrategySet, "Uniqueness defined by only Last Name"); }
@Test public void unifiedMapWithHashingStrategy() { MutableMap<Person, Integer> hashingStrategyMap = UnifiedMapWithHashingStrategy.newMapWith(LAST_NAME_HASHING_STRATEGY); hashingStrategyMap.put(person1, 1); // The key is considered same due to same last name. // Hence, by Map symantics, the new value will be stored hashingStrategyMap.put(person2, 2); hashingStrategyMap.put(person3, 3); Assertions.assertEquals( Maps.mutable.with(person1, 2, person3, 3), hashingStrategyMap, "Uniqueness defined by only Last Name"); }
@Test public void hashBagWithHashingStrategy() { MutableBag<Person> hashingStrategyBag = HashBagWithHashingStrategy.newBag( LAST_NAME_HASHING_STRATEGY, people); Assertions.assertEquals( Bags.mutable.with(person1, person1, person3), hashingStrategyBag, "Uniqueness defined by only Last Name, hence, person1 comes up twice"); }
detect
: is used to find the first instance of an element that satisfies thePredicate
. In case no element satisfies thePredicate
then anull
is returned. This method is similar tofindFirst()
in JDK Collections except that anull
is returned when no element is found.@Test public void detect() { var list = Lists.mutable.with(1, 2, 3); Assertions.assertEquals( 2, list.detect(each -> each % 2 == 0)); Assertions.assertNull( list.detect(each -> each % 4 == 0)); }
detectOptional
: This is a correlated method todetect
wherein instead of returning an element that satisfies thePredicate
anOptional
is returned. In case no element satisfies thePredicate
then an emptyOptional
is returned. This method is equivalent tofindFirst()
in JDK Collections.@Test public void detectOptional() { var list = Lists.mutable.with(1, 2, 3); Assertions.assertEquals( Optional.of(2), list.detectOptional(each -> each % 2 == 0)); Assertions.assertEquals( list.stream() .filter(each -> each % 2 == 0) .findFirst(), list.detectOptional(each -> each % 2 == 0)); Assertions.assertEquals( Optional.empty(), list.detectOptional(each -> each % 4 == 0)); Assertions.assertEquals( list.stream() .filter(each -> each % 4 == 0) .findFirst(), list.detectOptional(each -> each % 4 == 0)); }
Summary
In this blog I explained a few lesser known features of Eclipse Collections namely HashingStrategy
, Collections with HashingStrategies
, detect
, and detectOptional
.
I hope you found the post informative. If you have not used Eclipse Collections before, give it a try. There are few resources below. Make sure you show us your support and put a star on our GitHub Repository
Eclipse Collections Resources
Eclipse Collections comes with it’s own implementations of List, Set and Map. It also has additional data structures like Multimap, Bag and an entire Primitive Collections hierarchy. Each of our collections have a fluent and rich API for commonly required iteration patterns.
- Website
- Source code on GitHub (Make sure to star the Repository)
- Contribution Guide
- Reference Guide
Author: Nikhil Nanivadekar
Lead Eclipse Collections: eclipse.org/collections, Java Champion. I enjoy hiking, skiing, reading. All opinions stated by me are my own.