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

remove Animal class from ex26

parent 1fcdc51d
No related branches found
No related tags found
No related merge requests found
...@@ -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(new Animal(name, legs)))); names.forEach(name -> result.add(name + "," + 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 -> new Animal(name, entry.getKey()))) // .map(name -> name + "," + 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">
......
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