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?

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

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.

@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));
}

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

@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. countBy — counts elements of a collection grouping them by some Function.
  2. Bags.mutable.empty — creates an empty MutableBag.
  3. occurrencesOf — counts the number of occurrences of an item in a Bag.
  4. toList — converts a collection to a MutableList.
  5. IntInterval.oneTo — creates an IntInterval which is an ImmutableIntList from one to the specified number.
  6. IntPredicates.isEven / isOdd — creates an IntPredicate which tests if an int is even or odd.
  7. 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 (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