Newer
Older
Stuart Marks
committed
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
package exercises;
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
/**
* This set of exercises covers simple stream pipelines,
* including intermediate operations and basic collectors.
*
* Some of these exercises use a BufferedReader variable
* named "reader" that the test has set up for you.
*/
public class D_SimpleStreams {
/**
* Given a list of words, create an output list that contains
* only the odd-length words, converted to upper case.
*/
@Test @Ignore
public void ex06_upcaseOddLengthWords() {
List<String> input = Arrays.asList(
"alfa", "bravo", "charlie", "delta", "echo", "foxtrot");
Stuart Marks
committed
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
List<String> result = null; // TODO
assertEquals("[BRAVO, CHARLIE, DELTA, FOXTROT]", result.toString());
}
// Hint 1:
// <editor-fold defaultstate="collapsed">
// Use filter() and map().
// </editor-fold>
// Hint 2:
// <editor-fold defaultstate="collapsed">
// Use collect() to create the result list.
// </editor-fold>
/**
* Take the third through fifth words of the list, extract the
* second letter from each, and join them, separated by commas,
* into a single string. Watch for off-by-one errors.
*/
@Test @Ignore
public void ex07_joinStreamRange() {
List<String> input = new ArrayList<>(Arrays.asList(
"alfa", "bravo", "charlie", "delta", "echo", "foxtrot"));
String result = ""; // TODO
assertEquals("h,e,c", result);
}
// Hint 1:
// <editor-fold defaultstate="collapsed">
// Use Stream.skip() and Stream.limit().
// </editor-fold>
// Hint 2:
// <editor-fold defaultstate="collapsed">
// Use Collectors.joining().
// </editor-fold>
/**
* Count the number of lines in the text file. (Remember to
* use the BufferedReader named "reader" that has already been
* opened for you.)
*
* @throws IOException
*/
@Test @Ignore
public void ex08_countLinesInFile() throws IOException {
long count = 0; // TODO
assertEquals(14, count);
}
// Hint 1:
// <editor-fold defaultstate="collapsed">
// Use BufferedReader.lines() to get a stream of lines.
// </editor-fold>
// Hint 2:
// <editor-fold defaultstate="collapsed">
// Use Stream.count().
// </editor-fold>
/**
* Find the length of the longest line in the text file.
*
* @throws IOException
*/
@Test @Ignore
public void ex09_findLengthOfLongestLine() throws IOException {
int longestLength = 0; // TODO
assertEquals(53, longestLength);
}
// Hint 1:
// <editor-fold defaultstate="collapsed">
// Use Stream.mapToInt() to convert to IntStream.
// </editor-fold>
// Hint 2:
// <editor-fold defaultstate="collapsed">
// Look at java.util.OptionalInt to get the result.
// </editor-fold>
/**
* Find the longest line in the text file.
*
* @throws IOException
*/
@Test @Ignore
public void ex10_findLongestLine() throws IOException {
Stuart Marks
committed
assertEquals("Feed'st thy light's flame with self-substantial fuel,", longest);
}
// Hint 1:
// <editor-fold defaultstate="collapsed">
// Use Stream.max() with a Comparator.
// </editor-fold>
// Hint 2:
// <editor-fold defaultstate="collapsed">
// Use static methods on Comparator to help create a Comparator instance.
// </editor-fold>
/**
* Select the list of words from the input list whose length is greater than
Stuart Marks
committed
* the word's position in the list (starting from zero) .
*/
@Test @Ignore
public void ex11_selectByLengthAndPosition() {
List<String> input = new ArrayList<>(Arrays.asList(
"alfa", "bravo", "charlie", "delta", "echo", "foxtrot", "golf", "hotel"));
List<String> result = null; // TODO
assertEquals(Arrays.asList("alfa", "bravo", "charlie", "delta", "foxtrot"), result);
Stuart Marks
committed
}
// Hint:
// <editor-fold defaultstate="collapsed">
// Instead of a stream of words (Strings), run an IntStream of positions.
// </editor-fold>
/**
* Given two lists of Integer, compute a third list where each element is the
* difference between the corresponding elements of the two input lists
* (first minus second).
*/
@Test @Ignore
public void ex12_listDifference() {
List<Integer> one = Arrays.asList(3, 1, 4, 1, 5, 9, 2, 6, 5, 3);
List<Integer> two = Arrays.asList(2, 7, 1, 8, 2, 8, 1, 8, 2, 8);
List<Integer> result = null; // TODO
assertEquals(Arrays.asList(1, -6, 3, -7, 3, 1, 1, -2, 3, -5), result);
Stuart Marks
committed
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
202
203
204
205
206
}
// Hint 1:
// <editor-fold defaultstate="collapsed">
// Run an IntStream of list positions (indexes).
// </editor-fold>
// Hint 2:
// <editor-fold defaultstate="collapsed">
// Deal with boxed Integers either by casting or by using mapToObj().
// </editor-fold>
// ========================================================
// END OF EXERCISES
// TEST INFRASTRUCTURE IS BELOW
// ========================================================
private BufferedReader reader;
@Before
public void z_setUpBufferedReader() throws IOException {
reader = Files.newBufferedReader(
Paths.get("SonnetI.txt"), StandardCharsets.UTF_8);
}
@After
public void z_closeBufferedReader() throws IOException {
reader.close();
}
}