EC by Example: Counting

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

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.

Counting with a Predicate

There is also a countWith method which takes a Predicate2 and a countBy method which takes a Function.

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;

Now, let’s count some things. First, I will create a MutableBag of SkyEntity instances using addOccurrences to set the counts directly. Then I will convert the MutableBag to a MutableList. Then I will use count to count the number of HELICOPTER and countWith to count the number of SKYSCRAPER.

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();


list.countWith(Object::equals, SkyEntity.SKYSCRAPER));

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


Finally, I group and count the SkyEntity who can and can’t fly using countBy with a Function which returns a Boolean.

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.

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 =
"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.

Check out this presentation to learn more about the origins, design and evolution of the Eclipse Collections API.

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 ( Inspired by Smalltalk. Opinions are my own.

Java Champion. Creator of the Eclipse Collections OSS Java library ( Inspired by Smalltalk. Opinions are my own.