Skip to content

Commit 5d0f0fe

Browse files
author
Bruce Eckel
committed
Rewrites
1 parent 79bb072 commit 5d0f0fe

File tree

16 files changed

+178
-117
lines changed

16 files changed

+178
-117
lines changed

collectiontopics/QueueBehavior.java

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,49 @@
22
// (c)2017 MindView LLC: see Copyright.txt
33
// We make no guarantees that this code is fit for any purpose.
44
// Visit http://OnJava8.com for more book information.
5-
// Compares the behavior of some of the queues
6-
import java.util.concurrent.*;
5+
// Compares basic behavior
76
import java.util.*;
7+
import java.util.stream.*;
8+
import java.util.concurrent.*;
89

910
public class QueueBehavior {
10-
private static String[] s =
11-
("one two three four five six seven " +
12-
"eight nine ten").split(" ");
13-
static void test(Queue<String> queue) {
14-
for(String ss : s)
15-
queue.offer(ss);
11+
static Stream<String> strings() {
12+
return Arrays.stream(
13+
("one two three four five six seven " +
14+
"eight nine ten").split(" "));
15+
}
16+
static void test(int id, Queue<String> queue) {
17+
System.out.print(id + ": ");
18+
strings().map(queue::offer).count();
1619
while(queue.peek() != null)
1720
System.out.print(queue.remove() + " ");
1821
System.out.println();
1922
}
2023
public static void main(String[] args) {
21-
int count = 10;
22-
test(new LinkedList<>());
23-
test(new PriorityQueue<>());
24-
test(new ArrayBlockingQueue<>(count));
25-
test(new ConcurrentLinkedQueue<>());
26-
test(new LinkedBlockingQueue<>());
27-
test(new PriorityBlockingQueue<>());
24+
int count = 10;
25+
test(1, new LinkedList<>());
26+
test(2, new PriorityQueue<>());
27+
test(3, new ArrayBlockingQueue<>(count));
28+
test(4, new ConcurrentLinkedQueue<>());
29+
test(5, new LinkedBlockingQueue<>());
30+
test(6, new PriorityBlockingQueue<>());
31+
test(7, new ArrayDeque<>());
32+
test(8, new ConcurrentLinkedDeque<>());
33+
test(9, new LinkedBlockingDeque<>());
34+
test(10, new LinkedTransferQueue<>());
35+
test(11, new SynchronousQueue<>());
2836
}
2937
}
3038
/* Output:
31-
one two three four five six seven eight nine ten
32-
eight five four nine one seven six ten three two
33-
one two three four five six seven eight nine ten
34-
one two three four five six seven eight nine ten
35-
one two three four five six seven eight nine ten
36-
eight five four nine one seven six ten three two
39+
1: one two three four five six seven eight nine ten
40+
2: eight five four nine one seven six ten three two
41+
3: one two three four five six seven eight nine ten
42+
4: one two three four five six seven eight nine ten
43+
5: one two three four five six seven eight nine ten
44+
6: eight five four nine one seven six ten three two
45+
7: one two three four five six seven eight nine ten
46+
8: one two three four five six seven eight nine ten
47+
9: one two three four five six seven eight nine ten
48+
10: one two three four five six seven eight nine ten
49+
11:
3750
*/

collectiontopics/SimpleDeques.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@
33
// We make no guarantees that this code is fit for any purpose.
44
// Visit http://OnJava8.com for more book information.
55
// Very basic test of Deques
6-
import java.util.concurrent.*;
76
import java.util.*;
7+
import java.util.concurrent.*;
88
import java.util.function.*;
99

1010
class CountString implements Supplier<String> {
1111
private int n = 0;
1212
public CountString() {}
1313
public CountString(int start) { n = start; }
1414
@Override
15-
public String get() { return Integer.toString(n++); }
15+
public String get() {
16+
return Integer.toString(n++);
17+
}
1618
}
1719

1820
public class SimpleDeques {

collectiontopics/SortedSetDemo.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22
// (c)2017 MindView LLC: see Copyright.txt
33
// We make no guarantees that this code is fit for any purpose.
44
// Visit http://OnJava8.com for more book information.
5-
// What you can do with a TreeSet
65
import java.util.*;
6+
import static java.util.stream.Collectors.*;
77

88
public class SortedSetDemo {
99
public static void main(String[] args) {
10-
SortedSet<String> sortedSet = new TreeSet<>();
11-
Collections.addAll(sortedSet,
12-
"one two three four five six seven eight"
13-
.split(" "));
10+
SortedSet<String> sortedSet =
11+
Arrays.stream(
12+
"one two three four five six seven eight"
13+
.split(" "))
14+
.collect(toCollection(TreeSet::new));
1415
System.out.println(sortedSet);
1516
String low = sortedSet.first();
1617
String high = sortedSet.last();

collectiontopics/ToDoList.java

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,44 +5,42 @@
55
// A more complex use of PriorityQueue
66
import java.util.*;
77

8-
class ToDoList extends PriorityQueue<ToDoList.ToDoItem> {
9-
static class ToDoItem implements Comparable<ToDoItem> {
10-
private char primary;
11-
private int secondary;
12-
private String item;
13-
public ToDoItem(String td, char pri, int sec) {
14-
primary = pri;
15-
secondary = sec;
16-
item = td;
17-
}
18-
@Override
19-
public int compareTo(ToDoItem arg) {
20-
if(primary > arg.primary)
8+
class ToDoItem implements Comparable<ToDoItem> {
9+
private char primary;
10+
private int secondary;
11+
private String item;
12+
public ToDoItem(String td, char pri, int sec) {
13+
primary = pri;
14+
secondary = sec;
15+
item = td;
16+
}
17+
@Override
18+
public int compareTo(ToDoItem arg) {
19+
if(primary > arg.primary)
20+
return +1;
21+
if(primary == arg.primary)
22+
if(secondary > arg.secondary)
2123
return +1;
22-
if(primary == arg.primary)
23-
if(secondary > arg.secondary)
24-
return +1;
25-
else if(secondary == arg.secondary)
26-
return 0;
27-
return -1;
28-
}
29-
@Override
30-
public String toString() {
31-
return Character.toString(primary) +
32-
secondary + ": " + item;
33-
}
24+
else if(secondary == arg.secondary)
25+
return 0;
26+
return -1;
3427
}
35-
public void add(String td, char pri, int sec) {
36-
super.add(new ToDoItem(td, pri, sec));
28+
@Override
29+
public String toString() {
30+
return Character.toString(primary) +
31+
secondary + ": " + item;
3732
}
33+
}
34+
35+
class ToDoList {
3836
public static void main(String[] args) {
39-
ToDoList toDo = new ToDoList();
40-
toDo.add("Empty trash", 'C', 4);
41-
toDo.add("Feed dog", 'A', 2);
42-
toDo.add("Feed bird", 'B', 7);
43-
toDo.add("Mow lawn", 'C', 3);
44-
toDo.add("Water lawn", 'A', 1);
45-
toDo.add("Feed cat", 'B', 1);
37+
PriorityQueue<ToDoItem> toDo = new PriorityQueue<>();
38+
toDo.add(new ToDoItem("Empty trash", 'C', 4));
39+
toDo.add(new ToDoItem("Feed dog", 'A', 2));
40+
toDo.add(new ToDoItem("Feed bird", 'B', 7));
41+
toDo.add(new ToDoItem("Mow lawn", 'C', 3));
42+
toDo.add(new ToDoItem("Water lawn", 'A', 1));
43+
toDo.add(new ToDoItem("Feed cat", 'B', 1));
4644
while(!toDo.isEmpty())
4745
System.out.println(toDo.remove());
4846
}

collectiontopics/TypesForSets.java

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,40 +6,48 @@
66
import java.util.*;
77
import java.util.function.*;
88
import java.util.Objects;
9+
import onjava.CountMap;
910

1011
class SetType {
11-
int i;
12+
protected int i;
1213
public SetType(int n) { i = n; }
1314
@Override
1415
public boolean equals(Object o) {
1516
return o instanceof SetType &&
1617
Objects.equals(i, ((SetType)o).i);
1718
}
1819
@Override
19-
public int hashCode() { return Objects.hash(i); }
20-
@Override
21-
public String toString() { return Integer.toString(i); }
20+
public String toString() {
21+
return Integer.toString(i);
22+
}
2223
}
2324

2425
class HashType extends SetType {
2526
public HashType(int n) { super(n); }
27+
@Override
28+
public int hashCode() {
29+
return Objects.hashCode(i);
30+
}
2631
}
2732

2833
class TreeType extends SetType
2934
implements Comparable<TreeType> {
3035
public TreeType(int n) { super(n); }
3136
@Override
3237
public int compareTo(TreeType arg) {
33-
return (arg.i < i ? -1 : (arg.i == i ? 0 : 1));
38+
return Integer.compare(arg.i, i);
39+
// Equivalent to:
40+
// return arg.i < i ? -1 : (arg.i == i ? 0 : 1);
3441
}
3542
}
3643

3744
public class TypesForSets {
38-
static <T> Set<T>
45+
static <T> void
3946
fill(Set<T> set, Function<Integer, T> type) {
40-
for(int i = 0; i < 10; i++)
41-
set.add(type.apply(i));
42-
return set;
47+
for(int i = 10; i >= 5; i--) // Descending
48+
set.add(type.apply(i));
49+
for(int i = 0; i < 5; i++) // Ascending
50+
set.add(type.apply(i));
4351
}
4452
static <T> void
4553
test(Set<T> set, Function<Integer, T> type) {
@@ -60,23 +68,27 @@ public static void main(String[] args) {
6068
try {
6169
test(new TreeSet<>(), SetType::new);
6270
} catch(Exception e) {
63-
System.out.println("Expected: " + e.getMessage());
71+
System.out.println(e.getMessage());
6472
}
6573
try {
6674
test(new TreeSet<>(), HashType::new);
6775
} catch(Exception e) {
68-
System.out.println("Expected: " + e.getMessage());
76+
System.out.println(e.getMessage());
6977
}
7078
}
7179
}
7280
/* Output:
73-
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
74-
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
75-
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
76-
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
77-
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
78-
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
79-
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
80-
Expected: SetType cannot be cast to java.lang.Comparable
81-
Expected: HashType cannot be cast to java.lang.Comparable
81+
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
82+
[10, 9, 8, 7, 6, 5, 0, 1, 2, 3, 4]
83+
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
84+
[1, 6, 8, 6, 2, 7, 8, 9, 4, 10, 7, 5, 1, 3, 4, 9, 9,
85+
10, 5, 3, 2, 0, 4, 1, 2, 0, 8, 3, 0, 10, 6, 5, 7]
86+
[3, 1, 4, 8, 7, 6, 9, 5, 3, 0, 10, 5, 5, 10, 7, 8, 8,
87+
9, 1, 4, 10, 2, 6, 9, 1, 6, 0, 3, 2, 0, 7, 2, 4]
88+
[10, 9, 8, 7, 6, 5, 0, 1, 2, 3, 4, 10, 9, 8, 7, 6, 5,
89+
0, 1, 2, 3, 4, 10, 9, 8, 7, 6, 5, 0, 1, 2, 3, 4]
90+
[10, 9, 8, 7, 6, 5, 0, 1, 2, 3, 4, 10, 9, 8, 7, 6, 5,
91+
0, 1, 2, 3, 4, 10, 9, 8, 7, 6, 5, 0, 1, 2, 3, 4]
92+
SetType cannot be cast to java.lang.Comparable
93+
HashType cannot be cast to java.lang.Comparable
8294
*/

onjava/CountMap.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ private static String value(int key) {
1919
Integer.toString(key / chars.length);
2020
}
2121
public CountMap(int size) {
22-
if(size < 0) this.size = 0;
23-
else this.size = size;
22+
this.size = size < 0 ? 0 : size;
2423
}
2524
@Override
2625
public String get(Object key) {
@@ -57,9 +56,17 @@ public Set<Map.Entry<Integer,String>> entrySet() {
5756
Collectors.toCollection(LinkedHashSet::new));
5857
}
5958
public static void main(String[] args) {
59+
final int LIM = 6;
6060
CountMap cm = new CountMap(60);
6161
System.out.println(cm);
6262
System.out.println(cm.get(500));
63+
cm.values().stream()
64+
.limit(LIM)
65+
.forEach(System.out::println);
66+
System.out.println();
67+
new Random().ints(LIM, 0, 1000)
68+
.mapToObj(cm::get)
69+
.forEach(System.out::println);
6370
}
6471
}
6572
/* Output:
@@ -72,4 +79,17 @@ public static void main(String[] args) {
7279
49=X1, 50=Y1, 51=Z1, 52=A2, 53=B2, 54=C2, 55=D2, 56=E2,
7380
57=F2, 58=G2, 59=H2}
7481
G19
82+
A0
83+
B0
84+
C0
85+
D0
86+
E0
87+
F0
88+
89+
E4
90+
X33
91+
P15
92+
L28
93+
M24
94+
Y36
7595
*/

patterns/PaperScissorsRock.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ class ItemFactory {
8686
Arrays.asList(
8787
Scissors::new, Paper::new, Rock::new);
8888
static final int SZ = items.size();
89-
private static SplittableRandom rand = new SplittableRandom(47);
89+
private static SplittableRandom rand =
90+
new SplittableRandom(47);
9091
public static Item newItem() {
9192
return items.get(rand.nextInt(SZ)).get();
9293
}

patterns/ShapeFactory1.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
import java.util.stream.*;
88
import patterns.shapes.*;
99

10-
class StaticFactory {
11-
static Shape create(String type) {
10+
public class ShapeFactory1 implements FactoryMethod {
11+
public Shape create(String type) {
1212
switch(type) {
1313
case "Circle": return new Circle();
1414
case "Square": return new Square();
@@ -17,16 +17,8 @@ static Shape create(String type) {
1717
throw new BadShapeCreation(type);
1818
}
1919
}
20-
}
21-
22-
public class ShapeFactory1 {
2320
public static void main(String[] args) {
24-
Stream.of("Circle", "Square", "Triangle",
25-
"Square", "Circle", "Circle", "Triangle")
26-
.map(StaticFactory::create)
27-
.peek(Shape::draw)
28-
.peek(Shape::erase)
29-
.count(); // Terminal operation
21+
FactoryTest.test(new ShapeFactory1());
3022
}
3123
}
3224
/* Output:

0 commit comments

Comments
 (0)