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
1ba41ec9
Commit
1ba41ec9
authored
8 years ago
by
Stuart Marks
Browse files
Options
Downloads
Patches
Plain Diff
remove Animal class from ex26
parent
1fcdc51d
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
LambdaLab/test/solutions/Exercises.java
+27
-40
27 additions, 40 deletions
LambdaLab/test/solutions/Exercises.java
with
27 additions
and
40 deletions
LambdaLab/test/solutions/Exercises.java
+
27
−
40
View file @
1ba41ec9
...
@@ -898,9 +898,8 @@ public class Exercises {
...
@@ -898,9 +898,8 @@ public class Exercises {
/**
/**
* Denormalize this map. The input is a map whose keys are the number of legs of an animal
* Denormalize this map. The input is a map whose keys are the number of legs of an animal
* and whose values are lists of names of animals. Run through the map and generate a
* and whose values are lists of names of animals. Run through the map and generate a
* "denormalized" list of Animal objects using the provided Animal class, where
* "denormalized" list of strings describing the animal, with the animal's name separated
* each Animal instance contains the name of the animal and the number of legs.
* by a comma from the number of legs it has. The ordering in the output list is not
* A simple Animal data class is provided. The ordering in the output list is not
* considered significant.
* considered significant.
*
*
* Input is Map<Integer, List<String>>:
* Input is Map<Integer, List<String>>:
...
@@ -909,13 +908,13 @@ public class Exercises {
...
@@ -909,13 +908,13 @@ public class Exercises {
* ...
* ...
* }
* }
*
*
* Output should be a List<
Animal
>:
* Output should be a List<
String
>:
* [
Animal("ibex", 4)
,
* [
"ibex,4"
,
*
Animal(
"hedgehog
", 4)
,
* "hedgehog
,4"
,
*
Animal(
"wombat
", 4)
,
* "wombat
,4"
,
*
Animal("ant", 6)
,
*
"ant,6"
,
*
Animal(
"beetle
", 6)
,
* "beetle
,6"
,
*
Animal(
"cricket
", 6)
,
* "cricket
,6"
,
* ...
* ...
* ]
* ]
*/
*/
...
@@ -928,56 +927,44 @@ public class Exercises {
...
@@ -928,56 +927,44 @@ public class Exercises {
input
.
put
(
10
,
Arrays
.
asList
(
"crab"
,
"lobster"
,
"scorpion"
));
input
.
put
(
10
,
Arrays
.
asList
(
"crab"
,
"lobster"
,
"scorpion"
));
input
.
put
(
750
,
Arrays
.
asList
(
"millipede"
));
input
.
put
(
750
,
Arrays
.
asList
(
"millipede"
));
class
Animal
{
//UNCOMMENT//List<String> result = null; // TODO
final
String
name
;
final
int
legs
;
public
Animal
(
String
s
,
int
i
)
{
name
=
s
;
this
.
legs
=
i
;
}
@Override
public
boolean
equals
(
Object
obj
)
{
if
(!
(
obj
instanceof
Animal
))
return
false
;
Animal
other
=
(
Animal
)
obj
;
return
this
.
name
.
equals
(
other
.
name
)
&&
this
.
legs
==
other
.
legs
;
}
@Override
public
int
hashCode
()
{
return
name
.
hashCode
()
^
legs
;
}
@Override
public
String
toString
()
{
return
String
.
format
(
"(%s,%d)"
,
name
,
legs
);
}
}
//UNCOMMENT//List<Animal> result = null; // TODO
//BEGINREMOVE
//BEGINREMOVE
// Simple solution: use Map.forEach to iterate over each entry,
// Simple solution: use Map.forEach to iterate over each entry,
// and use a nested List.forEach to iterate over each list entry,
// and use a nested List.forEach to iterate over each list entry,
// and accumulate values into the result list.
// and accumulate values into the result list.
List
<
Animal
>
result
=
new
ArrayList
<>();
List
<
String
>
result
=
new
ArrayList
<>();
input
.
forEach
((
legs
,
names
)
->
input
.
forEach
((
legs
,
names
)
->
names
.
forEach
(
name
->
result
.
add
(
n
ew
Animal
(
name
,
legs
)))
)
;
names
.
forEach
(
name
->
result
.
add
(
n
ame
+
","
+
legs
)));
// Alternative solution: stream over map entries, and use flatMap to generate
// Alternative solution: stream over map entries, and use flatMap to generate
// Animal instances for each animal name with the given number of legs. This
// Animal instances for each animal name with the given number of legs. This
// is more complicated, but it's a more general technique, and it can be run
// is more complicated, but it's a more general technique, and it can be run
// in parallel.
// in parallel.
// List<
Animal
> result =
// List<
String
> result =
// input.entrySet().stream()
// input.entrySet().stream()
// .flatMap(entry -> entry.getValue().stream()
// .flatMap(entry -> entry.getValue().stream()
// .map(name -> n
ew Animal(name,
entry.getKey()))
)
// .map(name -> n
ame + "," +
entry.getKey()))
// .collect(toList());
// .collect(toList());
//ENDREMOVE
//ENDREMOVE
assertEquals
(
13
,
result
.
size
());
assertEquals
(
13
,
result
.
size
());
assertTrue
(
result
.
contains
(
new
Animal
(
"ibex"
,
4
)
));
assertTrue
(
result
.
contains
(
"ibex,4"
));
assertTrue
(
result
.
contains
(
new
Animal
(
"hedgehog
"
,
4
)
));
assertTrue
(
result
.
contains
(
"hedgehog
,4"
));
assertTrue
(
result
.
contains
(
new
Animal
(
"wombat
"
,
4
)
));
assertTrue
(
result
.
contains
(
"wombat
,4"
));
assertTrue
(
result
.
contains
(
new
Animal
(
"ant"
,
6
)
));
assertTrue
(
result
.
contains
(
"ant,6"
));
assertTrue
(
result
.
contains
(
new
Animal
(
"beetle
"
,
6
)
));
assertTrue
(
result
.
contains
(
"beetle
,6"
));
assertTrue
(
result
.
contains
(
new
Animal
(
"cricket
"
,
6
)
));
assertTrue
(
result
.
contains
(
"cricket
,6"
));
assertTrue
(
result
.
contains
(
new
Animal
(
"octopus
"
,
8
)
));
assertTrue
(
result
.
contains
(
"octopus
,8"
));
assertTrue
(
result
.
contains
(
new
Animal
(
"spider
"
,
8
)
));
assertTrue
(
result
.
contains
(
"spider
,8"
));
assertTrue
(
result
.
contains
(
new
Animal
(
"squid
"
,
8
)
));
assertTrue
(
result
.
contains
(
"squid
,8"
));
assertTrue
(
result
.
contains
(
new
Animal
(
"crab
"
,
10
)
));
assertTrue
(
result
.
contains
(
"crab
,
10
"
));
assertTrue
(
result
.
contains
(
new
Animal
(
"lobster
"
,
10
)
));
assertTrue
(
result
.
contains
(
"lobster
,
10
"
));
assertTrue
(
result
.
contains
(
new
Animal
(
"scorpion
"
,
10
)
));
assertTrue
(
result
.
contains
(
"scorpion
,
10
"
));
assertTrue
(
result
.
contains
(
new
Animal
(
"millipede
"
,
750
)
));
assertTrue
(
result
.
contains
(
"millipede
,
750
"
));
}
}
// Hint 1:
// Hint 1:
// <editor-fold defaultstate="collapsed">
// <editor-fold defaultstate="collapsed">
...
...
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