Skip to content

Commit c641e58

Browse files
committed
Clean up code examples from Chapter 7 (Lambdas and Streams)
1 parent d2cf57e commit c641e58

File tree

16 files changed

+77
-42
lines changed

16 files changed

+77
-42
lines changed

src/effectivejava/chapter6/item34/Operation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import static java.util.stream.Collectors.toMap;
66

7-
// Enum type with constant-specific class bodies and data (Page 163-4)
7+
// Enum type with constant-specific class bodies and data (Pages 163-4)
88
public enum Operation {
99
PLUS("+") {
1010
public double apply(double x, double y) { return x + y; }

src/effectivejava/chapter7/item42/Operation.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import java.util.function.DoubleBinaryOperator;
44

5-
// Enum with function object fields & constant-specific behavior
5+
// Enum with function object fields & constant-specific behavior (Page 195)
66
public enum Operation {
77
PLUS ("+", (x, y) -> x + y),
88
MINUS ("-", (x, y) -> x - y),
@@ -22,4 +22,13 @@ public enum Operation {
2222
public double apply(double x, double y) {
2323
return op.applyAsDouble(x, y);
2424
}
25+
26+
// Main method from Item 34 (Page 163)
27+
public static void main(String[] args) {
28+
double x = Double.parseDouble(args[0]);
29+
double y = Double.parseDouble(args[1]);
30+
for (Operation op : Operation.values())
31+
System.out.printf("%f %s %f = %f%n",
32+
x, op, y, op.apply(x, y));
33+
}
2534
}

src/effectivejava/chapter7/item42/SortFourWays.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88

99
import static java.util.Comparator.comparingInt;
1010

11+
// Sorting with function objects (Pages 193-4)
1112
public class SortFourWays {
1213
public static void main(String[] args) {
1314
List<String> words = Arrays.asList(args);
1415

15-
// Anonymous class instance as a function object - obsolete!
16+
// Anonymous class instance as a function object - obsolete! (Page 193)
1617
Collections.sort(words, new Comparator<String>() {
1718
public int compare(String s1, String s2) {
1819
return Integer.compare(s1.length(), s2.length());
@@ -21,16 +22,18 @@ public int compare(String s1, String s2) {
2122
System.out.println(words);
2223
Collections.shuffle(words);
2324

24-
// Lambda expression as function object (replaces anonymous class)
25+
// Lambda expression as function object (replaces anonymous class) (Page 194)
2526
Collections.sort(words,
2627
(s1, s2) -> Integer.compare(s1.length(), s2.length()));
2728
System.out.println(words);
2829
Collections.shuffle(words);
2930

31+
// Comparator construction method (with method reference) in place of lambda (Page 194)
3032
Collections.sort(words, comparingInt(String::length));
3133
System.out.println(words);
3234
Collections.shuffle(words);
3335

36+
// Default method List.sort in conjunction with comparator construction method (Page 194)
3437
words.sort(comparingInt(String::length));
3538
System.out.println(words);
3639
}

src/effectivejava/chapter7/item43/Freq.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,19 @@
33
import java.util.Map;
44
import java.util.TreeMap;
55

6-
// p. 197
6+
// Frequency table implemented with map.merge, using lambda and method reference (Page 197)
77
public class Freq {
88
public static void main(String[] args) {
99
Map<String, Integer> frequencyTable = new TreeMap<>();
10-
for (String s : args) {
11-
// map.merge(key, 1, (count, incr) -> count + incr);
12-
frequencyTable.merge(s, 1, Integer::sum);
13-
}
10+
11+
for (String s : args)
12+
frequencyTable.merge(s, 1, (count, incr) -> count + incr); // Lambda
13+
System.out.println(frequencyTable);
1414

15+
frequencyTable.clear();
16+
for (String s : args)
17+
frequencyTable.merge(s, 1, Integer::sum); // Method reference
1518
System.out.println(frequencyTable);
19+
1620
}
1721
}

src/effectivejava/chapter7/item45/Card.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
import java.util.stream.Stream;
66
import static java.util.stream.Collectors.*;
77

8+
// Generating the Cartesian product of two lists using iteration and streams (Page 209)
89
public class Card {
910
public enum Suit { SPADE, HEART, DIAMOND, CLUB }
1011
public enum Rank { ACE, DEUCE, THREE, FOUR, FIVE, SIX, SEVEN,
11-
EIGHT, NINE, TEN, JACK, KING, QUEEN }
12+
EIGHT, NINE, TEN, JACK, QUEEN, KING }
1213

1314
private final Suit suit;
1415
private final Rank rank;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package effectivejava.chapter7.item45;
2+
3+
// Refrain from using streams to process char values (Page 206)
4+
public class CharStream {
5+
public static void main(String[] args) {
6+
// Does not produce the expected result
7+
"Hello world!".chars().forEach(System.out::print);
8+
System.out.println();
9+
10+
// Fixes the problem
11+
"Hello world!".chars().forEach(x -> System.out.print((char) x));
12+
System.out.println();
13+
}
14+
}

src/effectivejava/chapter7/item45/MersennePrimes.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import static java.math.BigInteger.*;
77

8-
// P. 208
8+
// Generating the first twent Mersenne primes using streams (Page 208)
99
public class MersennePrimes {
1010
static Stream<BigInteger> primes() {
1111
return Stream.iterate(TWO, BigInteger::nextProbablePrime);

src/effectivejava/chapter7/item45/anagrams/HybridAnagrams.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
import static java.util.stream.Collectors.groupingBy;
1111

12-
// Tasteful use of streams enhances clarity and conciseness
12+
// Tasteful use of streams enhances clarity and conciseness (Page 205)
1313
public class HybridAnagrams {
1414
public static void main(String[] args) throws IOException {
1515
Path dictionary = Paths.get(args[0]);

src/effectivejava/chapter7/item45/anagrams/IterativeAnagrams.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import java.io.IOException;
55
import java.util.*;
66

7-
// Prints all large anagram groups in a dictionary iteratively
7+
// Prints all large anagram groups in a dictionary iteratively (Page 204)
88
public class IterativeAnagrams {
99
public static void main(String[] args) throws IOException {
1010
File dictionary = new File(args[0]);

src/effectivejava/chapter7/item45/anagrams/StreamAnagrams.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import static java.util.stream.Collectors.groupingBy;
1010

11-
// Overuse of streams - don't do this! - (page 205)
11+
// Overuse of streams - don't do this! (page 205)
1212
public class StreamAnagrams {
1313
public static void main(String[] args) throws IOException {
1414
Path dictionary = Paths.get(args[0]);

0 commit comments

Comments
 (0)