Skip to content

Commit ad33d22

Browse files
committed
Code review/streamification
1 parent f3ef6ec commit ad33d22

7 files changed

Lines changed: 39 additions & 31 deletions

File tree

‎references/AddingClone.java‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// You must go through a few gyrations
66
// to add cloning to your own class
77
import java.util.*;
8+
import java.util.stream.*;
89

910
class Int2 implements Cloneable {
1011
private int i;
@@ -41,18 +42,18 @@ public static void main(String[] args) {
4142
// Anything inherited is also cloneable:
4243
Int3 x3 = new Int3(7);
4344
x3 = (Int3)x3.clone();
44-
ArrayList<Int2> v = new ArrayList<>();
45-
for(int i = 0; i < 10; i++ )
46-
v.add(new Int2(i));
45+
ArrayList<Int2> v = IntStream.range(0, 10)
46+
.mapToObj(Int2::new)
47+
.collect(Collectors
48+
.toCollection(ArrayList::new));
4749
System.out.println("v: " + v);
4850
ArrayList<Int2> v2 =
4951
(ArrayList<Int2>)v.clone();
5052
// Now clone each element:
51-
for(int i = 0; i < v.size(); i++)
52-
v2.set(i, v2.get(i).clone());
53+
IntStream.range(0, v.size())
54+
.forEach(i -> v2.set(i, v.get(i).clone()));
5355
// Increment all v2's elements:
54-
for(Int2 i2 : v2)
55-
i2.increment();
56+
v2.forEach(Int2::increment);
5657
System.out.println("v2: " + v2);
5758
// See if it changed v's elements:
5859
System.out.println("v: " + v);

‎references/CloneArrayList.java‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// The clone() operation works for only a few
66
// items in the standard Java library
77
import java.util.*;
8+
import java.util.stream.*;
89

910
class Int {
1011
private int i;
@@ -18,15 +19,15 @@ public String toString() {
1819

1920
public class CloneArrayList {
2021
public static void main(String[] args) {
21-
ArrayList<Int> v = new ArrayList<>();
22-
for(int i = 0; i < 10; i++)
23-
v.add(new Int(i));
22+
ArrayList<Int> v = IntStream.range(0, 10)
23+
.mapToObj(Int::new)
24+
.collect(Collectors
25+
.toCollection(ArrayList::new));
2426
System.out.println("v: " + v);
2527
@SuppressWarnings("unchecked")
2628
ArrayList<Int> v2 = (ArrayList<Int>)v.clone();
2729
// Increment all v2's elements:
28-
for(Int e : v2)
29-
e.increment();
30+
v2.forEach(Int::increment);
3031
// See if it changed v's elements:
3132
System.out.println("v: " + v);
3233
}

‎references/CopyConstructor.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public static void ripen(Tomato t) {
117117
t.getClass().getName());
118118
}
119119
public static void slice(Fruit f) {
120-
f = new Fruit(f); // Hmmm... will this work? (2)
120+
f = new Fruit(f); // [2] Hmmm... will this work?
121121
System.out.println("In slice, f is a " +
122122
f.getClass().getName());
123123
}

‎references/ImmutableInteger.java‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,18 @@
44
// Visit http://OnJava8.com for more book information.
55
// The Integer class cannot be changed
66
import java.util.*;
7+
import java.util.stream.*;
78

89
public class ImmutableInteger {
910
public static void main(String[] args) {
10-
List<Integer> v = new ArrayList<>();
11-
for(int i = 0; i < 10; i++)
12-
v.add(Integer.valueOf(i));
11+
List<Integer> v = IntStream.range(0, 10)
12+
.mapToObj(Integer::new)
13+
.collect(Collectors.toList());
14+
System.out.println(v);
1315
// But how do you change the int
1416
// inside the Integer?
1517
}
1618
}
19+
/* Output:
20+
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
21+
*/

‎references/MutableInteger.java‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// Visit http://OnJava8.com for more book information.
55
// A changeable wrapper class
66
import java.util.*;
7+
import java.util.stream.*;
78

89
class IntValue {
910
private int n;
@@ -19,12 +20,11 @@ public String toString() {
1920

2021
public class MutableInteger {
2122
public static void main(String[] args) {
22-
List<IntValue> v = new ArrayList<>();
23-
for(int i = 0; i < 10; i++)
24-
v.add(new IntValue(i));
23+
List<IntValue> v = IntStream.range(0, 10)
24+
.mapToObj(IntValue::new)
25+
.collect(Collectors.toList());
2526
System.out.println(v);
26-
for(int i = 0; i < v.size(); i++)
27-
v.get(i).increment();
27+
v.forEach(IntValue::increment);
2828
System.out.println(v);
2929
}
3030
}

‎references/SimplerMutableInteger.java‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// Visit http://OnJava8.com for more book information.
55
// A trivial wrapper class
66
import java.util.*;
7+
import java.util.stream.*;
78

89
class IntValue2 {
910
public int n;
@@ -12,16 +13,15 @@ class IntValue2 {
1213

1314
public class SimplerMutableInteger {
1415
public static void main(String[] args) {
15-
List<IntValue2> v = new ArrayList<>();
16-
for(int i = 0; i < 10; i++)
17-
v.add(new IntValue2(i));
18-
for(IntValue2 i : v)
19-
System.out.print(i.n + " ");
16+
List<IntValue2> v = IntStream.range(0, 10)
17+
.mapToObj(IntValue2::new)
18+
.collect(Collectors.toList());
19+
v.forEach(iv2 ->
20+
System.out.print(iv2.n + " "));
2021
System.out.println();
21-
for(int i = 0; i < v.size(); i++)
22-
v.get(i).n = v.get(i).n + 1;
23-
for(IntValue2 i : v)
24-
System.out.print(i.n + " ");
22+
v.forEach(iv2 -> iv2.n += 1);
23+
v.forEach(iv2 ->
24+
System.out.print(iv2.n + " "));
2525
}
2626
}
2727
/* Output:

‎references/Snake.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ public static void main(String[] args) {
4040
Snake s2 = s.clone();
4141
System.out.println("s2 = " + s2);
4242
s.increment();
43-
System.out.println("after s.increment, s2 = " + s2);
43+
System.out.println(
44+
"after s.increment, s2 = " + s2);
4445
}
4546
}
4647
/* Output:

0 commit comments

Comments
 (0)