Sunday 12 April 2020

TableSaw: Print table contents using for-each loop

Below snippet prints table contents row wise using for-each loop.

for (Row row : table) {
  System.out.println(row);
}

App.java
package com.sample.app;

import tech.tablesaw.api.IntColumn;
import tech.tablesaw.api.Row;
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 };
    String[] firstNames = { "Hari", "Ram", "Sowmya", "Chamu", "Hareesh" };
    String[] lastNames = { "Krishna", "Gurram", "Maj", "Dev", "Baji" };

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

    for (Row row : table) {
       System.out.println(row);
    }
  }
}

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



Previous                                                    Next                                                    Home

No comments:

Post a Comment