Java 8: From a for-loop to forEach statement

Ordinary for loops:


List<String> strings = new ArrayList<>();
for (String string : strings) {
	System.out.println("Content: " + string);
}

...can be easily translated into a forEach statement:

List<String> strings = new ArrayList<>();
strings.stream().forEach((string) -> {
	System.out.println("Content: " + string);
});

With the "functional looping style" any pre- or post-processing like filtering, grouping or even parallelization can be easily achieved:


strings.parallelStream().
        filter(s -> s.contains("java")).
        forEach((string) -> {
            System.out.println("Content: " + string);
        });
}

NetBeans 8 converts ordinary for-loops into the functional notation by hitting the ALT+ENTER key on, or by clicking on the yellow bulb.

See you at Java EE Workshops at MUC Airport or on demand and in a location very near you: airhacks.io!

Comments:

small correction: yellow bulb instead of yellow bulp

Posted by asdf on January 20, 2014 at 07:38 AM CET #

When talking about ordinary for loops, I still call them "new-style forma loops". I feel like an old guy :(

Posted by Danilo Piazzalunga on January 20, 2014 at 10:30 AM CET #

I still refer to ordinary for loops as "new-style for loops", and there already are newer-style loops. I'm looking forward to developing with Java 8 and NetBeans 8!

Posted by Danilo Piazzalunga on January 20, 2014 at 10:40 AM CET #

What actually is the benefit
For "strings.stream().forEach((string) -> {"
over
"for (String string : strings) {"

Posted by Wese on January 25, 2014 at 02:18 AM CET #

in the 'functional style loop' the iteration it's done by forEach statement and you'll focus only to the action of what to do on each element and you don't have to concentrate on how to go through collection, if if I'm not mistaken this function, forEach it's implemented on stream interface so it's applied to all collections.

Posted by sorin on September 19, 2014 at 03:28 PM CEST #

i want more detail about arraylist with custom objects

Posted by 122.165.92.50 on July 08, 2015 at 02:00 PM CEST #

In Netbeans' Graphical user interface, I have two text fields - one for the user to type a number and the other for them to type a larger number. There's also an enter button. When they click the enter button, I want to display on another label all the numbers between the smaller and larger number they entered including the two numbers they entered.

I have used this code to achieve this:

String Strno1 = txtno.getText();

String Strno2 = txtno2.getText();
int intno2 = Integer.parseInt (Strno2);

for (intno1= Integer.parseInt(Strno1); intno1 <= intno2; intno1++)
{
lbloutput.setText (String.valueOf(intno1));
}

But, when the user clicks enter, nothing is displayed at all.

Can you please explain why & what code I can use to solve this problem? Thank you.

Posted by 70.50.79.209 on August 09, 2015 at 07:32 PM CEST #

List<String> strings= list.stream()
.collect(Collectors.toList());

strings.forEach(System.out::println);

Another alternative will be to use the above statements.

Posted by Valleyman on August 10, 2015 at 06:58 PM CEST #

As a direct drop-in replacement for a for statement, forEachOrdered is preferable in some cases to forEach. forEachOrdered more closely mimics the semantics of a deterministic loop.

The javadoc for forEach states: "The behavior of this operation is explicitly nondeterministic. For parallel stream pipelines, this operation does not guarantee to respect the encounter order of the stream, as doing so would sacrifice the benefit of parallelism. For any given element, the action may be performed at whatever time and in whatever thread the library chooses."

The javadoc for forEachOrdered states: "This operation processes the elements one at a time, in encounter order if one exists. Performing the action for one element happens-before performing the action for subsequent elements, but for any given element, the action may be performed in whatever thread the library chooses."

Posted by John Smith on November 04, 2015 at 07:27 PM CET #

nice

Posted by 103.51.55.231 on March 22, 2016 at 09:10 AM CET #

Post a Comment:
  • HTML Syntax: NOT allowed
...the last 150 posts
...the last 10 comments
License