Class HashMap


  • public class HashMap
    extends java.lang.Object
    My implementation of a JDK 1.2 Map. I do not use synchronization, so be careful in a threaded environment. I also do not specifically "implements" java.util.Map, since support for JDK 1.1 is needed.
    Version:
    $Revision: 3633 $ $Date: 2003-03-01 08:38:44 +0100 (Sat, 01 Mar 2003) $
    Author:
    Keith Visco
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int DEFAULT_SIZE
      The default number of buckets in this Map
    • Constructor Summary

      Constructors 
      Constructor Description
      HashMap()
      Creates a new HashMap with the default number of buckets
      HashMap​(int size)
      Creates a new HashMap with the given number of buckets.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()
      Removes all entries from this Map
      boolean containsKey​(java.lang.Object key)
      Returns true if the given object is a key contained in this Map
      boolean containsValue​(java.lang.Object value)
      Returns true if the given object is a value contained in this Map
      Note: Depending on the size of the Map, this could be a slow operation.
      Iterator entries()
      Returns an interator for the entries of this Map.
      boolean equals​(java.lang.Object object)
      Returns true if the given Object is a HashMap which contains equivalent HashMap entries as this HashMap.
      java.lang.Object get​(java.lang.Object key)
      Returns the value associated with the given key
      int hashCode()
      Returns the hashCode for this Map.
      boolean isEmpty()
      Returns true if this map contains no entries
      Iterator keys()  
      void put​(java.lang.Object key, java.lang.Object value)
      Associated the specified value with the given key in this Map
      java.lang.Object remove​(java.lang.Object key)
      Removes the association with the given Key in the Map.
      int size()
      Returns the number of associations in the Map
      • Methods inherited from class java.lang.Object

        getClass, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • DEFAULT_SIZE

        public static final int DEFAULT_SIZE
        The default number of buckets in this Map
        See Also:
        Constant Field Values
    • Constructor Detail

      • HashMap

        public HashMap()
        Creates a new HashMap with the default number of buckets
      • HashMap

        public HashMap​(int size)
        Creates a new HashMap with the given number of buckets.
        Parameters:
        size - , the number of buckets to use, this value must be a non-zero positive integer, if not the default size will be used.
        Note:The number of buckets is not the same as the allocating space for a desired number of entries. If fact, if you know number of entries that will be in the hash, you might want to use a HashMap with a different implementation. This Map is implemented using separate chaining.
    • Method Detail

      • clear

        public void clear()
        Removes all entries from this Map
      • containsKey

        public boolean containsKey​(java.lang.Object key)
        Returns true if the given object is a key contained in this Map
        Returns:
        true if the given object is a key contained in this Map
      • containsValue

        public boolean containsValue​(java.lang.Object value)
        Returns true if the given object is a value contained in this Map
        Note: Depending on the size of the Map, this could be a slow operation. If you know the key an object would be associated with, contains key would be much faster, or simply do (get(key) != null).
        Returns:
        true if the given object is a value contained in this Map
      • entries

        public Iterator entries()
        Returns an interator for the entries of this Map. Each element returned by a call to Iterator#next() is a Map.Entry.
        Note: This is different than a JDK 1.2 Map because I didn't want to deal with implementing Sets at this point.
        Returns:
        an Iterator for the entries of this Map.
      • equals

        public boolean equals​(java.lang.Object object)
        Returns true if the given Object is a HashMap which contains equivalent HashMap entries as this HashMap.
        Overrides:
        equals in class java.lang.Object
        Returns:
        true if the given Object is a HashMap, and is equivalent to this Map
        I will be probably make an interface for Map, to allow comparisons with different Map implemenations.
      • get

        public java.lang.Object get​(java.lang.Object key)
        Returns the value associated with the given key
        Returns:
        the value associated with the given key
      • hashCode

        public int hashCode()
        Returns the hashCode for this Map. The hash code is the sum of all the hash codes of each entry in the map
        Overrides:
        hashCode in class java.lang.Object
      • isEmpty

        public boolean isEmpty()
        Returns true if this map contains no entries
        Returns:
        true if this map contains no entries
      • put

        public void put​(java.lang.Object key,
                        java.lang.Object value)
        Associated the specified value with the given key in this Map
        Parameters:
        key - the object to associate with the given value
        value - the object to add an association in this Map
      • remove

        public java.lang.Object remove​(java.lang.Object key)
        Removes the association with the given Key in the Map.
        Parameters:
        key - the object key to remove the association for
        Returns:
        the associated value being removed from this Map
      • size

        public int size()
        Returns the number of associations in the Map
        Returns:
        the number of associations in the Map