Kohei TAMURA
Kohei TAMURA

Reputation: 5122

Java loop expressions to print list

Could you tell me if there are other loop expressions which Java programmer must be able to read?

final List<String> names = Arrays.asList("Alice", "Bob", "Carol", "Dave", "Eve");

System.out.println("Pattern 1:");
for (int i = 0; i < names.size(); i++) {
    System.out.println(names.get(i));
}

System.out.println("Pattern 2:");
Iterator<String> iterator = names.iterator();
while (iterator.hasNext()) {
    System.out.println(iterator.next());
}

System.out.println("Pattern 3:");
for (String name : names) {
    System.out.println(name);
}

System.out.println("Pattern 4:");
names.forEach(new Consumer<String>() {
    @Override
    public void accept(String name) {
        System.out.println(name);
    }
});

System.out.println("Pattern 5:");
names.forEach((String name) -> {System.out.println(name);});

System.out.println("Pattern 6:");
names.forEach((String name) -> System.out.println(name));

System.out.println("Pattern 7:");
names.forEach(name -> System.out.println(name));

System.out.println("Pattern 8:");
names.forEach(System.out::println);

System.out.println("Pattern 9:");
names.stream().forEach(System.out::println);

Upvotes: 0

Views: 476

Answers (4)

DodgyCodeException
DodgyCodeException

Reputation: 6133

Some more possibilities:

System.out.println("Pattern 12:");
IntStream.range(0, names.size())
        .mapToObj(i -> names.get(i))
        .forEach(System.out::println);

System.out.println("Pattern 13:");
Stream.generate(names.iterator()::next)
        .limit(names.size())
        .forEach(System.out::println);

But, rather than simply listing all of the looping patterns you can think of, it would be more useful to categorize them and think about the use cases for each one. As a start, I suggest these categories:

  1. Indexed loops, e.g. patterns 1, 12

    • use when you need an index (obviously)
    • also useful when you want to iterate through more than one loop at a time
    • you can also have additional terminating conditions
  2. Non-indexed, breakable loops, e.g. patterns 2, 3

    • more readable than indexed loops when you don't need an index
    • use when you need other terminating conditions (e.g. when searching for a specific value in a list)
  3. Non-indexed, non-breakable loops, e.g. patterns 4, 9, 13:

    • can be the most readable kind in certain cases (but certainly not all)
    • use when you readily have a lambda or method reference and you don't need an index, don't need to process more than one collection together, and don't need to break out early
    • in Java 11, (well, to be precise, from Java 9 onwards, but who on earth still uses Java 9 nowadays), patterns 9 and 13 fall into the breakable category using the new takeWhile method.

Upvotes: 1

Cwrwhaf
Cwrwhaf

Reputation: 324

You can use an iterator in a for loop:

for(Iterator<String> nameIterator = names.iterator(); nameIterator.hasNext(); ) {
    System.out.println(nameIterator.next());
}

Upvotes: 1

YouKnowWhoIAm
YouKnowWhoIAm

Reputation: 344

Also we have,

names.stream().forEachOrdered(System.out::println);

forEachOrdered should be used instead of forEach because the behaviour of forEach is non-deterministic, but forEachOrdered performs the operations in the order defined in the stream, provided if you have defined an order in the stream

Also also, you have parallelStream, which can print the list irrespective of order,

names.parallelStream().forEach(System.out::println);

Alternatively you can also do, removing the first element after printing the list, you can do the same with iterators too, using iterator.previous method too.

while(!names.empty()){
    SOP(names.get(0));
    names = names.subList(1,names.size());
}

Upvotes: 2

Scary Wombat
Scary Wombat

Reputation: 44854

Java 11

System.out.println("Pattern 11:");
names.forEach((var name) -> System.out.println(name));

Upvotes: 1

Related Questions