Friday 10 April 2020

TableSaw: Get all the columns of specific type

‘table.columnsOfType’ method returns all the columns of specific type.

Example
table.columnsOfType(ColumnType.STRING).forEach(column -> System.out.println(column.print()));

Above statement print all the columns of type string.

App.java
package com.sample.app;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;

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

            List<LocalDate> dateOfBirths = Arrays.asList(LocalDate.of(1988, 5, 1), LocalDate.of(1976, 8, 1),
                        LocalDate.of(1948, 5, 11), LocalDate.of(1968, 5, 21));

            Table table = Table.create().addColumns(IntColumn.create("Employee Ids", empIds))
                        .addColumns(StringColumn.create("FirstName", firstNames))
                        .addColumns(StringColumn.create("LastName", lastNames))
                        .addColumns(DateColumn.create("Date Of Birth", dateOfBirths));

            System.out.println(table.print() + "\n");

            table.columnsOfType(ColumnType.STRING).forEach(column -> System.out.println(column.print()));

      }
}

Output
Employee Ids  |  FirstName  |  LastName  |  Date Of Birth  |
-------------------------------------------------------------
            1  |       Hari  |   Krishna  |     1988-05-01  |
            2  |        Ram  |    Gurram  |     1976-08-01  |
            3  |     Sowmya  |       Maj  |     1948-05-11  |
            4  |      Chamu  |       Dev  |     1968-05-21  |

Column: FirstName
Hari
Ram
Sowmya
Chamu

Column: LastName
Krishna
Gurram
Maj
Dev




Previous                                                    Next                                                    Home

No comments:

Post a Comment