Created
October 25, 2014 13:21
-
-
Save takei-shg/e3503795e587ddc279ed to your computer and use it in GitHub Desktop.
java8 stream
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.company; | |
import java.util.Arrays; | |
import java.util.List; | |
import java.util.stream.Stream; | |
public class Main { | |
public static void main(String[] args) { | |
// write your code here | |
List<Integer> list = Arrays.asList(1,2,3,4,5); | |
Stream<Integer> stream1 = list.stream(); | |
Integer ab = stream1.reduce(0, (a,b) -> a + b); | |
System.out.println(ab); | |
// can not use stream1, because stream1 has already consumed. | |
Stream<Integer> stream2 = list.stream(); | |
Stream<String> s2 = stream2.map(x -> x.toString()); | |
s2.forEach(System.out::println); | |
// peek can show consumed elements | |
Stream<Integer> stream3 = list.stream(); | |
stream3.filter(x -> x < 3) | |
.peek(e -> {System.out.println(e);}) | |
.forEach(System.out::println); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment