Hello need help implementing the backing set for this map Th

Hello, need help implementing the \"backing\" set for this map. This set doesn\'t have its own data structure, it uses the data structure of the map. The details are commented within code.

public class TreeMap<K,V> extends AbstractMap<K,V> {

   // Here is the data structure to use.
  
   private static class Node<K,V> extends AbstractEntry<K,V> {
       Node<K,V> left, right;
       Node<K,V> parent;
       Node(K k, V v) {
           super(k,v);
           parent = left = right = null;
       }
   }
  
   private Comparator<K> comparator;
   private Node<K,V> dummy;
   private int numItems = 0;
   private int version = 0;

   private class EntrySet extends AbstractSet<Entry<K,V>> {

       @Override
       public int size() {
           // TODO: delegate to TreeMap.size()

       }

       @Override
       public Iterator<Entry<K, V>> iterator() {
           return new MyIterator();
       }
      
      
       @Override
       public boolean contains(Object o) {
           // TODO if o is not an entry (instanceof Entry<?,?>), return false
           // Otherwise, check the entry for this entry\'s key.
           // If there is no such entry return false;
           // Otherwise return whether the entries match (use the equals method of the Node class).
       }

       @Override
       public boolean remove(Object x) {
           // TODO: if the tree doesn\'t contain x, return false
           // otherwise do a TreeMap remove.
           // make sure that the invariant is true before returning.
       }
      
       @Override
       public void clear() {
           // TODO: delegate to the TreeMap.clear()
       }
   }

Solution

private static class SetFromMap<E> extends AbstractSet<E>

    implements Set<E>, Serializable

{

    private final Map<E, Boolean> m; // The backing map

    private transient Set<E> s;       // Its keySet

    SetFromMap(Map<E, Boolean> map) {

        if (!map.isEmpty())

            throw new IllegalArgumentException(\"Map is non-empty\");

        m = map;

        s = map.keySet();

    }

    public void clear()               {        m.clear(); }

    public int size()                 { return m.size(); }

    public boolean isEmpty()          { return m.isEmpty(); }

    public boolean contains(Object o) { return m.containsKey(o); }

    public boolean remove(Object o)   { return m.remove(o) != null; }

    public boolean add(E e) { return m.put(e, Boolean.TRUE) == null; }

    public Iterator<E> iterator()     { return s.iterator(); }

    public Object[] toArray()         { return s.toArray(); }

    public <T> T[] toArray(T[] a)     { return s.toArray(a); }

    public String toString()          { return s.toString(); }

    public int hashCode()             { return s.hashCode(); }

    public boolean equals(Object o)   { return o == this || s.equals(o); }

    public boolean containsAll(Collection<?> c) {return s.containsAll(c);}

    public boolean removeAll(Collection<?> c)   {return s.removeAll(c);}

    public boolean retainAll(Collection<?> c)   {return s.retainAll(c);}

    // addAll is the only inherited implementation

    private static final long serialVersionUID = 2454657854757543876L;

    private void readObject(java.io.ObjectInputStream stream)

        throws IOException, ClassNotFoundException

    {

        stream.defaultReadObject();

        s = m.keySet();

    }

}

Hello, need help implementing the \
Hello, need help implementing the \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site