Halloween Kata Solutions
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)
APIs covered in Kata
- flatCollect — flattens a nested collection of collections based on some attribute specified in a Function.
- toBag — converts a collection to a Bag.
- 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.
- collect — transforms a collection from one type to another using a specified Function.
- toSet — converts a collection to a Set.
- reduce — applies a BinaryOperator to all elements of the collection, in this case a call to intersect two sets.
- intersect — returns the result of intersecting two sets.