diff --git a/code/src/com/allendowney/thinkdast/MyBetterMap.java b/code/src/com/allendowney/thinkdast/MyBetterMap.java index 1e95e2b0..99f375f5 100644 --- a/code/src/com/allendowney/thinkdast/MyBetterMap.java +++ b/code/src/com/allendowney/thinkdast/MyBetterMap.java @@ -1,11 +1,12 @@ package com.allendowney.thinkdast; import java.util.ArrayList; -import java.util.Collection; -import java.util.HashSet; import java.util.List; +import java.util.LinkedList; import java.util.Map; import java.util.Set; +import java.util.HashSet; +import java.util.Collection; /** * Implementation of a Map using a collection of MyLinearMap, and @@ -90,6 +91,9 @@ public boolean isEmpty() { return size() == 0; } + /** + * Returns a Set of keys in the Map. Note that this Set is not backed by the Map. + */ @Override public Set keySet() { // add up the keySets from the sub-maps @@ -129,14 +133,18 @@ public int size() { return total; } + /** + * Returns a collection of values in the map. Note that this Collection is not + * backed by the Map. + */ @Override public Collection values() { // add up the valueSets from the sub-maps - Set set = new HashSet(); + Collection list = new LinkedList<>(); for (MyLinearMap map: maps) { - set.addAll(map.values()); + list.addAll(map.values()); } - return set; + return list; } /** diff --git a/code/src/com/allendowney/thinkdast/MyLinearMap.java b/code/src/com/allendowney/thinkdast/MyLinearMap.java index b99dcc55..0f9bfb71 100644 --- a/code/src/com/allendowney/thinkdast/MyLinearMap.java +++ b/code/src/com/allendowney/thinkdast/MyLinearMap.java @@ -3,12 +3,14 @@ */ package com.allendowney.thinkdast; + import java.util.ArrayList; -import java.util.Collection; -import java.util.HashSet; import java.util.List; +import java.util.LinkedList; import java.util.Map; import java.util.Set; +import java.util.HashSet; +import java.util.Collection; /** * Implementation of a Map using a List of entries, so most @@ -140,13 +142,17 @@ public int size() { return entries.size(); } + /** + * Returns a collection of values in the map. Note that this Collection is not + * backed by the Map. + */ @Override public Collection values() { - Set set = new HashSet(); + Collection list = new LinkedList<>(); for (Entry entry: entries) { - set.add(entry.getValue()); + list.add(entry.getValue()); } - return set; + return list; } /**