Array Map extends AbstractMap : HashTable Map « Collections Data Structure « Java






Array Map extends AbstractMap

Array Map extends AbstractMap
     
import java.io.Serializable;
import java.util.*;

public class ArrayMap extends AbstractMap
    implements Cloneable, Serializable {

  static class Entry implements Map.Entry {
    protected Object key, value;

    public Entry(Object key, Object value) {
      this.key = key;
      this.value = value;
    }

    public Object getKey() {
      return key;
    }

    public Object getValue() {
      return value;
    }

    public Object setValue(Object newValue) {
      Object oldValue = value;
      value = newValue;
      return oldValue;
    }

    public boolean equals(Object o) {
      if (!(o instanceof Map.Entry)) {
        return false;
      }
      Map.Entry e = (Map.Entry)o;
      return (key==null ? e.getKey()==null : key.equals(e.getKey())) &&
        (value==null ? e.getValue()==null : value.equals(e.getValue()));
    }

    public int hashCode() {
      int keyHash = (key==null ? 0 : key.hashCode());
      int valueHash = (value==null ? 0 : value.hashCode());
      return keyHash ^ valueHash;
    }

    public String toString() {
      return key + "=" + value;
    }
  }

  private Set entries = null;
  private ArrayList list;

  public ArrayMap() {
    list = new ArrayList();
  }

  public ArrayMap(Map map) {
    list = new ArrayList();
    putAll(map);
  }

  public ArrayMap(int initialCapacity) {
    list = new ArrayList(initialCapacity);
  }

  public Set entrySet() {
    if (entries==null) {
      entries = new AbstractSet() {
        public void clear() {
          list.clear();
        }
        public Iterator iterator() {
          return list.iterator();
        }
        public int size() {
          return list.size();
        }
      };
    }
    return entries;
  }

  public Object put(Object key, Object value) {
    int size = list.size();
    Entry entry = null;
    int i;
    if (key==null) {
      for (i=0; i<size; i++) {
        entry = (Entry)(list.get(i));
        if (entry.getKey() == null) {
          break;
        }
      }
    } else {
      for (i=0; i<size; i++) {
        entry = (Entry)(list.get(i));
        if (key.equals(entry.getKey())) {
          break;
        }
      }
    }
    Object oldValue = null;
    if (i<size) {
      oldValue = entry.getValue();
      entry.setValue(value);
    } else {
      list.add(new Entry(key, value));
    }
    return oldValue;
  }

  public Object clone() {
    return new ArrayMap(this);
  }

}




           
         
    
    
    
    
  








Related examples in the same category

1.Check if a particular key exists in Java Hashtable
2.Check if a particular value exists in Java Hashtable
3.Get Collection of Values from Java Hashtable
4.Get Set view of Keys from Java Hashtable
5.Get Size of Java Hashtable
6.Iterate through keys of Java Hashtable
7.Remove all values from Java Hashtable
8.Scan the content of a hashtable
9.Remove value from Java Hashtable
10.Sort keys in an Hashtable
11.Associates keys with valuesAssociates keys with values
12.Iterate through values of Java Hashtable
13.A simple Map implementationA simple Map implementation
14.Hash table with separate chaining
15.Hash table with linear probingHash table with linear probing
16.Hash table with double hashingHash table with double hashing
17.Working with Key-Value Pairs in a Hashtable
18.Demonstrate the Hashtable class, and an Enumeration
19.Demonstrate the HashMap class, and an IteratorDemonstrate the HashMap class, and an Iterator
20.Soft HashMap
21.MultiMap extends AbstractMap
22.Demonstrating the WeakHashMapDemonstrating the WeakHashMap
23.Use treemapUse treemap
24.Sorting Elements in a TreeMapSorting Elements in a TreeMap
25.What you can do with a TreeMapWhat you can do with a TreeMap
26.A Map implemented with ArrayLists
27.Simple demonstration of HashMapSimple demonstration of HashMap
28.HashMap
29.Caching Hashtable
30.Hashtable that supports mostly-concurrent reading, but exclusive writing.
31.Lru Hashtable
32.Bucketized Hashtable
33.Custom hash table based on customized array