diff --git a/code/ArrayAdd.java b/code/ArrayAdd.java index 5d9fe8b..2144bf8 100644 --- a/code/ArrayAdd.java +++ b/code/ArrayAdd.java @@ -2,7 +2,7 @@ class ArrayAdd { public static void main(String[] args) { - List arr = new ArrayList(); + List arr = new ArrayList<>(); arr.add("first"); arr.add("second"); System.out.println(arr); diff --git a/code/ArrayConcat.java b/code/ArrayConcat.java index e122668..2171a8b 100644 --- a/code/ArrayConcat.java +++ b/code/ArrayConcat.java @@ -4,7 +4,7 @@ class ArrayConcat { public static void main(String[] args) { List arr1 = Arrays.asList(1, 2); List arr2 = Arrays.asList(3, 4); - List concated = new ArrayList(arr1); + List concated = new ArrayList<>(arr1); concated.addAll(arr2); System.out.println(concated); } diff --git a/code/ArrayCount.java b/code/ArrayCount.java index a022033..f4c827d 100644 --- a/code/ArrayCount.java +++ b/code/ArrayCount.java @@ -3,10 +3,6 @@ class ArrayCount { public static void main(String[] args) { List arr = Arrays.asList(1, 11, 111); - int count = 0; - for (int n : arr) - if (n > 10) - count++; - System.out.println(count); + System.out.println(arr.stream().filter(i -> (i > 10)).count()); } } diff --git a/code/ArrayDeleteElement.java b/code/ArrayDeleteElement.java index a952a11..285b1d8 100644 --- a/code/ArrayDeleteElement.java +++ b/code/ArrayDeleteElement.java @@ -2,12 +2,8 @@ class ArrayDeleteElement { public static void main(String[] args) { - List arr = new LinkedList(Arrays.asList("a", "b", "c")); - Iterator iter = arr.iterator(); - while(iter.hasNext()) { - if(iter.next().equalsIgnoreCase("b")) - iter.remove(); - } + List arr = new LinkedList<>(Arrays.asList("a", "b", "c")); + arr.removeIf(str -> str.equalsIgnoreCase("b")); System.out.println(arr); } } diff --git a/code/ArrayFind.java b/code/ArrayFind.java index 90ba3aa..99597f9 100644 --- a/code/ArrayFind.java +++ b/code/ArrayFind.java @@ -3,13 +3,7 @@ class ArrayFind { public static void main(String[] args) { List arr = Arrays.asList(1, 5, 10, 20); - int first = 0; - for (int n : arr) { - if (n % 2 == 0) { - first = n; - break; - } - } + int first = arr.stream().filter(n -> n % 2 == 0).findFirst().get(); System.out.println(first); } } diff --git a/code/ArrayIterate.java b/code/ArrayIterate.java index 109fca0..217ff37 100644 --- a/code/ArrayIterate.java +++ b/code/ArrayIterate.java @@ -1,9 +1,7 @@ -import java.util.*; +import java.util.Arrays; class ArrayIterate { public static void main(String[] args) { - for (int num : Arrays.asList(1, 2)) { - System.out.println(num); - } + Arrays.asList(1, 2).stream().sequential().forEach(System.out::println); } } diff --git a/code/ArrayMap.java b/code/ArrayMap.java index acb4035..bb437e4 100644 --- a/code/ArrayMap.java +++ b/code/ArrayMap.java @@ -1,11 +1,10 @@ import java.util.*; +import java.util.stream.Collectors; class ArrayMap { public static void main(String[] args) { - List arr = Arrays.asList(1, 5, 10, 20); - List mapped = new ArrayList(); - for (int n : arr) - mapped.add(n * 2); + List arr = Arrays.asList(1, 5, 10, 20); + List mapped = arr.stream().map(n -> n * 2).collect(Collectors.toList()); System.out.println(mapped); } } diff --git a/code/ArrayMulti.java b/code/ArrayMulti.java index 9a0f299..7dcc1c6 100644 --- a/code/ArrayMulti.java +++ b/code/ArrayMulti.java @@ -2,7 +2,7 @@ class ArrayMulti { public static void main(String[] args) { - List> arr = new ArrayList>(); + List> arr = new ArrayList<>(); arr.add(Arrays.asList("first", "second")); arr.add(Arrays.asList("third", "forth")); System.out.println(arr.get(1).get(1)); diff --git a/code/ArrayReduce.java b/code/ArrayReduce.java index 5e1ddd4..e4cbec4 100644 --- a/code/ArrayReduce.java +++ b/code/ArrayReduce.java @@ -1,10 +1,8 @@ -import java.util.*; +import java.util.stream.IntStream; class ArrayReduce { public static void main(String[] args) { - int sum = 0; - for (int n : Arrays.asList(1, 2, 3)) - sum += n; + int sum = IntStream.of(1, 2, 3).sum(); System.out.println(sum); } } diff --git a/code/ArraySelect.java b/code/ArraySelect.java index 5d9922b..293de79 100644 --- a/code/ArraySelect.java +++ b/code/ArraySelect.java @@ -1,12 +1,10 @@ import java.util.*; +import java.util.stream.Collectors; class ArraySelect { public static void main(String[] args) { List arr = Arrays.asList(1, 5, 10, 20); - List all = new ArrayList(); - for (int n : arr) - if (n % 2 == 0) - all.add(n); + List all = arr.stream().filter(n -> n % 2 == 0).collect(Collectors.toList()); System.out.println(all); } } diff --git a/code/FileRead.java b/code/FileRead.java index 106e332..7fbc525 100644 --- a/code/FileRead.java +++ b/code/FileRead.java @@ -1,18 +1,12 @@ import java.io.*; +import java.util.stream.Collectors; class FileRead { public static void main(String[] args) throws IOException { String filePath = System.getProperty("user.dir") + "/code/file.txt"; String content; try (BufferedReader br = new BufferedReader(new FileReader(filePath))) { - StringBuilder sb = new StringBuilder(); - String line = br.readLine(); - while (line != null) { - sb.append(line); - sb.append(System.lineSeparator()); - line = br.readLine(); - } - content = sb.toString(); + content = br.lines().collect(Collectors.joining(System.lineSeparator()))+System.lineSeparator(); } System.out.println(content); } diff --git a/code/HashAdd.java b/code/HashAdd.java index bb0d7e1..3ab87a3 100644 --- a/code/HashAdd.java +++ b/code/HashAdd.java @@ -2,7 +2,7 @@ class HashAdd { public static void main(String[] args) { - Map options = new HashMap(); + Map options = new HashMap<>(); options.put("fontSize", "10"); options.put("fontFamily", "Arial"); System.out.println(options); diff --git a/code/LoopFor.java b/code/LoopFor.java index 559a00f..2d521dc 100644 --- a/code/LoopFor.java +++ b/code/LoopFor.java @@ -1,6 +1,7 @@ +import java.util.stream.IntStream; + class LoopFor { public static void main(String[] args) { - for (int i = 1; i <= 3; i++) - System.out.println(i + ". Hi"); + IntStream.range(1, 3+1).sequential().mapToObj(i -> i + ". Hi").forEach(System.out::println); } } diff --git a/code/LoopNext.java b/code/LoopNext.java index 6de270d..616aac3 100644 --- a/code/LoopNext.java +++ b/code/LoopNext.java @@ -1,9 +1,11 @@ +import java.util.stream.IntStream; + class LoopNext { public static void main(String[] args) { - for (int i = 0; i < 3; i++) { - if (i == 1) - continue; - System.out.println((i + 1) + ". Hi"); - } + IntStream.range(0, 3) + .sequential() + .filter(i -> i != 1) + .mapToObj(i -> (i + 1) + ". Hi") + .forEach(System.out::println); } } diff --git a/code/LoopTimes.java b/code/LoopTimes.java index b6cab09..584108a 100644 --- a/code/LoopTimes.java +++ b/code/LoopTimes.java @@ -1,6 +1,7 @@ +import java.util.stream.IntStream; + class LoopTimes { public static void main(String[] args) { - for (int i = 0; i < 3; i++) - System.out.println("Hi"); + IntStream.range(0, 3).sequential().mapToObj(i -> "Hi").forEach(System.out::println); } } diff --git a/code/LoopUntil.java b/code/LoopUntil.java index b5abfc3..f58ee11 100644 --- a/code/LoopUntil.java +++ b/code/LoopUntil.java @@ -1,8 +1,8 @@ +import java.util.stream.IntStream; + class LoopUntil { public static void main(String[] args) { - int i = 0; - while (i != 3) - i++; + int i = IntStream.iterate(0, n -> n + 1).filter(n -> n == 3).findFirst().getAsInt(); System.out.println(i); } } diff --git a/code/OtherRunCommand.java b/code/OtherRunCommand.java index cdcb20d..63eb7bf 100644 --- a/code/OtherRunCommand.java +++ b/code/OtherRunCommand.java @@ -1,17 +1,16 @@ import java.io.*; +import java.util.stream.Collectors; class OtherRunCommand { public static void main(String[] args) throws IOException, InterruptedException { - String result = ""; + final String result; ProcessBuilder ps = new ProcessBuilder("java", "-version"); ps.redirectErrorStream(true); Process pr = ps.start(); - BufferedReader in = new BufferedReader(new InputStreamReader(pr.getInputStream())); - String line; - while ((line = in.readLine()) != null) - result += line + "\n"; - pr.waitFor(); - in.close(); + try (BufferedReader in = new BufferedReader(new InputStreamReader(pr.getInputStream()))) { + result = in.lines().collect(Collectors.joining("\n")); + pr.waitFor(); + } System.out.println(result); } } diff --git a/code/TypeNullTrueFalse.java b/code/TypeNullTrueFalse.java index 1a07159..be944bc 100644 --- a/code/TypeNullTrueFalse.java +++ b/code/TypeNullTrueFalse.java @@ -8,7 +8,7 @@ public static void main(String[] args) { String shouldBeNull = null; System.out.println(shouldBeNull == null); - List arr = new ArrayList(); + List arr = new ArrayList<>(); System.out.println(arr.isEmpty()); } }