Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions code/src/com/allendowney/thinkdast/MyBetterMap.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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<K> keySet() {
// add up the keySets from the sub-maps
Expand Down Expand Up @@ -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<V> values() {
// add up the valueSets from the sub-maps
Set<V> set = new HashSet<V>();
Collection<V> list = new LinkedList<>();
for (MyLinearMap<K, V> map: maps) {
set.addAll(map.values());
list.addAll(map.values());
}
return set;
return list;
}

/**
Expand Down
16 changes: 11 additions & 5 deletions code/src/com/allendowney/thinkdast/MyLinearMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<V> values() {
Set<V> set = new HashSet<V>();
Collection<V> list = new LinkedList<>();
for (Entry entry: entries) {
set.add(entry.getValue());
list.add(entry.getValue());
}
return set;
return list;
}

/**
Expand Down