lundi 16 mars 2020

Java 8 Streams API functions

I am new to Java 8 Streams API. I was working with forEach and filter and noticed that the following piece of code can be modified with either to produce the same result. In that case, how are the two different and how to decide when to use which?

items.forEach(item -> {
    if (item.contains("B") {
        System.out.println(item);
    }
});
items
    .filter(item -> item.contains("B"))
    .forEach(System.out::println);

Aucun commentaire:

Enregistrer un commentaire