Wednesday 21 December 2016

Difference : List Vs Map

List and Map differ based on following parameters:

1.      Approch
List stores only the value of the element.
Map applies different mechanism as it stores value of the element along with a unique key associated to the each stored value.

2.      Memory requirement
As List only stores the value of the element so it requires less memory space in comparison to Map.
On other hand Map needs to store a unique key for each value so memory requirement increases.

3.      Implementation
ArrayList, LinkedList and Vector implements List interface.
HashMap, LinkedHashMap, TreeMap, etc. implements Map interface.

4.      Order maintainence
List ensures that the insertion order is maintained.
All Map implementations doesn’t guarantee of maintaining insertion order (if you care about order, useLinkedHashMap).
*In case where insertion order is maintained we get the elements returned in same order in which they were inserted while retrieving them.

5.      Null value
Any number of null values can be stored in List.
Only one null key is allowed in Map. But you can store any number of null values.

6.      Duplicate value
List accepts duplicate values while storing.
Map doesn’t accept duplicate Key. But duplicate values are allowed.

7.      Retrieve element
In List elements can be retrieved specifying index of the element.
In Map elements can be retrieved by specifying its Key.

8.      Example
List Example


package javaRadarArrayList;

import java.util.ArrayList;
import java.util.List;

public class SimpleArrayList {

      public static void main(String[] args) {
            //create Array List
            List<String> javaRadarList = new ArrayList<String>();
            //Add elements to ArrayList
            javaRadarList.add("Java");
            javaRadarList.add("Spring");
            javaRadarList.add("Hibernate");
            javaRadarList.add("EJB");

            System.out.println(javaRadarList);
      }
}

OUTPUT:

[Java, Spring, Hibernate, EJB]


Map Example

package map;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class MapExample {

        public static void main(String[] args) {

                        Map m = new HashMap<String, String>();
                        m.put("x", "1");
                        m.put("w", "2");
                       
                        Set set = m.entrySet();
                        Iterator itr = set.iterator();
                       
                        while(itr.hasNext()){
                                        Map.Entry entry = (Map.Entry) itr.next();
                                        System.out.println(entry.getKey() + " "+ entry.getValue());
                        }

        }

}

OUTPUT:
w 2
x 1



You may also like to read:

No comments:

Post a Comment

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
If you are looking for a reference book on java then we recommend you to go for → Java The Complete Reference
Click on the image link below to get it now.