Halloween Kata Solutions

Here are my solutions to the Halloween Kata using Eclipse Collections APIs.

Image for post
Image for post

Top Candy

For the solutions, I used flatCollect, toBag, topOccurrences, collect, toSet, reduce and intersect.

public void topCandy()
MutableList<Bag<Candy>> bagsOfCandy =

// Hint: Flatten the Bags of Candy into a single Bag
Bag<Candy> bigBagOfCandy =
bagsOfCandy.flatCollect(bag -> bag).toBag();

// Hint: Find the top occurrence in the bag and convert that
// to a set.
MutableSet<Candy> mostCommon =


// Hint: Find the top 10 occurrences of Candy in each of the
// bags and intersect them.
MutableSet<Candy> commonInTop10 =
bag -> bag.topOccurrences(10)

Sets.mutable.with(Candy.REESES_PIECES, Candy.CRUNCH),

APIs covered in Kata

  1. flatCollect — flattens a nested collection of collections based on some attribute specified in a Function.
  2. toBag — converts a collection to a Bag.
  3. topOccurrences — find the top occurrences of items in a Bag based on their counts. The List returned will be bigger than the specified count requested if there are any ties.
  4. collect — transforms a collection from one type to another using a specified Function.
  5. toSet — converts a collection to a Set.
  6. reduce — applies a BinaryOperator to all elements of the collection, in this case a call to intersect two sets.
  7. intersect — returns the result of intersecting two sets.

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