Code review/streamification

This commit is contained in:
Bruce Eckel 2017-01-21 11:39:19 -08:00
parent f3ef6ec758
commit ad33d2247f
7 changed files with 39 additions and 31 deletions

View File

@ -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<Int2> v = new ArrayList<>();
for(int i = 0; i < 10; i++ )
v.add(new Int2(i));
ArrayList<Int2> v = IntStream.range(0, 10)
.mapToObj(Int2::new)
.collect(Collectors
.toCollection(ArrayList::new));
System.out.println("v: " + v);
ArrayList<Int2> v2 =
(ArrayList<Int2>)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);

View File

@ -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<Int> v = new ArrayList<>();
for(int i = 0; i < 10; i++)
v.add(new Int(i));
ArrayList<Int> v = IntStream.range(0, 10)
.mapToObj(Int::new)
.collect(Collectors
.toCollection(ArrayList::new));
System.out.println("v: " + v);
@SuppressWarnings("unchecked")
ArrayList<Int> v2 = (ArrayList<Int>)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);
}

View File

@ -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());
}

View File

@ -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<Integer> v = new ArrayList<>();
for(int i = 0; i < 10; i++)
v.add(Integer.valueOf(i));
List<Integer> 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]
*/

View File

@ -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<IntValue> v = new ArrayList<>();
for(int i = 0; i < 10; i++)
v.add(new IntValue(i));
List<IntValue> 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);
}
}

View File

@ -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<IntValue2> 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<IntValue2> 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:

View File

@ -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: