Start learning free

LESSON 599 OF 671 · PROJECTS: COMMAND-LINE TOOLS

cat v1 in Java: Printing Lines with String.join

Practice Read the full explanation

Build a tiny version of the Unix cat command. Given a list of lines, return them joined by newline characters (\n) so they print one per line.

Lines can be blank, and the list can be empty (the result is then an empty string).

Function signature:

public String cat(List<String> lines)

Example:

Input: lines = ["hello", "world", "foo"]

Output: "hello\nworld\nfoo"

When printed:
hello
world
foo

Think of the output as one big String where each input line is separated from the next by a newline character. Java has a static helper that glues all elements of a collection together with a chosen separator.

Call String.join("\n", lines) — the first argument is the separator, the second is the list of lines.

The full solution is a one-liner:
class Solution {
    public String cat(List<String> lines) {
        return String.join("\n", lines);
    }
}

STEP 1 OF 1
    Ctrl + Enter to check

    JAVA · PROJECTS: COMMAND-LINE TOOLS

    Cat V1 in Java, explained

    Problem overview

    This is the first version of a small command-line tool, a miniature of the Unix cat command. The method cat receives a List<String> of lines and returns one String in which the lines are separated by newline characters (\n), so that printing the result shows one line per row.

    For the list ["hello", "world", "foo"] the result is the string hello\nworld\nfoo, which prints as:

    hello
    world
    foo
    

    Blank lines count as lines, and an empty list gives an empty string. The next version builds on this one.

    Approach: join, don't append

    A first idea is a loop that appends each line followed by "\n". That leaves a newline after the last line, which the example doesn't have, so the loop needs a special case for the end (or for the start). The word to look for is separator: a newline goes between two lines, never after the last one. Java's standard library already has a method for exactly that.

    String.join(separator, elements) is a static method that glues the elements together with the separator between each pair. It accepts any Iterable of character sequences, so a List<String> works as it is, with no loop and no special cases.

    Step by step

    1. Return the joined lines. Call String.join("\n", lines) and return the result. The first argument is the separator and the second is the collection to join.

    That is the whole lesson. The method has one statement, because the library does the looping.

    The solution in Java

    class Solution {
        public String cat(List<String> lines) {
            return String.join("\n", lines);
        }
    }
    

    The Java details worth a closer look:

    • The separator is only put between elements. With one element there is nothing to separate, so ["only"] gives only with no newline. With an empty list, there is nothing to join and the result is the empty string.
    • "\n" is a single character. In a Java string literal, \n is an escape sequence for the newline character. The string "hello\nworld" has 11 characters, not 12.
    • Static call. String.join belongs to the String class, not to a particular string, so it is called on the class name.
    • The input is not changed. join builds a new string and leaves the list as it was.

    Complexity

    Time: O(total length). Every character of every line is copied once into the result, plus one separator between each pair of lines.

    Space: O(total length) for the returned string. No other collection is built.

    Edge cases and common mistakes

    • A trailing newline. A loop that appends line + "\n" for every line returns hello\nworld\nfoo\n, which differs from the expected result. String.join avoids this.
    • Blank lines. ["a", "", "b"] gives a\n\nb. The empty string is a real line, so both newlines stay.
    • An empty list against a list with one blank line. Both give the empty string, "". A list of two blank lines, ["", ""], gives a single newline, "\n". Joining only inserts separators, so the result alone can't tell those first two inputs apart.
    • Swapped arguments. The separator comes first. String.join(lines, "\n") is a compile error: no join method accepts a List<String> as its first argument.

    Testing

    The results below come from running the solution:

    • ["hello", "world", "foo"] gives "hello\nworld\nfoo".
    • ["a", "", "b"] gives "a\n\nb".
    • ["only"] gives "only".
    • [] gives "".
    • ["", ""] gives "\n".
    Keep going with Java650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free