Howto count each entity type in the chunk?

I would like make something like chunk spawn limiter. I would like set limits for each type of mob. Howto count each mob type in the chunk?

My listener:

    @Listener(order = Order.FIRST)
    @Exclude(SpawnEntityEvent.ChunkLoad.class)
    public void spawn(SpawnEntityEvent event) {
        Entity entity = event.getEntities().get(0);
        Optional chunk = entity.getWorld().getChunk(entity.getLocation().getChunkPosition());
        Chunk entityChunk = null;
        if (chunk.isPresent()) {
            entityChunk = chunk.get();
            if (entityChunk.getEntities().size() >= 20) {
                event.setCancelled(true);
            }
        }
    
    }

I not understand howto filter with Predicate<<>Entity>, can anyone show me some example?

Thanks a lot! That is exactly what I looking for.

Note that a lambda is much cleaner and more concise than an anonymous class.
Where you would otherwise type

Predicate<Entity> filter = new Predicate<Entity>() {
    @Override
    public boolean test(Entity entity) {
        return entity.getType().equals(entityType);
    }
}

it is much easier to type

Predicate<Entity> filter = (entity) -> {
    return entity.getType().equals(entityType);
}

or even

Predicate<Entity> filter = entity -> entity.getType().equals(entityType)

At that point, you don’t even need a variable, and would just put it directly in the World#getEntities call.