Παράδειγμα: Λέξεις κειμένου

/*
 * Output ordered list of a file's unique words
 */

import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Stream;
import java.io.IOException;

class UniqueWords {
    public static void main(String args[]) {
        if (args.length != 1) {
            System.err.println("Usage: UniqueWords file");
            System.exit(1);
        }

        try {
            Files
                .lines(Paths.get(args[0]))
                .flatMap(line -> Stream.of(line.split("\\W+")))
                .sorted()
                .distinct()
                .filter((x) -> x.length() > 0)
                .forEach(System.out::println);
        } catch (IOException e) {
            System.err.println("Error reading line: " + e.getMessage());
            System.exit(1);
        }
    }
}