The missing Java data structures no one ever told you about — Part 1
Eclipse Collections provides additional collection types not found in the JDK.
Data structures you won’t find in the JDK
The JDK may not have all of the data structures you need to build your Java applications. In part 1 of this blog series, I will cover several data structures that you will not find in the JDK today, but that are available in Eclipse Collections.
Interval
- An
Interval
is a range ofInteger
values with afrom
,to
, andstep
value. - An
Interval
is immutable. - An
Interval
may be forward or reverse depending on whether thefrom
is less than or greater than theto
value. - In Eclipse Collections, the
Interval
class is aList<Integer>
and is alsoRandomAccess
. - In Eclipse Collections, the
Interval
class is also aLazyIterable<Integer>
. - An
Interval
is useful for quickly (and efficiently) creating a range ofInteger
values as aList
. Because anInterval
is aList<Integer>
, it can be used for testing equality with otherList
instances.
Bag
- A
Bag
is an unorderedCollection
that allows duplicates. - A
Bag
is similar to aMap<K, Integer>
. - A
Bag
is useful for counting things. - The method
countBy
takes aFunction
and returns aBag
. - The method
toBag
converts any Eclipse Collections type to aBag
.
Nikhil Nanivadekar has a great blog which explains the Bag
implementation in Eclipse Collections. The blog includes memory and performance comparisons.
Multimap
- A
Multimap
is aMap
-like data structure that allows multiple values per key. - A
Multimap
is similar to aMap<K, Collection<V>>
. - A
Multimap
is useful for grouping things. - The method
groupBy
takes aFunction
and returns aMultimap
.
Nikhil Nanivadekar also has a great blog which explains the Multimap
implementation in Eclipse Collections. The blog includes memory comparisons as well.
HashingStrategy + Pool
HashingStrategy
is an interface that helps you externalize a definition of uniqueness for an object.- Developers need to implement two methods on the interface —
computeHashCode
andequals
. - A
HashingStrategy
is useful for creating hash based data structures without requiring dedicated key objects to define uniqueness. - A
Pool
is an interface with aput
andget
method on it. It is like aSet
in that it holds unique values, and is like aMap
in that it provides aget
method to look up values.
There are several HashingStrategy
data structures available today in Eclipse Collections.
UnifiedSetWithHashingStrategy
— Both aMutableSet
and aPool
UnifiedMapWithHashingStrategy
— aMutableMap
HashBagWithHashingStrategy
— aMutableBag
Object<Primitive>HashMapWithHashingStrategy
— Available for all primitive types.
BiMap
- A
BiMap
allows lookups based on key or value. - Both keys and values must be unique
- The value lookup is available by calling
inverse
.
But wait, there’s more…
These are just some of the data structures you won’t find in the JDK today. In part 2 and part 3 of this blog series, I will cover MultiReader
collections and primitive collections.
I am a Project Lead and Committer for the Eclipse Collections OSS project at the Eclipse Foundation. Eclipse Collections is open for contributions. If you like the library, you can let us know by starring it on GitHub.
Other Java Programming articles you may like:
10 Things Java Programmer Should Learn
10 Programming languages You can Learn
10 Tools Every Java Developer Should Know
10 Reasons to Learn Java Programming languages in 2021
The Complete Java Developer roadmap
My Favorite Free Programming Courses for Beginners
10 Free Data Structure and Algorithms Courses
7 Best Data Structure And Algorithms Courses for Beginners
50+ Data Structure and Algorithms Interview questions