From ad33d2247fa105945f3c54d398f66742f86689f8 Mon Sep 17 00:00:00 2001 From: Bruce Eckel Date: Sat, 21 Jan 2017 11:39:19 -0800 Subject: [PATCH] Code review/streamification --- references/AddingClone.java | 15 ++++++++------- references/CloneArrayList.java | 11 ++++++----- references/CopyConstructor.java | 2 +- references/ImmutableInteger.java | 11 ++++++++--- references/MutableInteger.java | 10 +++++----- references/SimplerMutableInteger.java | 18 +++++++++--------- references/Snake.java | 3 ++- 7 files changed, 39 insertions(+), 31 deletions(-) diff --git a/references/AddingClone.java b/references/AddingClone.java index f0fab39d..d9972c95 100644 --- a/references/AddingClone.java +++ b/references/AddingClone.java @@ -5,6 +5,7 @@ // You must go through a few gyrations // to add cloning to your own class import java.util.*; +import java.util.stream.*; class Int2 implements Cloneable { private int i; @@ -41,18 +42,18 @@ public class AddingClone { // Anything inherited is also cloneable: Int3 x3 = new Int3(7); x3 = (Int3)x3.clone(); - ArrayList v = new ArrayList<>(); - for(int i = 0; i < 10; i++ ) - v.add(new Int2(i)); + ArrayList v = IntStream.range(0, 10) + .mapToObj(Int2::new) + .collect(Collectors + .toCollection(ArrayList::new)); System.out.println("v: " + v); ArrayList v2 = (ArrayList)v.clone(); // Now clone each element: - for(int i = 0; i < v.size(); i++) - v2.set(i, v2.get(i).clone()); + IntStream.range(0, v.size()) + .forEach(i -> v2.set(i, v.get(i).clone())); // Increment all v2's elements: - for(Int2 i2 : v2) - i2.increment(); + v2.forEach(Int2::increment); System.out.println("v2: " + v2); // See if it changed v's elements: System.out.println("v: " + v); diff --git a/references/CloneArrayList.java b/references/CloneArrayList.java index 9dbe7f4c..5135a373 100644 --- a/references/CloneArrayList.java +++ b/references/CloneArrayList.java @@ -5,6 +5,7 @@ // The clone() operation works for only a few // items in the standard Java library import java.util.*; +import java.util.stream.*; class Int { private int i; @@ -18,15 +19,15 @@ class Int { public class CloneArrayList { public static void main(String[] args) { - ArrayList v = new ArrayList<>(); - for(int i = 0; i < 10; i++) - v.add(new Int(i)); + ArrayList v = IntStream.range(0, 10) + .mapToObj(Int::new) + .collect(Collectors + .toCollection(ArrayList::new)); System.out.println("v: " + v); @SuppressWarnings("unchecked") ArrayList v2 = (ArrayList)v.clone(); // Increment all v2's elements: - for(Int e : v2) - e.increment(); + v2.forEach(Int::increment); // See if it changed v's elements: System.out.println("v: " + v); } diff --git a/references/CopyConstructor.java b/references/CopyConstructor.java index f6528738..edaeefd2 100644 --- a/references/CopyConstructor.java +++ b/references/CopyConstructor.java @@ -117,7 +117,7 @@ public class CopyConstructor { t.getClass().getName()); } public static void slice(Fruit f) { - f = new Fruit(f); // Hmmm... will this work? (2) + f = new Fruit(f); // [2] Hmmm... will this work? System.out.println("In slice, f is a " + f.getClass().getName()); } diff --git a/references/ImmutableInteger.java b/references/ImmutableInteger.java index 06243267..3dc86a28 100644 --- a/references/ImmutableInteger.java +++ b/references/ImmutableInteger.java @@ -4,13 +4,18 @@ // Visit http://OnJava8.com for more book information. // The Integer class cannot be changed import java.util.*; +import java.util.stream.*; public class ImmutableInteger { public static void main(String[] args) { - List v = new ArrayList<>(); - for(int i = 0; i < 10; i++) - v.add(Integer.valueOf(i)); + List v = IntStream.range(0, 10) + .mapToObj(Integer::new) + .collect(Collectors.toList()); + System.out.println(v); // But how do you change the int // inside the Integer? } } +/* Output: +[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] +*/ diff --git a/references/MutableInteger.java b/references/MutableInteger.java index 8958af5e..30c99e0c 100644 --- a/references/MutableInteger.java +++ b/references/MutableInteger.java @@ -4,6 +4,7 @@ // Visit http://OnJava8.com for more book information. // A changeable wrapper class import java.util.*; +import java.util.stream.*; class IntValue { private int n; @@ -19,12 +20,11 @@ class IntValue { public class MutableInteger { public static void main(String[] args) { - List v = new ArrayList<>(); - for(int i = 0; i < 10; i++) - v.add(new IntValue(i)); + List v = IntStream.range(0, 10) + .mapToObj(IntValue::new) + .collect(Collectors.toList()); System.out.println(v); - for(int i = 0; i < v.size(); i++) - v.get(i).increment(); + v.forEach(IntValue::increment); System.out.println(v); } } diff --git a/references/SimplerMutableInteger.java b/references/SimplerMutableInteger.java index 3db01005..e0f7d24c 100644 --- a/references/SimplerMutableInteger.java +++ b/references/SimplerMutableInteger.java @@ -4,6 +4,7 @@ // Visit http://OnJava8.com for more book information. // A trivial wrapper class import java.util.*; +import java.util.stream.*; class IntValue2 { public int n; @@ -12,16 +13,15 @@ class IntValue2 { public class SimplerMutableInteger { public static void main(String[] args) { - List v = new ArrayList<>(); - for(int i = 0; i < 10; i++) - v.add(new IntValue2(i)); - for(IntValue2 i : v) - System.out.print(i.n + " "); + List v = IntStream.range(0, 10) + .mapToObj(IntValue2::new) + .collect(Collectors.toList()); + v.forEach(iv2 -> + System.out.print(iv2.n + " ")); System.out.println(); - for(int i = 0; i < v.size(); i++) - v.get(i).n = v.get(i).n + 1; - for(IntValue2 i : v) - System.out.print(i.n + " "); + v.forEach(iv2 -> iv2.n += 1); + v.forEach(iv2 -> + System.out.print(iv2.n + " ")); } } /* Output: diff --git a/references/Snake.java b/references/Snake.java index 199d1d8f..62485549 100644 --- a/references/Snake.java +++ b/references/Snake.java @@ -40,7 +40,8 @@ public class Snake implements Cloneable { Snake s2 = s.clone(); System.out.println("s2 = " + s2); s.increment(); - System.out.println("after s.increment, s2 = " + s2); + System.out.println( + "after s.increment, s2 = " + s2); } } /* Output: