Last active
August 9, 2018 02:53
-
-
Save lq920320/113b332ba9f9ab29fe93cb26d138cd6c to your computer and use it in GitHub Desktop.
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
/** | |
* @author liuqian | |
* @date 2018/8/9 10:33 | |
*/ | |
public class StreamFilterTest { | |
@Test | |
public void listStreamFind() { | |
List<Integer> list = Arrays.asList(1, 10, 3, 7, 5); | |
Integer a = list.stream() | |
.filter(x -> x > 10) | |
.peek(num -> System.out.println("will filter " + num)) | |
.findFirst().orElse(null); | |
System.out.println(a); | |
} | |
} | |
/** | |
* Returns a stream consisting of the elements of this stream, additionally | |
* performing the provided action on each element as elements are consumed | |
* from the resulting stream. | |
* | |
* <p>This is an <a href="package-summary.html#StreamOps">intermediate | |
* operation</a>. | |
* | |
* <p>For parallel stream pipelines, the action may be called at | |
* whatever time and in whatever thread the element is made available by the | |
* upstream operation. If the action modifies shared state, | |
* it is responsible for providing the required synchronization. | |
* | |
* @apiNote This method exists mainly to support debugging, where you want | |
* to see the elements as they flow past a certain point in a pipeline: | |
* <pre>{@code | |
* Stream.of("one", "two", "three", "four") | |
* .filter(e -> e.length() > 3) | |
* .peek(e -> System.out.println("Filtered value: " + e)) | |
* .map(String::toUpperCase) | |
* .peek(e -> System.out.println("Mapped value: " + e)) | |
* .collect(Collectors.toList()); | |
* }</pre> | |
* | |
* <p>In cases where the stream implementation is able to optimize away the | |
* production of some or all the elements (such as with short-circuiting | |
* operations like {@code findFirst}, or in the example described in | |
* {@link #count}), the action will not be invoked for those elements. | |
* | |
* @param action a <a href="package-summary.html#NonInterference"> | |
* non-interfering</a> action to perform on the elements as | |
* they are consumed from the stream | |
* @return the new stream | |
* Stream<T> peek(Consumer<? super T> action); | |
*/ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment