Added a few code snippets (#9084)

This commit is contained in:
Mona Mohamadinia 2020-04-17 00:17:10 +04:30 committed by GitHub
parent 2815700ebf
commit 6ceff529ad
1 changed files with 30 additions and 0 deletions

View File

@ -0,0 +1,30 @@
package com.baeldung.streams.closure;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.stream.Stream;
/**
* Contains a couple of simple stream API usages.
*/
public class StreamClosureSnippets {
public static void main(String[] args) throws IOException {
// Collection based streams shouldn't be closed
Arrays.asList("Red", "Blue", "Green")
.stream()
.filter(c -> c.length() > 4)
.map(String::toUpperCase)
.forEach(System.out::print);
String[] colors = {"Red", "Blue", "Green"};
Arrays.stream(colors).map(String::toUpperCase).forEach(System.out::println);
// IO-Based Streams Should be Closed via Try with Resources
try (Stream<String> lines = Files.lines(Paths.get("/path/tp/file"))) {
// lines will be closed after exiting the try block
}
}
}