This file contains hidden or 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
| import java.util.stream.Collectors; | |
| import java.util.stream.Stream; | |
| public class StreamPeekEx { | |
| public static void main(String a[]) { | |
| Stream.of("bus", "car", "bycle", "flight", "train") | |
| .filter(e -> e.length() > 3) | |
| .peek(e -> System.out.println("Filtered value: " + e)) |
This file contains hidden or 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
| import java.util.stream.Collectors; | |
| import java.util.stream.Stream; | |
| public class StreamPeekEx { | |
| public static void main(String a[]) { | |
| Stream.of("bus", "car", "bycle", "flight", "train") | |
| .filter(e -> e.length() > 3) | |
| .peek(e -> System.out.println("Filtered value: " + e)) |
This file contains hidden or 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
| public static void main(String[] args) { | |
| System.out.println("Stream without terminal operation"); | |
| Arrays.stream(new int[] { 1, 2, 3 }).map(i -> { | |
| System.out.println("doubling " + i); | |
| return i * 2; | |
| }); | |
| System.out.println("Stream with terminal operation"); | |
| Arrays.stream(new int[] { 1, 2, 3 }).map(i -> { |
This file contains hidden or 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
| import java.time.LocalTime; | |
| import java.util.Arrays; | |
| import java.util.stream.Stream; | |
| public class SequentialParallelComparison { | |
| public static void main (String[] args) { | |
| String[] strings = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"}; | |
| System.out.println("-------\nRunning sequential\n-------"); |
This file contains hidden or 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
| targetLongList = | |
| sourceLongList.stream(). | |
| filter(l -> l > 100). | |
| collect(Collectors.toList()); |
This file contains hidden or 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
| @Test | |
| public void testflatMap() throws Exception { | |
| List<Integer> together = Stream.of(asList(1, 2), asList(3, 4)) // Stream of List<Integer> | |
| .flatMap(List::stream) | |
| .map(integer -> integer + 1) | |
| .collect(Collectors.toList()); | |
| assertEquals(asList(2, 3, 4, 5), together); | |
| } |
This file contains hidden or 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
| @Test | |
| public void convertStringToUpperCaseStreams() { | |
| List<String> collected = Stream.of("a", "b", "hello") // Stream of String | |
| .map(String::toUpperCase) // Returns a stream consisting of the results of applying the given function to the elements of this stream. | |
| .collect(Collectors.toList()); | |
| assertEquals(asList("A", "B", "HELLO"), collected); | |
| } |
This file contains hidden or 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
| Stream<String> stringStream = Stream.of("a", "b", "c"); | |
| String[] stringArray = stringStream.toArray(size -> new String[size]); | |
| Arrays.stream(stringArray).forEach(System.out::println); | |
| /*Output | |
| a | |
| b | |
| c | |
| */ |
This file contains hidden or 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
| Map<String, Choice> result = | |
| choices.stream().collect(Collectors.toMap(Choice::getName, | |
| Function.identity())); |
This file contains hidden or 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
| <servlet-mapping> | |
| <servlet-name>dispatcher</servlet-name> | |
| <url-pattern>/*</url-pattern> | |
| </servlet-mapping> |