Points to Note

  1. ArrayList allows NULL values many times
  2. Set and HashMap allows NULL once
  3. In HashMap the values will be overwritten when keys are same

List An ordered collection (also known as a sequence). The user of this interface has precise control over where in the list each element is inserted. The user can access elements by their integer index (position in the list), and search for elements in the list.

MapAn object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value.

Test.java

public class Test
{
 public static void main(String[] args) 
 {
 List<String> arrNames = new ArrayList<String>();
 
 arrNames.add("Hello");
 arrNames.add(null);
 arrNames.add(null);
 arrNames.add("Hi"); 
 System.out.println("List - "+arrNames.size());
 
 Map hmTest = new HashMap();
 hmTest.put("1", "mugil");
 hmTest.put(null, null);
 hmTest.put(null, null);
 hmTest.put("1", "vannan");
 System.out.println("HashMap - "+ hmTest.size());
 
 Set<Integer> setTest = new HashSet<Integer>();
 
 setTest.add(new Integer("10"));
 setTest.add(new Integer("20"));
 setTest.add(new Integer("10"));
 
 System.out.println("Set - "+ setTest.size());
 }
}

Output

List - 4
HashMap - 2
Set - 2

Comments are closed.