Enter your E-mail Address below for Free E-mail Alerts right Into your Inbox: -

Showing posts with label LinkedHashMap. Show all posts
Showing posts with label LinkedHashMap. Show all posts

Wednesday

What is Map in Java?

What is Map in Java?

Other than List and Set Map is used to Store Key and Value Pair.The key is unique and values are duplicate allowed. Map implement the collection Interface. It allows only one null value which is stored in the zeroth index.

These are the some of the classes implements the Map<K,V> Interface.  HashMap, Hashtable, IdentityHashMap, LinkedHashMap.

Example:

public class HelloWorld {

public static void main(String[] args) {
List<String> list = new ArrayList<String>();
       list.add("Hello");
       list.add("World");
       list.add("Hello1");
       list.add("World2");
       Map<Integer,String> map = new HashMap<Ineger,String>();
     
       for(int i=0;i<list.size();i++)
       {
       map.put(i, list.get(i));
       }
     
        System.out.println("Data stored in mainlist "+map.get(2));

}


}

Monday

Difference between HasMap and LinkedHashMap in java

Difference between HasMap and LinkedHashMap in java

HashMap and LinkedHashMap are the two most commonly used Map interface in java. Both are not synchronized.
Here is the basic difference between HashMap and LinkedHashMap.

  1. HashMap doesn't maintains the order, while the LinkedHashMap maintains the order of insertion.
  2. LinkedHahMap consumes more memory than LinkedHashMap.
  3. LinkedHashMap implement Map interface but extends HashMap.
Here are some of the similarities between these.

  1. Both are subject to race condition because both are not synchronized.
  2. Both are fail-fast.