import ballerina/io;
import ballerina/jsonutils;
import ballerina/xmlutils;
type Employee record {
    int id;
    string name;
    float salary;
};public function main() {
    table<Employee> tbEmployee = table {
        {key id, name, salary},
        [
            {1, "Mary", 300.5},
            {2, "John", 200.5},
            {3, "Jim", 330.5}
        ]
    };
    io:print("Table Information: ");
    io:println(tbEmployee);
    Employee e1 = {id: 1, name: "Jane", salary: 300.50};
    Employee e2 = {id: 2, name: "Anne", salary: 100.50};
    Employee e3 = {id: 3, name: "John", salary: 400.50};
    Employee e4 = {id: 4, name: "Peter", salary: 150.0};
    table<Employee> tb = table {
        {key id, name, salary},
        [
            e1,
            e2
        ]
    };    Employee[] employees = [e3, e4];
    foreach var emp in employees {
        var ret = tb.add(emp);
        if (ret is ()) {
            io:println("Adding record to table successful");
        } else {
            io:println("Adding to table failed: ", ret.reason());
        }
    }
    io:println("Table Information: ", tb);
    io:println("Using foreach:");
    foreach var x in tb {
        io:println("Name: ", x.name);
    }
    io:println("Using while loop:");
    while (tb.hasNext()) {
        var ret = tb.getNext();
        io:println("Name: ", ret.name);
    }
    json retValJson = jsonutils:fromTable(tb);
    io:println("JSON: ", retValJson.toJsonString());
    xml retValXml = xmlutils:fromTable(tb);
    io:println("XML: ", retValXml);
    int|error count = tb.remove(isHigherSalary);
    io:println("Deleted Count: ", count);
    io:println(tb);
}
function isHigherSalary(Employee emp) returns boolean {
    return emp.salary > 300.0;
}# To run this sample, navigate to the directory that contains the
# `.bal` file, and execute the `ballerina run` command below.
ballerina run table.bal
Table Information: id=1 name=Mary salary=300.5 id=2 name=John salary=200.5 id=3 name=Jim salary=330.5
Adding record to table successful
Adding record to table successful
Table Information: id=1 name=Jane salary=300.5 id=2 name=Anne salary=100.5 id=3 name=John salary=400.5 id=4 name=Peter salary=150.0
Using foreach:
Name: Jane
Name: Anne
Name: John
Name: Peter
Using while loop:
Name: Jane
Name: Anne
Name: John
Name: Peter
JSON: [{"id":1, "name":"Jane", "salary":300.5}, {"id":2, "name":"Anne", "salary":100.5}, {"id":3, "name":"John", "salary":400.5}, {"id":4, "name":"Peter", "salary":150.0}]
XML: <results><result><id>1</id><name>Jane</name><salary>300.5</salary></result><result><id>2</id><name>Anne</name><salary>100.5</salary></result><result><id>3</id><name>John</name><salary>400.5</salary></result><result><id>4</id><name>Peter</name><salary>150.0</salary></result></results>
Deleted Count: 2
id=2 name=Anne salary=100.5 id=4 name=Peter salary=150.0

Table

The type table is a data structure that organizes information in rows and columns. This example demonstrates how to create an in-memory table using a type constraint, insert data to it, and then access/delete the data.

import ballerina/io;
import ballerina/jsonutils;
import ballerina/xmlutils;
type Employee record {
    int id;
    string name;
    float salary;
};

This is the type created to represent a data row.

public function main() {
    table<Employee> tbEmployee = table {
        {key id, name, salary},
        [
            {1, "Mary", 300.5},
            {2, "John", 200.5},
            {3, "Jim", 330.5}
        ]
    };

This creates an in-memory table constrained by the Employee type with the id marked as the primary key in the column descriptor. Three data records are inserted into the table. The order of the data values should match the order of the column descriptor.

    io:print("Table Information: ");
    io:println(tbEmployee);

Print the table data.

    Employee e1 = {id: 1, name: "Jane", salary: 300.50};
    Employee e2 = {id: 2, name: "Anne", salary: 100.50};
    Employee e3 = {id: 3, name: "John", salary: 400.50};
    Employee e4 = {id: 4, name: "Peter", salary: 150.0};

Create Employee records.

    table<Employee> tb = table {
        {key id, name, salary},
        [
            e1,
            e2
        ]
    };

Create an in-memory table constrained by the Employee type with the id as the primary key. Two records are inserted into the table.

    Employee[] employees = [e3, e4];
    foreach var emp in employees {
        var ret = tb.add(emp);
        if (ret is ()) {
            io:println("Adding record to table successful");
        } else {
            io:println("Adding to table failed: ", ret.reason());
        }
    }

Add the created records to the table.

    io:println("Table Information: ", tb);

Print the table data.

    io:println("Using foreach:");
    foreach var x in tb {
        io:println("Name: ", x.name);
    }

Access the rows using the foreach loop.

    io:println("Using while loop:");
    while (tb.hasNext()) {
        var ret = tb.getNext();
        io:println("Name: ", ret.name);
    }

Access rows using the while loop.

    json retValJson = jsonutils:fromTable(tb);
    io:println("JSON: ", retValJson.toJsonString());

Convert the table to JSON format.

    xml retValXml = xmlutils:fromTable(tb);
    io:println("XML: ", retValXml);

Convert the table to XML format.

    int|error count = tb.remove(isHigherSalary);
    io:println("Deleted Count: ", count);

Remove employees with salaries higher than 300.0 from the table.

    io:println(tb);
}

Now the table contains the employees with salaries less than 300.0.

function isHigherSalary(Employee emp) returns boolean {
    return emp.salary > 300.0;
}

Check whether a given employee’s salary is higher than 300.0.

# To run this sample, navigate to the directory that contains the
# `.bal` file, and execute the `ballerina run` command below.
ballerina run table.bal
Table Information: id=1 name=Mary salary=300.5 id=2 name=John salary=200.5 id=3 name=Jim salary=330.5
Adding record to table successful
Adding record to table successful
Table Information: id=1 name=Jane salary=300.5 id=2 name=Anne salary=100.5 id=3 name=John salary=400.5 id=4 name=Peter salary=150.0
Using foreach:
Name: Jane
Name: Anne
Name: John
Name: Peter
Using while loop:
Name: Jane
Name: Anne
Name: John
Name: Peter
JSON: [{"id":1, "name":"Jane", "salary":300.5}, {"id":2, "name":"Anne", "salary":100.5}, {"id":3, "name":"John", "salary":400.5}, {"id":4, "name":"Peter", "salary":150.0}]
XML: <results><result><id>1</id><name>Jane</name><salary>300.5</salary></result><result><id>2</id><name>Anne</name><salary>100.5</salary></result><result><id>3</id><name>John</name><salary>400.5</salary></result><result><id>4</id><name>Peter</name><salary>150.0</salary></result></results>
Deleted Count: 2
id=2 name=Anne salary=100.5 id=4 name=Peter salary=150.0