EC by Example: Counting

Learn how to count the elements of a collection that match a Predicate using Eclipse Collections.

Image for post
Image for post
How many helicopters are there?

How do you count using Eclipse Collections?

The method count takes a Predicate as a parameter and returns the number of the elements in a collection that match.

Image for post
Image for post
Counting with a Predicate

Counting elements of a Collection

Here’s a simple enum we’ll use to identify different things in a Collection.

public enum SkyEntity
{
HELICOPTER(true), SKYSCRAPER(false), BIRD(true);

SkyEntity(boolean flies)
{
this.flies = flies;
}

private boolean flies;

public boolean canFly()
{
return this.flies;
}
}
@Test
public void count()
{
MutableBag<SkyEntity> bag = Bags.mutable.empty();
bag.addOccurrences(SkyEntity.HELICOPTER, 12);
bag.addOccurrences(SkyEntity.SKYSCRAPER, 10);
bag.addOccurrences(SkyEntity.BIRD, 100);
MutableList<SkyEntity> list = bag.toList();

Assert.assertEquals(
bag.occurrencesOf(SkyEntity.HELICOPTER),
list.count(SkyEntity.HELICOPTER::equals));

Assert.assertEquals(
bag.occurrencesOf(SkyEntity.SKYSCRAPER),
list.countWith(Object::equals, SkyEntity.SKYSCRAPER));

Bag<Boolean> flies = list.countBy(SkyEntity::canFly);

Assert.assertEquals(
flies.occurrencesOf(Boolean.TRUE),
list.count(SkyEntity::canFly));
}

Counting Primitive Collections

Eclipse Collections has a full complement of primitive collections. Each primitive collections supports a count API. Here are a couple examples of counting with primitive collections.

@Test
public void countPrimitive()
{
IntList intList = IntInterval.oneTo(10);
Assert.assertEquals(5, intList.count(IntPredicates.isEven()));
Assert.assertEquals(5, intList.count(IntPredicates.isOdd()));
Assert.assertEquals(3, intList.count(i -> i % 3 == 0));

CharAdapter chars =
Strings.asChars(
"the quick brown fox jumps over the lazy dog");
Assert.assertEquals(2, chars.count(c -> c == 't'));
Assert.assertEquals(8, chars.count(Character::isWhitespace));
}

APIs covered in the examples

  1. count / countWith — counts the number of elements of a collection that match a given Predicate or Predicate2.
  2. countBy — counts elements of a collection grouping them by some Function.
  3. Bags.mutable.empty — creates an empty MutableBag.
  4. occurrencesOf — counts the number of occurrences of an item in a Bag.
  5. toList — converts a collection to a MutableList.
  6. IntInterval.oneTo — creates an IntInterval which is an ImmutableIntList from one to the specified number.
  7. IntPredicates.isEven / isOdd — creates an IntPredicate which tests if an int is even or odd.
  8. Strings.asChars — creates a CharAdapter wrapping the specified String.

Written by

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