Difference between List and Set and Map
List,Set,Map are the basic collection interfaces, although Map doesn't inherit Collection Interface, it belongs to the Collection in Java.
What is List in Java?
List implements the Collection<E>, Iterable<E> Interfaces.
public interface List<E>extends Collection<E>.
List in Java allows to store objects by storing them in each index. Other than Set List allows Duplication of elements.To store an element in List use the add() method and to get a value use get() method.
Some Classes implementing List Interface are ArrayList,LinkedList etc.
List throws these Exceptions:
UnsupportedOperationException
ClassCastException
NullPointerException
IllegalArgumentException
IndexOutOfBoundsException
Example:
package com.jobberindia;
import java.util.*;
public class HelloWorld {
public static void main(String[] args) {
// TODO Auto-generated method stub
List<String> list = new ArrayList<String>();
list.add("Hello");
list.add("World");
list.add("Hello1");
list.add("World2");
System.out.println("Data stored in mainlist "+list);
System.out.println("Size of list before removing "+list.size());
ArrayList<String> subList = new ArrayList<String>(list.subList(1, 2));
System.out.println("Data stored in sublist "+subList);
System.out.println("Size of list before removing "+subList.size());
}
}
What is Set in Java?
Set implements the Collection<E>, Iterable<E> Interfaces.
public interface Set<E>
Set in Java allows to store objects by storing them in each index. Other than Set Set doesn't allows Duplication of elements.To store an element in Set use the add() method.
Example:
package com.jobberindia;
import java.util.*;
public class HelloWorld {
public static void main(String[] args) {
Set<String> set = new TreeSet<String>();
set.add("Hello");
set.add("World");
set.add("Hello1");
set.add("World2");
set.add("Hello3");
set.add("World4");
System.out.println("Data stored in mainlist "+set);
}
}
Some Classes implementing List Interface are TreeSet,HashSet etc.
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("Hello1");list.add("World");Map<Integer,String> map = new HashMap<Ineger,String>();list.add("World2");map.put(i, list.get(i));for(int i=0;i<list.size();i++){ }}System.out.println("Data stored in mainlist "+map.get(2)); }Difference between List VS Set VS Map Interface:
- List allows duplicate values.
- List allows any number of null values.
- Set doesn't allows duplicate values.
- Set allows one null value.
- Map allows one null key value.
- List maintains the insertion order.
- Set and Map doesn't maintain the insertion order.
- Map stores key and value pair.
Based on your usage select these interfaces carefully.
No comments:
Post a Comment