Collections

Collections are like containers that groups multiple items in a single unit.

Example: a jar of chocolates, list of names etc.

Collections are used almost in every programming language and when Java arrived, it also came with few Collection classes like List, Set, Maps.

In this session, I will cover ArrayList and HashMap which are commonly used in selenium automation testing.

1) ArrayList:
  1. Java ArrayList class uses a dynamic array for storing the elements.
  2. It extends AbstractList class and implements List interface.
  3. Java ArrayList class can contain duplicate elements.
  4. Java ArrayList class maintains insertion order.
  5. Java ArrayList class is non synchronized.
  6. Java ArrayList allows random access because array works at the index basis.
ArrayList in Java

Syntax:
ArrayList<String> al=new ArrayList<String>();

Example:
In following example, I have described how to created arraylist object, how to add values in arraylist, how to get index of arraylist item, how to remove arraylist item, how to reset arraylist item and how to print items.

import java.util.*;  
class CollectionDemo{  
 public static void main(String args[]){  
  ArrayList<String> myList=new ArrayList<String>();//creating arraylist  
  myList.add("Pune");//adding object in arraylist  
  myList.add("Mumbai");  
  myList.add("Delhi");  
  myList.add("Hyderabad");  

   //To get the Index of an Item from arraylist.
  System.out.println(myList.indexOf("Delhi")); 

   //To remove an Item from arraylist.
  myList.remove(1);

   //To reset value of an arraylist item. 
  myList.set(2, "Bangalore");
  
  Iterator<String> itr=myList.iterator();  //getting Iterator from arraylist to traverse elements  
    
  //Printing each elements using Iterator
  while(itr.hasNext()){  
   System.out.println(itr.next());  
  }  
  
  //Printing Element with for each loop
  for(String obj:myList)  
         System.out.println(obj);  
  }  



2) Hashmap:
  1. A HashMap contains values based on the key. It implements the Map interface and extends AbstractMap class.
  2. It contains only unique elements.
  3. It may have one null key and multiple null values.
  4. It maintains no order.
Java HashMap Key-Value pairs

Syntax:
HashMap<Integer,String> map=new HashMap<Integer,String>(); 

Example:
In following example I have described how to create hashmap object, how to add and remove values, How to get size of hashmap, How to clear the hashmap, etc.

class CollectionDemo{  
 
public static void main(String args[]){  
  
 HashMap<Integer,String> map=new HashMap<Integer,String>();  
 
 map.put(100,"Mumbai");  
 map.put(101,"Delhi");  
 map.put(102,"Pune");  
 
 // Add Element
 map.put(103, "Surat");
 
 // Size of map
 System.out.println(map.size());
 
 //clears hashmap , removes all element
 map.clear(); 
 
 // Remove element from hashmap
 map.remove(100);
 
 //Checking if HashMap is empty
 System.out.println("Is HashMap is empty: " + map.isEmpty());
  
 for(Map.Entry m:map.entrySet()){  
  System.out.println(m.getKey()+" "+m.getValue());  
 }  
}  
}   


Difference between ArrayList and HashMap in Java


<-- Previous || Next -->

5 comments:

  1. Very useful for fast interview preparation in core java.

    ReplyDelete
  2. Could you please let me know what all other Java collections intefaces and classes used in Selenium ?

    ReplyDelete
  3. Simple and understandable.
    Great work brother. All the best

    ReplyDelete
  4. This comment has been removed by a blog administrator.

    ReplyDelete