Tuesday 31 March 2020

TableSaw: count: Count number of elements that satisfy given predicate

default int count(Predicate<? super T> test)
‘count’ function returns number of elements that satisfy given predicate.

Example : Count number of elements that are > 20
Predicate<Integer> predicate = new Predicate<Integer> () {

         @Override
         public boolean test(Integer t) {
                  return t > 20;
         }
        
};

int eleGreater20 = numbersColumn.count(predicate);

App.java
package com.sample.app;

import java.util.function.Predicate;

import tech.tablesaw.api.IntColumn;

public class App {

 public static void main(String args[]) {
  int[] numbers = { 23, 45, 6, 32, 1, 43 };
  
  IntColumn numbersColumn = IntColumn.create("My Numbers", numbers);
  
  Predicate<Integer> predicate = new Predicate<Integer> () {

   @Override
   public boolean test(Integer t) {
    return t > 20;
   }
   
  };
  
  int eleGreater20 = numbersColumn.count(predicate);
  

  System.out.println("Total number of elements > 20 : " + eleGreater20);
 }
}

Output
Total number of elements > 20 : 4




Previous                                                    Next                                                    Home

No comments:

Post a Comment