Friday 10 April 2020

TableSaw: Table: Get columns of desired type

There are two ways to get the column of desired type.
a.   By doing explicit casting
b.   By using the methods that return columns of desired type

By doing explicit casting
DateColumn dateColumn = (DateColumn) table.column(3);

App.java
package com.sample.app;

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

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());

            DateColumn dateColumn = (DateColumn) table.column(3);

            System.out.println("\n\n" + dateColumn.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: Date Of Birth
1988-05-01
1976-08-01
1948-05-11
1968-05-21

b. By using the methods that return columns of desired type
DateColumn dateColumn1 = table.dateColumn(3);
DateColumn dateColumn2 = table.dateColumn("Date Of Birth");

App.java
package com.sample.app;

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

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());

            DateColumn dateColumn1 = table.dateColumn(3);
            DateColumn dateColumn2 = table.dateColumn("Date Of Birth");

            System.out.println("\n\n" + dateColumn1.print());
            System.out.println("\n\n" + dateColumn2.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: Date Of Birth
1988-05-01
1976-08-01
1948-05-11
1968-05-21



Column: Date Of Birth
1988-05-01
1976-08-01
1948-05-11
1968-05-21





Previous                                                    Next                                                    Home

No comments:

Post a Comment