0

I am new to Java Collections. I was going through Map, so please tell me just like java have provided the Map, can we also make our own Map? Let's say a Map named MineMap. Please advise me how to achieve this. I was doing googling, and I discovered something like this:

public interface MyMap
{
    public void put(Object key,Object value);
    public Object get(Object key);
    public int size();
    public Set keySet();
    public Set entrySet();
    public interface MyEntry
    {
        public Object getKey();
        public Object getValue();
    }
}

and its implementation:

class MySimpleMap implements MyMap
{
    private ArrayList keys;
    private ArrayList values;
    private int index;

    public MySimpleMap()
    {
        keys=new ArrayList();
        values=new ArrayList();
        index=0;
    }  

    public void put(Object key,Object value)
    {
        keys.add(key);
        values.add(value);
        index++;
    }

    public Object get(Object key)
    {
        int i=keys.indexOf(key);
        if (i>=0)
            return values.get(i);
        else
            return null;
    }

    public int size()
    { 
        return index;
    }

    public Set keySet()
    {
       HashSet set=new HashSet();
       set.addAll(keys);
       return set;
    }

    //Nested class starts...
    class MySimpleEntry implements MyMap.MyEntry
    {
       Object key;
       Object value;

       public MySimpleEntry(Object k,Object v)
       {
           key=k;
           value=v;
       }

      public Object getKey()
      {
          return key;
      }

      public Object getValue()
      {
          return value;
      }
 }// Nested class ends.

 public Set entrySet()
 {
     HashSet set=new HashSet();
     for (int i=0;i<index;i++)
     {
        Object k=keys.get(i);
        Object v=values.get(i);
        MySimpleEntry temp=new MySimpleEntry(k,v);
        set.add(temp);
     }
     return set;
 }
 }

and finally this was the class that was using this:

class MyMapDemo
{
    public static void main(String arr[])
    {
        MySimpleMap map=new MySimpleMap();
        map.put("Amit","Java");
        map.put("Rahul",".Net");
        map.put("Nitin","SQT");
        map.put("Ajay","PHP");
        map.put("Raman","Java");
        System.out.println("There are "+map.size()+" elemenets in the map...");
        System.out.println("contents of Map...");
        Set s=map.entrySet();
        Iterator itr=s.iterator();
        while(itr.hasNext())
        {
            MyMap.MyEntry m=(MyMap.MyEntry) itr.next();
            System.out.println(m.getKey()+"\t"+m.getValue());
        }
        Scanner in=new Scanner(System.in);
        System.out.println("Enter Name to find out course, ctrl+c to terminate...");
        while(true)
        {
            System.out.println("Name:");
            String n=in.nextLine();
            System.out.println("Course is:"+map.get(n));
        }
 }
}

but I need some implemenation that would be simpler.

2
  • 1
    Why and how simpler? What do you actually need this for? Commented Apr 17, 2012 at 3:23
  • Simpler? Your implementation is actually too simple, for example, it's missing a uniqueness check on the keys. Commented Apr 17, 2012 at 3:25

1 Answer 1

6

You mentioned that you are beginner and want to implement your own map. At beginner level it's too early to write your Map. Collection framework has already given optimized and proven implementation of several data types. It's important to understand those instead of reinventing wheel.

  • You can get java library sources easily and try to see how they have implemented collection classes. It's state of the art development and covers many minute details.

  • Regarding implementation.

However, what ever you have found can not be classified as map directly. No doubt does similar thing.

  • not generic implementation.
  • not implementing Map so can to use impl vice-versa with existing Map type variables.
  • can not use algorithms from the Collections.class.

You may want to write implementation such as to include above features.

// Hash map java library defined as follows.
public class HashMap<K,V>
    extends AbstractMap<K,V>
    implements Map<K,V>, Cloneable, Serializable


// Hence, you may want to write this.
public class MyMap<K,V>
    extends AbstractMap<K, V> // May skip this
    implements Map<K,V>, Cloneable, Serializable // important
{
   //Now it will ask to write all the methods which are defined in Map interface.
   //These are minimum methods required for Map operations.
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.