| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -7,6 +7,7 @@ | |||
| 7 | 7 | import java.nio.file.Path; | |
| 8 | 8 | import java.nio.file.Paths; | |
| 9 | 9 | import java.util.List; | |
| 10 | + import java.util.stream.Collectors; | ||
| 10 | 11 | import java.util.stream.Stream; | |
| 11 | 12 | ||
| 12 | 13 | /** | |
@@ -26,25 +27,26 @@ public static void main(String[] args) throws IOException { | |||
| 26 | 27 | } | |
| 27 | 28 | ||
| 28 | 29 | private static void testReaderLines() throws IOException { | |
| 29 | - try (BufferedReader reader = | ||
| 30 | - Files.newBufferedReader(Paths.get("res", "nashorn1.js"))) { | ||
| 31 | - long countPrints = reader.lines() | ||
| 30 | + Path path = Paths.get("res/nashorn1.js"); | ||
| 31 | + try (BufferedReader reader = Files.newBufferedReader(path)) { | ||
| 32 | + long countPrints = reader | ||
| 33 | + .lines() | ||
| 32 | 34 | .filter(line -> line.contains("print")) | |
| 33 | 35 | .count(); | |
| 34 | 36 | System.out.println(countPrints); | |
| 35 | 37 | } | |
| 36 | 38 | } | |
| 37 | 39 | ||
| 38 | 40 | private static void testWriter() throws IOException { | |
| 39 | - try (BufferedWriter writer = | ||
| 40 | - Files.newBufferedWriter(Paths.get("res", "output.js"))) { | ||
| 41 | + Path path = Paths.get("res/output.js"); | ||
| 42 | + try (BufferedWriter writer = Files.newBufferedWriter(path)) { | ||
| 41 | 43 | writer.write("print('Hello World');"); | |
| 42 | 44 | } | |
| 43 | 45 | } | |
| 44 | 46 | ||
| 45 | 47 | private static void testReader() throws IOException { | |
| 46 | - try (BufferedReader reader = | ||
| 47 | - Files.newBufferedReader(Paths.get("res", "nashorn1.js"))) { | ||
| 48 | + Path path = Paths.get("res/nashorn1.js"); | ||
| 49 | + try (BufferedReader reader = Files.newBufferedReader(path)) { | ||
| 48 | 50 | System.out.println(reader.readLine()); | |
| 49 | 51 | } | |
| 50 | 52 | } | |
@@ -53,10 +55,11 @@ private static void testWalk() throws IOException { | |||
| 53 | 55 | Path start = Paths.get(""); | |
| 54 | 56 | int maxDepth = 5; | |
| 55 | 57 | try (Stream<Path> stream = Files.walk(start, maxDepth)) { | |
| 56 | - long fileCount = stream | ||
| 57 | - .filter(path -> String.valueOf(path).endsWith(".js")) | ||
| 58 | - .count(); | ||
| 59 | - System.out.format("JS files found: %s", fileCount); | ||
| 58 | + String joined = stream | ||
| 59 | + .map(String::valueOf) | ||
| 60 | + .filter(path -> path.endsWith(".js")) | ||
| 61 | + .collect(Collectors.joining("; ")); | ||
| 62 | + System.out.println("walk(): " + joined); | ||
| 60 | 63 | } | |
| 61 | 64 | } | |
| 62 | 65 | ||
@@ -65,26 +68,36 @@ private static void testFind() throws IOException { | |||
| 65 | 68 | int maxDepth = 5; | |
| 66 | 69 | try (Stream<Path> stream = Files.find(start, maxDepth, (path, attr) -> | |
| 67 | 70 | String.valueOf(path).endsWith(".js"))) { | |
| 68 | - stream.sorted().forEach(System.out::println); | ||
| 71 | + String joined = stream | ||
| 72 | + .sorted() | ||
| 73 | + .map(String::valueOf) | ||
| 74 | + .collect(Collectors.joining("; ")); | ||
| 75 | + System.out.println("find(): " + joined); | ||
| 69 | 76 | } | |
| 70 | 77 | } | |
| 71 | 78 | ||
| 72 | 79 | private static void testList() throws IOException { | |
| 73 | - try (Stream<Path> stream = Files.list(Paths.get("/usr"))) { | ||
| 74 | - stream.sorted().forEach(System.out::println); | ||
| 80 | + try (Stream<Path> stream = Files.list(Paths.get(""))) { | ||
| 81 | + String joined = stream | ||
| 82 | + .map(String::valueOf) | ||
| 83 | + .filter(path -> !path.startsWith(".")) | ||
| 84 | + .sorted() | ||
| 85 | + .collect(Collectors.joining("; ")); | ||
| 86 | + System.out.println("list(): " + joined); | ||
| 75 | 87 | } | |
| 76 | 88 | } | |
| 77 | 89 | ||
| 78 | 90 | private static void testLines() throws IOException { | |
| 79 | - try (Stream<String> stream = Files.lines(Paths.get("res", "nashorn1.js"))) { | ||
| 91 | + try (Stream<String> stream = Files.lines(Paths.get("res/nashorn1.js"))) { | ||
| 80 | 92 | stream | |
| 81 | 93 | .filter(line -> line.contains("print")) | |
| 94 | + .map(String::trim) | ||
| 82 | 95 | .forEach(System.out::println); | |
| 83 | 96 | } | |
| 84 | 97 | } | |
| 85 | 98 | ||
| 86 | 99 | private static void testReadWriteLines() throws IOException { | |
| 87 | - List<String> lines = Files.readAllLines(Paths.get("res", "nashorn1.js")); | ||
| 100 | + List<String> lines = Files.readAllLines(Paths.get("res/nashorn1.js")); | ||
| 88 | 101 | lines.add("print('foobar');"); | |
| 89 | 102 | Files.write(Paths.get("res", "nashorn1-modified.js"), lines); | |
| 90 | 103 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -30,28 +30,29 @@ private static void testUnsignedInt() { | |||
| 30 | 30 | System.out.println(string2); | |
| 31 | 31 | ||
| 32 | 32 | try { | |
| 33 | - System.out.println(Integer.parseInt(string, 10)); | ||
| 33 | + Integer.parseInt(string, 10); | ||
| 34 | 34 | } | |
| 35 | 35 | catch (NumberFormatException e) { | |
| 36 | - System.out.println("could not parse signed int of " + maxUnsignedInt); | ||
| 36 | + System.err.println("could not parse signed int of " + maxUnsignedInt); | ||
| 37 | 37 | } | |
| 38 | 38 | } | |
| 39 | 39 | ||
| 40 | 40 | private static void testMathExact() { | |
| 41 | + System.out.println(Integer.MAX_VALUE); | ||
| 41 | 42 | System.out.println(Integer.MAX_VALUE + 1); | |
| 42 | 43 | ||
| 43 | 44 | try { | |
| 44 | 45 | Math.addExact(Integer.MAX_VALUE, 1); | |
| 45 | 46 | } | |
| 46 | 47 | catch (ArithmeticException e) { | |
| 47 | - System.out.println(e.getMessage()); | ||
| 48 | + System.err.println(e.getMessage()); | ||
| 48 | 49 | } | |
| 49 | 50 | ||
| 50 | 51 | try { | |
| 51 | 52 | Math.toIntExact(Long.MAX_VALUE); | |
| 52 | 53 | } | |
| 53 | - catch (Exception e) { | ||
| 54 | - System.out.println(e.getMessage()); | ||
| 54 | + catch (ArithmeticException e) { | ||
| 55 | + System.err.println(e.getMessage()); | ||
| 55 | 56 | } | |
| 56 | 57 | } | |
| 57 | 58 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,7 +1,8 @@ | |||
| 1 | 1 | package com.winterbe.java8.samples.misc; | |
| 2 | 2 | ||
| 3 | - import java.util.function.Predicate; | ||
| 4 | 3 | import java.util.regex.Pattern; | |
| 4 | + import java.util.stream.Collectors; | ||
| 5 | + import java.util.stream.Stream; | ||
| 5 | 6 | ||
| 6 | 7 | /** | |
| 7 | 8 | * @author Benjamin Winterberg | |
@@ -16,29 +17,33 @@ public static void main(String[] args) { | |||
| 16 | 17 | } | |
| 17 | 18 | ||
| 18 | 19 | private static void testChars() { | |
| 19 | - String string = "foobar"; | ||
| 20 | - string.chars() | ||
| 21 | - .filter(c -> c > 100) | ||
| 22 | - .mapToObj(c -> (char)c) | ||
| 23 | - .forEach(System.out::println); | ||
| 20 | + String string = "foobar:foo:bar" | ||
| 21 | + .chars() | ||
| 22 | + .distinct() | ||
| 23 | + .mapToObj(c -> String.valueOf((char) c)) | ||
| 24 | + .sorted() | ||
| 25 | + .collect(Collectors.joining()); | ||
| 26 | + System.out.println(string); | ||
| 24 | 27 | } | |
| 25 | 28 | ||
| 26 | 29 | private static void testPatternSplit() { | |
| 27 | - Pattern.compile(":") | ||
| 30 | + String string = Pattern.compile(":") | ||
| 28 | 31 | .splitAsStream("foobar:foo:bar") | |
| 32 | + .filter(s -> s.contains("bar")) | ||
| 29 | 33 | .sorted() | |
| 30 | - .forEach(System.out::println); | ||
| 34 | + .collect(Collectors.joining(":")); | ||
| 35 | + System.out.println(string); | ||
| 31 | 36 | } | |
| 32 | 37 | ||
| 33 | 38 | private static void testPatternPredicate() { | |
| 34 | - Pattern pattern = Pattern.compile(".*123.*"); | ||
| 35 | - Predicate<String> predicate = pattern.asPredicate(); | ||
| 36 | - System.out.println(predicate.test("a123b")); | ||
| 37 | - System.out.println(predicate.test("boom")); | ||
| 39 | + long count = Stream.of("bob@gmail.com", "alice@hotmail.com") | ||
| 40 | + .filter(Pattern.compile(".*@gmail\\.com").asPredicate()) | ||
| 41 | + .count(); | ||
| 42 | + System.out.println(count); | ||
| 38 | 43 | } | |
| 39 | 44 | ||
| 40 | 45 | private static void testJoin() { | |
| 41 | - String string = String.join(";", "a", "b", "c", "d"); | ||
| 46 | + String string = String.join(":", "foobar", "foo", "bar"); | ||
| 42 | 47 | System.out.println(string); | |
| 43 | 48 | } | |
| 44 | 49 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments