Title: MyMap.java
Course: CPS 261: Advanced Java Concepts ~ DL & MM MASTER

MyMap.java


import java.util.*;

class DataRec
{
    Object key;
    Object value;
    DataRec next=null;
    DataRec (Object key, Object value)
    {
        this.key =key;
        this.value = value;
    }
}

class MyMap implements Map
{
    int MyBucketSize = 1;
    DataRec[] hashBucket = new DataRec[MyBucketSize];

    public Object get(Object keyIn)
    {
        int hashValue =keyIn.hashCode();
        int index = hashValue % MyBucketSize;
        DataRec d = hashBucket[index];
        if (d == null) return null;

        do
        {
            if (keyIn.equals(d.key))
            {
                return d.value;
            }
            d = d.next;

        } while (d!=null);
        return null;
    }

    public Object put(Object keyIn, Object value)
    {
        int hashValue =keyIn.hashCode();
        int index = hashValue % MyBucketSize;
        DataRec d = hashBucket[index];
        if (d == null)
        {
            try
            {
            hashBucket[index] = new DataRec(keyIn,value);
            }
            catch (Exception e)
            {
                System.out.println("Caught Exception keyIn=" +keyIn +
                    " value="+ value);
                System.out.println("Exception = " +e);
                e.printStackTrace();
              //  throw e;
            }
            return null;
        }

        DataRec dlast = null;
        do
        {
            if (keyIn.equals(d.key))
            {
               // Found key
               DataRec d2 = new DataRec(keyIn, value);
               d2.next = d.next;
               if (dlast != null)
                    dlast.next = d2;
               else
               {
                    hashBucket[index] = d2;
               }
               return d;
            }
            dlast = d;
            d = d.next;

        } while (d!=null);

        // Key Not found
        d = new DataRec(keyIn, value);
        d.next = hashBucket[index];
        hashBucket[index] =d;

        return null;
    }


    public void clear(){}

    public boolean containsKey(Object key){return false;}

    public boolean containsValue(Object value) {return false;}

    public Set entrySet() {return null;}

    public boolean equals(Object o){return false;}


    public int hashCode() {return 0;}

    public boolean isEmpty() {return false;}

    public Set keySet()
    {
        Set s = new HashSet();
        DataRec d;
        for (int i=0; i < MyBucketSize; i++)
        {
            d = hashBucket[i];
            if (d == null) continue;
            do
            {
                s.add(d.key);
                d = d.next;
            } while (d!=null);
        }
        return s;
    }

    public void putAll(Map t) {}

    public Object remove(Object key){return null;}

    public int size(){return 0;}

    public Collection values(){return null;}

    public static void  main(String[] args)
    {
        MyMap hm = new MyMap();

        hm.put("George", "Flintstone");
        hm.put("Fred", "Flintstone");
        hm.put("Sam", "Hasselbach");

        String fetch = (String)hm.get("Fred");
        System.out.println("Fetched " + fetch);
    }
}