Halloween Kata Solutions

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

Top Candy

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

@Test
public void topCandy()
{
MutableList<Bag<Candy>> bagsOfCandy =
this.collectBagsOfCandy();

// 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 =
bigBagOfCandy.topOccurrences(1)
.collect(ObjectIntPair::getOne)
.toSet();

Assert.assertEquals(
Sets.mutable.with(Candy.REESES_PIECES),
mostCommon);

// Hint: Find the top 10 occurrences of Candy in each of the
// bags and intersect them.
MutableSet<Candy> commonInTop10 =
bagsOfCandy.collect(
bag -> bag.topOccurrences(10)
.collect(ObjectIntPair::getOne)
.toSet())
.reduce(MutableSet::intersect)
.get();

Assert.assertEquals(
Sets.mutable.with(Candy.REESES_PIECES, Candy.CRUNCH),
commonInTop10);
}

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.