Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
L
lab_reactive_java
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
rtbdp
labs
lab_reactive_java
Commits
c0edc8a0
Commit
c0edc8a0
authored
8 years ago
by
Stuart Marks
Browse files
Options
Downloads
Patches
Plain Diff
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
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
LambdaLab/test/solutions/E_IntermediateStreams.java
+0
-35
0 additions, 35 deletions
LambdaLab/test/solutions/E_IntermediateStreams.java
LambdaLab/test/solutions/F_AdvancedStreams.java
+57
-0
57 additions, 0 deletions
LambdaLab/test/solutions/F_AdvancedStreams.java
with
57 additions
and
35 deletions
LambdaLab/test/solutions/E_IntermediateStreams.java
+
0
−
35
View file @
c0edc8a0
...
@@ -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
...
...
This diff is collapsed.
Click to expand it.
LambdaLab/test/solutions/F_AdvancedStreams.java
+
57
−
0
View file @
c0edc8a0
...
@@ -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
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment