Tuesday, January 18, 2011

Where to use MAP and where not

Map is a collection object which contains a key - value pair. Key is unique for identification and Value is the actual object itself. For example to store employees backed with their IDs.
  • 001 - Employee One
  • 002 - Employee Two
  • 003 - Employee Three
Point I will be stressing is the uniqueness of the Key in a Map. Keys inside a java.util.Map  will be stored as a java.util.Set no matter what implementation it is. Either java.util.HashMap or java.util.LinkedHashMap (where sequence or retrieval is retained). If we are adding objects to a Map using map.put(key,value) and retrieving using key by map.get(key), then yes we are using the Map.

Let me justify it futher. When using map.put(), map will check if there is any java.util.HashMap.Entry with the key passed. If so then it will replace the value with a new one and returns the previous Value. But the process of checking of unique key is complex. it involves method like key.equals() and key.hashcode(). Imagine if we have 100 objects to be added to a map, this comparison is done that many times irrespective if there is already an entry or not.
So, when we really think that we need to get benefited by the java.util.Set (inside the Map) then yes, usage of Map is encouraged. Many times, we do not want the feature of unique keys, we know that the Map will be populated with unique data but I want to retrieve objects by their keys, for example get Employee object with the help of an employee ID or java.util.Properties would be the best example for retrieval by keys. Here we use map.get() extensively. In this scenario there is no option but to going for Map unless you have written your own framework to retreive objects by key without using a Map.
If we have a scenario where we just use flat iteration over a map where we populate map by unique data or we don't consider uniqueness and we do not use map.get() even. We just iterate over all entries and use the data from both key and value to do some kind of processing then I would not prefer java.util.Map. Yes, there is an alternate which follows.
I would use any simple Collection object like java.util.List of type like EntryData and iterate over it.
            List<EntryData> data = new ArrayList<EntryData>(1);
            data.add(new EntryData("Some key", "Some vallue"));
            for(EntryData entry: data){
                processKey(entry.key);
                processValue(entry.value);
            }

in this way, I am preparing data which has a pair of key and value. But without using a features of map.get(key) method or I would say any feature of java.util.Map. I do not have a best example right now to describe where this could actually be used in real time. But believe me we do need this. I needed this kind of approach recently and figured out that usage of java.util.Map is not always a best practice in terms of performance.

EntryData.java
    private static class EntryData {
        final Object key,value;

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




No comments:

Post a Comment

Was this article useful?