The missing Java data structures no one ever told you about — Part 1

Donald Raab
Javarevisited
Published in
3 min readAug 2, 2021

Eclipse Collections provides additional collection types not found in the JDK.

A mind map with branches for Interval, Bag, Multimap, Hashing Strategy, BiMap, Pool, MultiReader and primitive collections.
Eclipse Collections data structures 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 of Integer values with a from, to, and step value.
  • An Interval is immutable.
  • An Interval may be forward or reverse depending on whether the from is less than or greater than the to value.
  • In Eclipse Collections, theInterval class is a List<Integer> and is also RandomAccess.
  • In Eclipse Collections, the Interval class is also a LazyIterable<Integer>.
  • An Interval is useful for quickly (and efficiently) creating a range of Integer values as a List. Because an Interval is a List<Integer>, it can be used for testing equality with other List instances.
Interval Tests

Bag

  • A Bag is an unordered Collection that allows duplicates.
  • A Bag is similar to a Map<K, Integer>.
  • A Bag is useful for counting things.
  • The method countBy takes a Function and returns a Bag.
  • The method toBag converts any Eclipse Collections type to a Bag.

Nikhil Nanivadekar has a great blog which explains the Bag implementation in Eclipse Collections. The blog includes memory and performance comparisons.

Bag Tests

Multimap

  • A Multimap is a Map-like data structure that allows multiple values per key.
  • A Multimap is similar to a Map<K, Collection<V>>.
  • A Multimap is useful for grouping things.
  • The method groupBy takes a Function and returns a Multimap.

Nikhil Nanivadekar also has a great blog which explains the Multimap implementation in Eclipse Collections. The blog includes memory comparisons as well.

Multimap Tests

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 and equals.
  • A HashingStrategy is useful for creating hash based data structures without requiring dedicated key objects to define uniqueness.
  • A Pool is an interface with a put and get method on it. It is like a Set in that it holds unique values, and is like a Map in that it provides a get method to look up values.

There are several HashingStrategy data structures available today in Eclipse Collections.

  1. UnifiedSetWithHashingStrategy — Both a MutableSet and a Pool
  2. UnifiedMapWithHashingStrategy — a MutableMap
  3. HashBagWithHashingStrategy — a MutableBag
  4. 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

--

--

Donald Raab
Javarevisited

Java Champion. Creator of the Eclipse Collections OSS Java library (https://github.com/eclipse/eclipse-collections). Inspired by Smalltalk. Opinions are my own.