Home » Java programming language

Differences between List and Map interface in Java

Set vs Map interface in Java: Here, we are going to learn what are the differences between Set and Map interface in Java programming language?
Submitted by Preeti Jain, on August 07, 2019

List vs Map interface

Here, we will see how List differs from Map interface in Java and we will see the points given below,

List interface

  • List is an interface that is defined in java.util package.
  • List is the data structure in Java.
  • List object is represented in the form of values.
  • The performance of List interface is low as compare to Map interface.
  • The implementation class of List interface is ArrayList, LinkedList, and Vector and Stack, etc.
  • List is not different from Collection or in other words, there is a relation between List and Collection (i.e. It is a child interface of Collection interface because List implements Collection interface).
  • List does not provide uniqueness (i.e. Duplicates are allowed for values or we can insert one object multiple times).
  • If we want to represent a group of individual objects where “insertion order is preserved” (i.e. the order of insertion is must be same as the order of retrieval).
  • We should go for List if we want to represent a group of the object as a single entity.
  • List is meant for a group of the individual object.

Example:

Let suppose we have a List with few elements. Here we are adding the elements in the order is [10,20,30,50, null,30] and if we are retrieving the elements so the order of retrieving elements must be the same (i.e. it is needed to be the same insertion and retrieval order of the elements.) so the output will be the same and the order will be like [10,20,30, null,30].

// Java program to demonstrate the behavior of List interface

import java.util.*;

class ListInterface {
    public static void main(String[] args) {
        // Creating an instance
        List list = new ArrayList();

        // By using add() method to add an elements
        list.add(10);
        list.add(20);
        list.add(30);
        list.add(50);
        list.add(null);

        // if we add again 30 then we will not get any error 
        // because duplicate element is allowed
        list.add(30);

        // Display List elements
        System.out.println("Retrieval order of the elements in List is :" + list);
    }
}

Output

E:\Programs>javac ListInterface.java

E:\Programs>java ListInterface
Retrieval order of the elements in List is :[10, 20, 30, 50, null, 30]

Now, we will see how Map differs from List interface in Java and we will see the points given below,

Map interface

  • The Map is an interface that is defined in java.util package.
  • The Map is the data structure in Java.
  • The Map is based on Hashing and The Map object is represented in the form of key-value pairs and the key-value pairs are called entry.
  • The performance of Map interface is high as compare to Set interface.
  • In the case of Map interface, there is no collision concept if we know keys.
  • The implementation class of Map interface is HashMap, LinkedHashMap, and ConcurrentHashMap, etc.
  • The Map is different from Collection or in other words, there is no relation between Map and Collection (i.e. It is not a child interface of Collection interface because Map does not implement Collection interface).
  • The Map does not provide uniqueness fully (i.e. Duplicates are not allowed for Keys and Duplicates are allowed for values).
  • We should go for Map if we want to represent a group of the object as key-value pairs.
  • The Map is meant for a group of key-value pairs.

Example:

Let suppose we have a Map with few elements. Here we are adding the elements in the order is {Java=1000, C=2000, C++=3000, Ruby=4000, Python=1000,null=null, Django=null, null=7000} and if we are retrieving the elements so the order of retrieving elements can be different (i.e. insertion order is not preserved and it is not needed to be the same insertion and retrieval order of the elements.) so the output will be different and the order will be like {Ruby=4000, C=2000, Django=null, Python=1000, C++=3000, null=7000, Java=1000}

// Java program to demonstrate the behavior of Map

import java.util.Collection;
import java.util.Map;
import java.util.HashMap;

class MapClass {
    public static void main(String[] args) {
        // Creating an instance of HashMap
        Map map = new HashMap();

        //By using put() method to add some values in Map
        map.put("Java", 1000);
        map.put("C", 2000);
        map.put("C++", 3000);
        map.put("Ruby", 4000);
        map.put("Python", 1000);
        map.put("null", null);
        map.put("Django", null);

        /* Here we will not get any error but one null is accepted for keys*/
        map.put("null", 7000);

        // Display retrieval order of Map
        System.out.println("Current Map list is :" + map);

        // by using values() to find values of Map
        Collection values = map.values();

        // Display Values of Map
        System.out.println("Current Map Key values is :" + values);
    }
}

Output

E:\Programs>javac MapClass.java

E:\Programs>java MapClass
Current Map list is :{Ruby=4000, C=2000, Django=null, 
Python=1000, C++=3000, null=7000, Java=1000}
Current Map Key values is :[4000, 2000, null, 1000, 3000, 7000, 1000]


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.