Collections are like containers that groups multiple items in a single unit.
1) ArrayList:
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:
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());
}
}
}
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:
- Java ArrayList class uses a dynamic array for storing the elements.
- It extends AbstractList class and implements List interface.
- Java ArrayList class can contain duplicate elements.
- Java ArrayList class maintains insertion order.
- Java ArrayList class is non synchronized.
- Java ArrayList allows random access because array works at the index basis.
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:
- A HashMap contains values based on the key. It implements the Map interface and extends AbstractMap class.
- It contains only unique elements.
- It may have one null key and multiple null values.
- It maintains no order.
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());
}
}
}
Very useful for fast interview preparation in core java.
ReplyDeleteCould you please let me know what all other Java collections intefaces and classes used in Selenium ?
ReplyDeleteSimple and understandable.
ReplyDeleteGreat work brother. All the best
quick revision purpose....
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDelete