Showing posts with label elements. Show all posts
Showing posts with label elements. Show all posts

Monday, 13 April 2020

TableSaw: Sort the elements of a table

‘sortOn’ method is used to sort the elements of a table based on column contents.

public Table sortOn(String... columnNames)
public Table sortOn(int... columnIndexes)

Example
Table sorted = table.sortOn("FirstName", "LastName");

App.java
package com.sample.app;

import tech.tablesaw.api.IntColumn;
import tech.tablesaw.api.StringColumn;
import tech.tablesaw.api.Table;

public class App {

  public static void main(String args[]) {
    int[] empIds = { 1, 2, 3, 4, 5, 6 };
    String[] firstNames = { "Hari", "Ram", "Sowmya", "Chamu", "Swethank", "Chamu" };
    String[] lastNames = { "Krishna", "Gurram", "Maj", "Maj", "Baji", "Gurram" };

    Table table = Table.create().addColumns(IntColumn.create("Employee Id", empIds))
        .addColumns(StringColumn.create("FirstName", firstNames))
        .addColumns(StringColumn.create("LastName", lastNames));

    Table sorted = table.sortOn("FirstName", "LastName");
    
    System.out.println(table.print());
    System.out.println(sorted.print());
    
  }
}

Output
Employee Id  |  FirstName  |  LastName  |
------------------------------------------
           1  |       Hari  |   Krishna  |
           2  |        Ram  |    Gurram  |
           3  |     Sowmya  |       Maj  |
           4  |      Chamu  |       Maj  |
           5  |   Swethank  |      Baji  |
           6  |      Chamu  |    Gurram  |
 Employee Id  |  FirstName  |  LastName  |
------------------------------------------
           6  |      Chamu  |    Gurram  |
           4  |      Chamu  |       Maj  |
           1  |       Hari  |   Krishna  |
           2  |        Ram  |    Gurram  |
           3  |     Sowmya  |       Maj  |
           5  |   Swethank  |      Baji  |


‘sortOn’ method sorts the elements in ascending order. If you want to sort the elements of table in descending order, you can use 'sortDescendingOn' method.

Example
Table sorted = table.sortDescendingOn("FirstName", "LastName");

App.java

package com.sample.app;

import tech.tablesaw.api.IntColumn;
import tech.tablesaw.api.StringColumn;
import tech.tablesaw.api.Table;

public class App {

  public static void main(String args[]) {
    int[] empIds = { 1, 2, 3, 4, 5, 6 };
    String[] firstNames = { "Hari", "Ram", "Sowmya", "Chamu", "Swethank", "Chamu" };
    String[] lastNames = { "Krishna", "Gurram", "Maj", "Maj", "Baji", "Gurram" };

    Table table = Table.create().addColumns(IntColumn.create("Employee Id", empIds))
        .addColumns(StringColumn.create("FirstName", firstNames))
        .addColumns(StringColumn.create("LastName", lastNames));

    Table sorted = table.sortDescendingOn("FirstName", "LastName");
    
    System.out.println(table.print());
    System.out.println("\nAfter Sorting\n");
    System.out.println(sorted.print());
    
  }
}

Output

Employee Id  |  FirstName  |  LastName  |
------------------------------------------
           1  |       Hari  |   Krishna  |
           2  |        Ram  |    Gurram  |
           3  |     Sowmya  |       Maj  |
           4  |      Chamu  |       Maj  |
           5  |   Swethank  |      Baji  |
           6  |      Chamu  |    Gurram  |

After Sorting

 Employee Id  |  FirstName  |  LastName  |
------------------------------------------
           5  |   Swethank  |      Baji  |
           3  |     Sowmya  |       Maj  |
           2  |        Ram  |    Gurram  |
           1  |       Hari  |   Krishna  |
           4  |      Chamu  |       Maj  |
           6  |      Chamu  |    Gurram  |


Previous                                                    Next                                                    Home

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