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.

Let's see the hierarchy of collection interface
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:

package testPkg;

import java.util.LinkedList;

public class ListSet {
       public static void main(String[] args) {
    LinkedList ll = new LinkedList();
    ll.add(1);
    ll.add("ABC");
    ll.add(2, 3.14f);
    ll.add(null);
    System.out.println(ll);
    ll.set(3, 'S');
    System.out.println(ll);
           ll.removeLast();
           System.out.println(ll);
           System.out.println("Size:"+ll.size());
 }
}
OutPut:
[1, ABC, 3.14, null]
[1, ABC, 3.14, S]
[1, ABC, 3.14]
Size:3

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 calss to HashMap, only insertion order is preserved.

package testPkg;

import java.util.HashMap;

public class MapTest {

      public static void main(String[] args) {
  HashMap hm = new HashMap();
    hm.put("Planet", "Earth");
    hm.put("Country", "Ind");
    hm.put("Continent","Asia");
    hm.put("Lang", "Hindi");
    System.out.println(hm);
    System.out.println("Country:"+hm.get("Country"));
    hm.put("Lang", "Eng"); //update value
    System.out.println(hm);
    if (hm.remove("Lang", "Hindi")) //remove a key value pair
        System.out.println(hm);
 }
}

6 comments:

  1. Responsive desing can yield a very good revenue to a business. It has been discovered since the usage of multiple devices increase. The content furnished above too tells the same. Thanks for sharing this information in here. Please keep bloging content like this.

    Web designing course in chennai | Web design training | PHP Training in Chennai

    ReplyDelete
  2. I really enjoyed while reading your article, the information you have mentioned in this post was damn good. Keep sharing your blog with updated and useful information.
    Regards,
    Python Training in Chennai|Python Training Institutes in Chennai|python training chennai

    ReplyDelete
  3. Pretty article! I found some useful information in your blog, it was awesome to read, thanks for sharing this great content to my vision, keep sharing.
    Regards,
    sas training in Chennai|sas course in Chennai|sas training center in Chennai

    ReplyDelete
  4. Cloud has beome the common word that is being used by most of the professional these day. The reason for relying on this technology is security. Your content too lecture the same. Thanks for sharing this worth able information in here. Keep blogging article like this.

    Hadoop Training Chennai
    | Best Hadoop Training in Chennai | Manual testing training in Chennai

    ReplyDelete
  5. The information you have posted here is really useful and interesting too & here, I had a chance to gather some useful information, thanks for sharing and I have an expectation about your future blogs keep your updates please.
    Regards,
    Informatica training in chennai|Best Informatica Training In Chennai|Informatica training in chennai

    ReplyDelete
  6. Very informative post.Informatica is a data integration/ETL tool that provides functionality for data transformation and loading of data. Informatica gets data from various sources and it loads the data into different targets.

    Informatica training in chennai

    ReplyDelete