Array Map extends AbstractMap : HashTable Map : Collections Data Structure : Java examples (example source code) Organized by topic

Java
C++
PHP
Java Home »  Collections Data Structure   » [  HashTable Map  ]  Screenshots 
 



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 (!(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 : key.hashCode());
      int valueHash = (value==null : 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.  Associates keys with values Associates keys with values
2.  A simple Map implementation A simple Map implementation
3.  Hash table with separate chaining
4.  Hash table with linear probing Hash table with linear probing
5.  Hash table with double hashing Hash table with double hashing
6.  Working with Key-Value Pairs in a Hashtable
7.  Demonstrate the Hashtable class, and an Enumeration
8.  Demonstrate the HashMap class, and an Iterator Demonstrate the HashMap class, and an Iterator
9.  Soft HashMap
10.  MultiMap extends AbstractMap
11.  Demonstrating the WeakHashMap Demonstrating the WeakHashMap
12.  Use treemap Use treemap
13.  Sorting Elements in a TreeMap Sorting Elements in a TreeMap
14.  What you can do with a TreeMap What you can do with a TreeMap
15.  A Map implemented with ArrayLists
16.  Simple demonstration of HashMap Simple demonstration of HashMap








Home| Contact Us
Copyright 2003 - 04 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.