forked from prmr/DesignBook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSamples.java
More file actions
201 lines (167 loc) · 5.91 KB
/
Copy pathSamples.java
File metadata and controls
201 lines (167 loc) · 5.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*******************************************************************************
* Companion code for the book "Introduction to Software Design with Java"
* by Martin P. Robillard.
*
* Copyright (C) 2019 by Martin P. Robillard
*
* This code is licensed under a Creative Commons
* Attribution-NonCommercial-NoDerivatives 4.0 International License.
*
* See http://creativecommons.org/licenses/by-nc-nd/4.0/
*******************************************************************************/
package chapter9;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import chapter9.Suit.Color;
/**
* Code samples for Sections 9.1-9.2 and 9.6. The code samples for Section 9.3 are in
* their corresponding classes.
*/
public class Samples
{
public static void main(String[] args)
{
samples1(); // Section 9.1
samples2(); // Section 9.2
samples3(); // Section 9.6
}
/**
* For Section 9.1
*/
private static void samples1()
{
List<Card> cards = new Deck().getCards();
// Calling 'sort' with an object of an anonymous class
Collections.sort(cards, new Comparator<Card>()
{
public int compare(Card pCard1, Card pCard2)
{
return pCard1.getRank().compareTo(pCard2.getRank());
}
});
printAll(cards);
// Calling 'sort' with a method reference
Collections.sort(cards, Card::compareByRank);
printAll(cards);
}
/**
* For Section 9.2
*/
@SuppressWarnings("unused")
private static void samples2()
{
// Defining a function object of type Filter, which is an application-defined functional interface
Filter blackCards1 = new Filter()
{
public boolean accept(Card pCard)
{
return pCard.getSuit().getColor() == Suit.Color.BLACK;
}
};
// Defining a function object whose type is a library functional interface
Predicate<Card> blackCards2 = new Predicate<Card>()
{
public boolean test(Card pCard)
{
return pCard.getSuit().getColor() == Suit.Color.BLACK;
}
};
// Defining a predicate using a lambda expression (expression syntax with parameter type specified)
Predicate<Card> blackCards3 = (Card card) -> card.getSuit().getColor() == Suit.Color.BLACK;
// Defining a predicate using a lambda expression (block syntax with parameter type specified)
Predicate<Card> blackCards4 =
(Card card) -> { return card.getSuit().getColor() == Suit.Color.BLACK; };
// Defining a predicate using a lambda expression (expression syntax with parameter type not specified)
Predicate<Card> blackCards5 = (card) -> card.getSuit().getColor() == Suit.Color.BLACK;
// Defining a predicate using a lambda expression (expression syntax with parameter type not specified
// and no parentheses around the parameter
Predicate<Card> blackCards6 = card -> card.getSuit().getColor() == Suit.Color.BLACK;
// Sample use of the filter:
int total = 0;
for( Card card : new Deck().getCards() )
{
if( blackCards6.test(card) )
{
total++;
}
}
System.out.println(total);
// Example use of removeIf
ArrayList<Card> cards = new ArrayList<>(new Deck().getCards());
cards.removeIf(card -> card.getSuit().getColor() == Suit.Color.BLACK );
printAll(cards);
// Example of using a method reference with removeIf
cards = new ArrayList<>(new Deck().getCards());
cards.removeIf(Card::hasBlackSuit);
printAll(cards);
}
/**
* For Section 9.6
*/
@SuppressWarnings("unused")
public static void samples3()
{
Stream<Card> cards = new Deck().stream();
long total = cards.count();
System.out.println(total);
Stream<Card> sortedCards = new Deck().stream().sorted();
sortedCards.forEach(System.out::println);
Stream<Card> sortedCards2 = new Deck().stream().sorted().limit(10);
sortedCards2.forEach(System.out::println);
Stream<Card> cards2 = Stream.concat(new Deck().stream(), new Deck().stream());
System.out.println(cards2.count());
Stream<Card> withDuplicates = Stream.concat(new Deck().stream(), new Deck().stream());
Stream<Card> withoutDuplicates = withDuplicates.distinct();
new Deck().stream().forEach(card -> System.out.println(card));
boolean allClubs = new Deck().stream()
.allMatch(card -> card.getSuit() == Suit.CLUBS );
long numberOfFaceCards = new Deck().stream()
.filter(card -> card.getRank().ordinal() >= Rank.JACK.ordinal()).count();
long numberOfFaceCards2 = new Deck().stream()
.filter(Card::isFaceCard)
.count();
long result = new Deck().stream()
.filter(card -> card.getRank().ordinal() >= Rank.JACK.ordinal()
&& card.getSuit()==Suit.CLUBS).count();
long result2 = new Deck().stream()
.filter(Card::isFaceCard)
.filter(card -> card.getSuit() == Suit.CLUBS)
.count();
new Deck().stream().map(card -> card.getSuit().getColor() );
long result3 = new Deck().stream()
.map(card -> card.getSuit().getColor() )
.filter( color -> color == Color.BLACK )
.count();
new Deck().stream()
.map(card -> Math.min(10, card.getRank().ordinal() + 1));
int total2 = new Deck().stream()
.mapToInt(card -> Math.min(10, card.getRank().ordinal() + 1))
.sum();
int total3 = new Deck().stream()
.map(Card::getRank)
.mapToInt(Rank::ordinal)
.map(ordinal -> Math.min(10, ordinal + 1))
.sum();
// Not ideal
List<Card> result4 = new ArrayList<>();
new Deck().stream()
.filter(Card::isFaceCard)
.forEach(card -> result4.add(card));
// Better
List<Card> result5 = new Deck().stream()
.filter(Card::isFaceCard)
.collect(Collectors.toList());
}
private static void printAll(List<Card> pCards)
{
for( Card card : pCards)
{
System.out.println(card);
}
}
}