Fundamentals 5 min read

ConsoleTable: A Simple Java Utility for Formatted Console Grid Output

This article introduces a lightweight Java class, ConsoleTable, that formats map and list data into a neatly aligned console grid resembling SQL client output, explains its usage, provides full source code, and discusses stream-based implementations and potential enhancements.

FunTester
FunTester
FunTester
ConsoleTable: A Simple Java Utility for Formatted Console Grid Output

When testing, developers often need to print information to the console, but the default format can be messy. To achieve a tidy, SQL‑client‑like display, the author created a simple Java class called ConsoleTable that renders data in a grid.

Usage currently supports Map and List<List<String>> data structures, though header functionality is not yet provided. Example code demonstrates creating sample lists and maps, then calling show(sss) , show(rows) , and show(json) to display the formatted tables.

Below is the demonstration output (image omitted) followed by the full source code of the ConsoleTable class:

public static void main(String[] args) {
        List
ss0 = Arrays.asList("234", "432", "54");
        List
ss3 = Arrays.asList("234", "432", "54", "54", "54");
        List
ss1 = Arrays.asList("6546", "7675");
        Map
sss = new HashMap<>();
        sss.put(getNanoMark() + EMPTY, "fdsf");
        sss.put(getNanoMark() + EMPTY, "fdsfdsaff");
        sss.put(getNanoMark() + EMPTY, "fdsf");
        sss.put(getNanoMark() + EMPTY, "fdsfafdsf");
        sss.put(getNanoMark() + EMPTY, "fdsf");
        sss.put(getMark() + EMPTY, "fdsf");
        show(sss);
        List
> rows = Arrays.asList(ss1, ss3, ss0);
        show(rows);
        JSONObject json = new JSONObject();
        json.put("3234", 32432);
        json.put("323dsa4", 32432);
        json.put("3fdsa234", 32432);
        json.put("323fdsf4", 32432);
        json.put("32d34", 32432);
        json.put("32fdsafdf34", 32432);
        show(json);
    }

The class defines static show methods for Map and List<List<String>> , which instantiate a new ConsoleTable . The constructor calculates column widths using Java streams, builds a string buffer with borders, and outputs the formatted table.

Key methods include:

getCel(int column, String content) – builds a cell with proper padding.

getHeader() – generates the top and bottom border lines based on column widths.

The implementation leverages Java 8 stream operations for calculating maximum string lengths, demonstrating concise and powerful data processing. The author notes that while stream syntax can be unfamiliar, it greatly improves coding efficiency, though some compatibility issues arise when using streams in Groovy.

Future improvements mentioned are adding support for additional data types, header rows, and sidebars.

JavaCLIUtilityStreamformattingconsoletable
FunTester
Written by

FunTester

10k followers, 1k articles | completely useless

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.