Wednesday 1 April 2020

TableSaw: Table

Table is a collection of columns. All columns in the table must have same number of values.

How to create a table?
You can create a table using 'Table.create' method

How to add columns to a table?
Using 'addColumns' method, you can add

Example
Table table = Table.create().addColumns(IntColumn.create("Number", numbers)).addColumns(IntColumn.create("Number * 3", result));

App.java
package com.sample.app;

import tech.tablesaw.api.IntColumn;

public class App {

 public static void main(String args[]) {
  int[] numbers = { 23, 45, 6, 32, 1, 43, 1, 45, 6, 1, 1 };

  IntColumn numbersColumn = IntColumn.create("My Numbers", numbers);

  double standardDeviation = numbersColumn.standardDeviation();

  System.out.println("standardDeviation : " + standardDeviation);
 }
}

Output
Number  |  Number * 3  |
-------------------------
      1  |           3  |
      2  |           6  |
      3  |           9  |
      4  |          12  |
      5  |          15  |
      6  |          18  |
      7  |          21  |
      8  |          24  |
      9  |          27  |

Table can contain any combination of column types.

Example
Table table = Table.create().addColumns(IntColumn.create("Employee Ids", empIds)).addColumns(StringColumn.create("FirstName", firstNames));

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, 7, 8, 9 };
  String[] firstNames = { "Hari", "Ram", "Chamu", "Sowmya", "Harini", "Lahari", "Rama", "Lakshman", "Sandya" };


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

  System.out.println(table.print());
 }
}

Output

Employee Ids  |  FirstName  |
------------------------------
            1  |       Hari  |
            2  |        Ram  |
            3  |      Chamu  |
            4  |     Sowmya  |
            5  |     Harini  |
            6  |     Lahari  |
            7  |       Rama  |
            8  |   Lakshman  |
            9  |     Sandya  |



Previous                                                    Next                                                    Home

No comments:

Post a Comment