Results 1 to 5 of 5
Thread: Help with Maps
- 12-13-2010, 05:34 PM #1
Member
- Join Date
- Dec 2010
- Posts
- 3
- Rep Power
- 0
Help with Maps
I need help with maps (Thinking inJava 3-d ed., 11#34).
I have my class:
It doesn't work when i want to delete smth or deleteAll. (There are no errors but nothing happens when I use map.clear)Java Code:public class SlowMap extends AbstractMap { //private static Test monitor = new Test(); private List keys = new ArrayList(), values = new ArrayList(); @Override public Object put(Object key, Object value) { Object result = get(key); if(!keys.contains(key)) { keys.add(key); values.add(value); } else values.set(keys.indexOf(key), value); return result; } @Override public Object get(Object key) { if(!keys.contains(key)) return null; return values.get(keys.indexOf(key)); } public Set entrySet() { Set entries = new HashSet(); Iterator ki = keys.iterator(), vi = values.iterator(); while(ki.hasNext()) entries.add(new MPair(ki.next(), vi.next())); return entries; } @Override public String toString() { StringBuffer s = new StringBuffer("{"); Iterator ki = keys.iterator(), vi = values.iterator(); while(ki.hasNext()) { s.append(ki.next() + "=" + vi.next()); if(ki.hasNext()) s.append(", "); } s.append("}"); return s.toString(); } public static void main(String[] args) { SlowMap m = new SlowMap(); Collections2.fill(m, Collections2.geography, 15); System.out.println(m); } }
How should I modify this?
- 12-13-2010, 05:52 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
If you're making a modifiable map the API docs say that "the iterator returned by entrySet().iterator() must additionally implement its remove method". The iterator of the set returned by entrySet() does implement remove(): but that implementation isn't actually removing anything from the map.
[Edit]
How should I modify this?
This is just a guess, but perhaps the set returned by entrySet() could be an inner class whose iterator() returns an iterator where remove() alters the map. (If it's not considered "cheating" I would have a look at src.zip.)Last edited by pbrockway2; 12-13-2010 at 05:55 PM.
- 12-13-2010, 06:11 PM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
In fact the API docs for entrySet() should be studied because by returning a new HashSet you are not fulfilling the contract that "changes to the map are reflected in the set, and vice-versa".
Perhaps there's more to this: including what an MPair is.
- 12-13-2010, 06:21 PM #4
Member
- Join Date
- Dec 2010
- Posts
- 3
- Rep Power
- 0
That's MPair
Java Code:public class MPair implements Map.Entry, Comparable { private Object key, value; public MPair(Object k, Object v) { key = k; value = v; } public Object getKey() { return key; } public Object getValue() { return value; } public Object setValue(Object v) { Object result = value; value = v; return result; } public boolean equals(Object o) { return key.equals(((MPair)o).key); } public int hashCode() { int hash = 7; hash = 17 * hash + (this.key != null ? this.key.hashCode() : 0); return hash; } public int compareTo(Object rv) { return ((Comparable)key).compareTo(((MPair)rv).key); } }
- 12-14-2010, 09:27 AM #5
Member
- Join Date
- Dec 2010
- Posts
- 3
- Rep Power
- 0
I added clear() method to SlowMap
it works, when i call map.clear but when I do operations with set it still doesn't workJava Code:@Override public void clear(){ keys.clear(); values.clear(); }
I understand I should additionly implement remove() method for entrySet.iterator, but have no idea where to do that...
Similar Threads
-
Maps and Sets
By darkblue24 in forum New To JavaReplies: 19Last Post: 03-25-2010, 06:13 PM -
Maps and Sets
By RedKMan in forum New To JavaReplies: 3Last Post: 02-16-2010, 09:36 AM -
Comparing List of maps
By thorne_ in forum New To JavaReplies: 1Last Post: 06-10-2009, 02:30 AM -
Maps
By natep67 in forum New To JavaReplies: 8Last Post: 05-06-2009, 03:59 AM -
Google Maps API
By mew in forum New To JavaReplies: 0Last Post: 12-26-2007, 10:28 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks