Skip to content
Snippets Groups Projects
Commit c0edc8a0 authored by Stuart Marks's avatar Stuart Marks
Browse files

get rid of peek/AtomicLong exercise and revamp to TotalAndDistinct collectors exercise

parent d96215b0
No related branches found
No related tags found
No related merge requests found
...@@ -8,10 +8,8 @@ import java.nio.file.Paths; ...@@ -8,10 +8,8 @@ import java.nio.file.Paths;
import java.util.Arrays; import java.util.Arrays;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.concurrent.atomic.LongAdder;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
...@@ -210,39 +208,6 @@ public class E_IntermediateStreams { ...@@ -210,39 +208,6 @@ public class E_IntermediateStreams {
// </editor-fold> // </editor-fold>
/**
* Count the total number of words and the number of distinct, lower case
* words in the text file, in one pass.
*/
@Test
public void ex18_countTotalAndDistinctWords() {
//TODO//long distinctCount = 0;
//TODO//long totalCount = 0;
//BEGINREMOVE
LongAdder adder = new LongAdder();
long distinctCount =
reader.lines()
.flatMap(line -> WORD_PATTERN.splitAsStream(line))
.map(String::toLowerCase)
.peek(s -> adder.increment())
.distinct()
.count();
long totalCount = adder.longValue();
//ENDREMOVE
assertEquals("distinct count", 81, distinctCount);
assertEquals("total count", 107, totalCount);
}
// Hint 1:
// <editor-fold defaultstate="collapsed">
// Use Stream.peek().
// </editor-fold>
// Hint 2:
// <editor-fold defaultstate="collapsed">
// Use LongAdder or AtomicLong/AtomicInteger to allow peek() to have side effects.
// </editor-fold>
// ======================================================== // ========================================================
// END OF EXERCISES // END OF EXERCISES
// TEST INFRASTRUCTURE IS BELOW // TEST INFRASTRUCTURE IS BELOW
......
...@@ -12,10 +12,12 @@ import java.nio.charset.StandardCharsets; ...@@ -12,10 +12,12 @@ import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.IntSummaryStatistics; import java.util.IntSummaryStatistics;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
import java.util.function.Function; import java.util.function.Function;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.Collectors; import java.util.stream.Collectors;
...@@ -294,6 +296,61 @@ public class F_AdvancedStreams { ...@@ -294,6 +296,61 @@ public class F_AdvancedStreams {
// in half and prepended/appended to the first argument. // in half and prepended/appended to the first argument.
// </editor-fold> // </editor-fold>
/**
* Count the total number of words and the number of distinct, lower case
* words in a stream, in one pass. This exercise uses a helper class
* that defines methods that are called by the Stream.collect() method.
* Your task is to fill in the implementation of the accumulate() and
* combine() methods in the helper class. You don't need to modify the
* test method itself.
*
* The stream is run in parallel, so you must write a combine() method
* that works properly.
*/
static class TotalAndDistinct {
private int count = 0;
private final Set<String> set = new HashSet<>();
// rely on implicit no-arg constructor
void accumulate(String s) {
//UNCOMMENT////TODO write code to accumulate a single string into this object
//BEGINREMOVE
count++;
set.add(s);
//ENDREMOVE
}
void combine(TotalAndDistinct other) {
//UNCOMMENT////TODO write code to combine the other object into this one
//BEGINREMOVE
count += other.count;
set.addAll(other.set);
//ENDREMOVE
}
int getTotalCount() { return count; }
int getDistinctCount() { return set.size(); }
}
@Test
public void ex26_countTotalAndDistinctWords() {
List<String> allWords = reader.lines()
.map(String::toLowerCase)
.flatMap(line -> WORD_PATTERN.splitAsStream(line))
.collect(Collectors.toList());
TotalAndDistinct totalAndDistinct =
Collections.nCopies(100, allWords)
.parallelStream()
.flatMap(List::stream)
.collect(TotalAndDistinct::new,
TotalAndDistinct::accumulate,
TotalAndDistinct::combine);
assertEquals("distinct count", 81, totalAndDistinct.getDistinctCount());
assertEquals("total count", 10700, totalAndDistinct.getTotalCount());
}
// ======================================================== // ========================================================
// END OF EXERCISES // END OF EXERCISES
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment