DEV Community

loading...

Discussion on: For Loop in different programming languages

Collapse
everton profile image
Everton Agner • Edited

Let's be fair with Java. The language has seen quite a few enhancements since Java 8 release, 3 years ago, and very recently on Java 9, both on API and syntax. Now using the new stuff...

Loop a string:

String str = "hello";
str.chars() // Produces a stream of integers... yeah, that's odd
    .mapToObj(i -> (char) i) // Now we have a stream of chars
    .forEach(System.out::println);

Loop a collection:

List<String> list = List.of("a", "b");
list.forEach(System.out::println);