Monday 6 April 2020

TableSaw: Retain specific columns of a table

'table.retainColumns' method is used to retain specific columns of a table.

Signature of the methods
public Table retainColumns(String... columnNames)
public Table retainColumns(Column<?>... columns)

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" };
  String[] lastNames = { "Krishna", "Gurram", "Maj", "Dev", "Gurram", "Ram", "Sen", "Grandi", "Neelam" };

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

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

  table.retainColumns("FirstName", "Employee Ids");
  System.out.println("\nRetaining the columns FirstName, Employee Ids\n");

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

 }
}

Output
Employee Ids  |  FirstName  |  LastName  |
-------------------------------------------
            1  |       Hari  |   Krishna  |
            2  |        Ram  |    Gurram  |
            3  |      Chamu  |       Maj  |
            4  |     Sowmya  |       Dev  |
            5  |     Harini  |    Gurram  |
            6  |     Lahari  |       Ram  |
            7  |       Rama  |       Sen  |
            8  |   Lakshman  |    Grandi  |
            9  |     Sandya  |    Neelam  |

Retaining the columns FirstName, Employee Ids

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





Previous                                                    Next                                                    Home

No comments:

Post a Comment