How to Loop and Print 2D array using Java 8

Try this

Stream.of(names).map(Arrays::toString).forEach(System.out::println);

Keeping the same output as your for loops:

Stream.of(names)
    .flatMap(Stream::of)
        .forEach(System.out::println);

(See Stream#flatMap.)

Also something like:

Arrays.stream(names)
    .map(a -> String.join(" ", a))
        .forEach(System.out::println);

Which produces output like:

Sam Smith
Robert Delgro
James Gosling

(See String#join.)

Also:

System.out.println(
    Arrays.stream(names)
        .map(a -> String.join(" ", a))
            .collect(Collectors.joining(", "))
);

Which produces output like:

Sam Smith, Robert Delgro, James Gosling

(See Collectors#joining.)

Joining is one of the less discussed but still wonderful new features of Java 8.


In standard Java

System.out.println(Arrays.deepToString(names));