Results 1 to 5 of 5

Thread: Help with Maps

  1. #1
    snripa is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

    Default Help with Maps

    I need help with maps (Thinking inJava 3-d ed., 11#34).
    I have my class:

    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);
    
      }
    }
    It doesn't work when i want to delete smth or deleteAll. (There are no errors but nothing happens when I use map.clear)
    How should I modify this?

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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 06:55 PM.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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.

  4. #4
    snripa is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

    Default

    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);
      }
    }

  5. #5
    snripa is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

    Default

    I added clear() method to SlowMap
    Java Code:
    @Override
        public void clear(){
        keys.clear();
        values.clear();
        }
    it works, when i call map.clear but when I do operations with set it still doesn't work

    I understand I should additionly implement remove() method for entrySet.iterator, but have no idea where to do that...

Similar Threads

  1. Maps and Sets
    By darkblue24 in forum New To Java
    Replies: 19
    Last Post: 03-25-2010, 07:13 PM
  2. Maps and Sets
    By RedKMan in forum New To Java
    Replies: 3
    Last Post: 02-16-2010, 10:36 AM
  3. Comparing List of maps
    By thorne_ in forum New To Java
    Replies: 1
    Last Post: 06-10-2009, 03:30 AM
  4. Maps
    By natep67 in forum New To Java
    Replies: 8
    Last Post: 05-06-2009, 04:59 AM
  5. Google Maps API
    By mew in forum New To Java
    Replies: 0
    Last Post: 12-26-2007, 11:28 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •