Collections framework in Java

Collection:
Group of individual objects in one unit.
It’s a root interface of collections framework, which defines most common methods to act on collection object.
Collections:
It’s a util class (Under java.util.package) which helps to perform different operations(like searching) on collection objects.So to handle the objects, we need certain classes(methods, variables) and interfaces which is nothing but collections framework.

We will be discussing about collections framework to some extent that can be helpful for an automation tester to design automation framework in Java.

There are methods available for

  • adding object to list
  • Remove an object from list
  • get a particular object
  • Update an object from list
  • loop through a list to get each object

For all of the above operations, there are different method names available (for all of the classes listed in the above tree structure), just the method name may be different, you can get the names of each of classes by creating an object of that class, and perform required operation.

List
Child interface of Collection, used to represent group of collection individual object where

  • Duplicates are allowed.
  • Insertion order is preserved.
  • Can access the objects by providing index.

Implemented classes of List: ArrayList / LinkedList / vector:

  • Growable array
  • null insertion is possible
  • allows heterogeneous objects.

Stack is a sub class of Vector.

ArrayList al = new ArrayList();
or
ArrayList al = new ArrayList(int size);
or
ArrayList al = new ArrayList(Collection c);

Preferences:
ArrayList is best choice, if frequent operation is retrieving (as arraylist implements RandomAccess interface, so we can access any object randomly with same speed.)
Vector is also best choice, if frequent operation is retrieving (Vector implements Serializable, Clonable and RandomAccess interface as well)
LinkedList is best choice, if frequent operations are insertion/deletion (implemented by Serializable and Clonable interface.)

For any of the above classes(LinkedList / ArrayList / vector), create an object of that class, and then put a . (dot) after that object, to get all the operations defined.

let’s see example of the usage of List:

OutPut:

Set
Child interface of Collection, used to represent group of collection individual object where

  • Duplicates are NOT allowed.
  • Can access the objects by providing index.

Implemented classes of List: HashSet

  • Growable array
  • null insertion is possible
  • allows heterogeneous objects.
  • Insertion order is NOT preserved.

LinkedHashSet

Same as HashSet, except in this, Insertion order is preserved.

Program for Set is same as LinkedList (as shown in above code)

SortedSet
Child interface of Set, represent a group of objects in some sorting order.
TreeSet is a implemented class to SortedSet.
 
Map
  • Represents a group of values in form of key and value pair instead of index.
  • It’s not a child interface of Collection
  • Both key and value are not objects.
  • Duplicate key is NOT allowed, but value can be duplicated.
HashMap: Implemented class to Map
Insertion order is NOT preserved.
Null Key is allowed only once, null values are allowed.
 
LinkedHashMap is child class to HashMap, only insertion order is preserved.

 

Content posted is based on learning or working experience, please leave comments if anything needs to be added or updated, discuss your queries on our facebook:qavalidation.com, Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: