From ce30af1c057e0a53f3d8f5b88977216b078e96a7 Mon Sep 17 00:00:00 2001 From: David Abrahams Date: Mon, 6 Feb 2017 14:35:23 -0500 Subject: [PATCH 1/2] fixing values bug --- .../allendowney/thinkdast/MyBetterMap.java | 20 ++++++++++--------- .../allendowney/thinkdast/MyLinearMap.java | 17 ++++++++-------- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/code/src/com/allendowney/thinkdast/MyBetterMap.java b/code/src/com/allendowney/thinkdast/MyBetterMap.java index 1e95e2b0..85098f08 100644 --- a/code/src/com/allendowney/thinkdast/MyBetterMap.java +++ b/code/src/com/allendowney/thinkdast/MyBetterMap.java @@ -1,11 +1,6 @@ package com.allendowney.thinkdast; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; +import java.util.*; /** * Implementation of a Map using a collection of MyLinearMap, and @@ -90,6 +85,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 +127,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..dbaf49f0 100644 --- a/code/src/com/allendowney/thinkdast/MyLinearMap.java +++ b/code/src/com/allendowney/thinkdast/MyLinearMap.java @@ -3,12 +3,7 @@ */ package com.allendowney.thinkdast; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; +import java.util.*; /** * Implementation of a Map using a List of entries, so most @@ -140,13 +135,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; } /** From 7b99dbdac70159c499efd173bdb2927d5e17f92a Mon Sep 17 00:00:00 2001 From: David Abrahams Date: Mon, 6 Feb 2017 14:37:42 -0500 Subject: [PATCH 2/2] fixing imports --- code/src/com/allendowney/thinkdast/MyBetterMap.java | 8 +++++++- code/src/com/allendowney/thinkdast/MyLinearMap.java | 9 ++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/code/src/com/allendowney/thinkdast/MyBetterMap.java b/code/src/com/allendowney/thinkdast/MyBetterMap.java index 85098f08..99f375f5 100644 --- a/code/src/com/allendowney/thinkdast/MyBetterMap.java +++ b/code/src/com/allendowney/thinkdast/MyBetterMap.java @@ -1,6 +1,12 @@ package com.allendowney.thinkdast; -import java.util.*; +import java.util.ArrayList; +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 diff --git a/code/src/com/allendowney/thinkdast/MyLinearMap.java b/code/src/com/allendowney/thinkdast/MyLinearMap.java index dbaf49f0..0f9bfb71 100644 --- a/code/src/com/allendowney/thinkdast/MyLinearMap.java +++ b/code/src/com/allendowney/thinkdast/MyLinearMap.java @@ -3,7 +3,14 @@ */ package com.allendowney.thinkdast; -import java.util.*; + +import java.util.ArrayList; +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