Articles

List vs map in java

List vs map in java

Here we will learn in detail about difference between list and map in java with proper examples.
 
List vs 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.

Example of List

Given below is a simple example on list (i.e arraylist)

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

public class SimpleArrayList {

      public static void main(String[] args) {
            //create Array List
            List

OUTPUT

[Java, Spring, Hibernate, EJB]

 

Example of Map

Given below is a simple example of hashmap in java:

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

OUTPUT

w 2
x 1

 


Java Interview Questions

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Rohanjit Kumar
Technology geek, loves to write and share knowledge with the world. Having 9+ years of IT experience. B.Tech in Computer Science & Engineering
Page Views :    Published Date : Jun 28,2020  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!