Book Image

Learning Selenium Testing Tools - Third Edition

Book Image

Learning Selenium Testing Tools - Third Edition

Overview of this book

Table of Contents (22 chapters)
Learning Selenium Testing Tools Third Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Collections


A collection is simply an object that groups multiple elements into a single unit. Collections are used to store, retrieve, manipulate, and communicate aggregate data. Typically, they represent data items that form a natural group, such as a poker hand (a collection of cards), a mail folder (a collection of letters), or a telephone directory (a mapping of names to phone numbers).

What is a Collections framework?

A collections framework is a unified architecture for representing and manipulating groups of data as a single unit collection. All collections frameworks consist of the following parts:

  • Interfaces: The Java collections framework interface provides the abstract data type to represent a collection. The root interface of Collections framework hierarchy is java.util.Collection. Some other important interfaces are java.util.List and java.util.Set. All the collections framework interfaces are present in the java.util package.

  • Implementations: These are the core implementations of the collection interfaces. They are reusable data structures to create different types of collections. Some important collection classes are ArrayList, LinkedList, HashSet, and TreeSet.

  • Algorithms: These are the methods that perform useful and reusable common functionalities, such as searching and sorting, on objects that implement collection interfaces. The same method can be used on many different implementations of the appropriate collection interface.

The following list describes the core collection interfaces:

  • The Collection interface is the root of the collection hierarchy. A collection represents a group of objects known as its elements. The Collection interface is the least common denominator that all collections implement and is used to pass collections around and to manipulate them when maximum generality is desired. Some types of collections allow duplicate elements, and others do not. Some are ordered and others are unordered. The Java platform doesn't provide any direct implementations of this interface but provides implementations of more specific sub-interfaces, such as Set and List

  • The Set interface is a collection that cannot contain duplicate elements. This interface models the mathematical set abstraction and is used to represent sets, such as the cards comprising a poker hand, the courses making up a student's schedule, or the processes running on a machine.

    Note

    For more information, please go through http://docs.oracle.com/javase/tutorial/collections/intro/.

    Following is an example code on Collections:

    import java.util.HashSet;
    import java.util.Iterator;
    import java.util.Set;
    
    public class SetCollection {
      public static void main(String args[]) {
        //Create a set
        Set<String> uniqueSet = new HashSet<String>();
        // Add an elements to set
        uniqueSet.add("Apple");
        uniqueSet.add("Cat");
        uniqueSet.add("Apple");
        uniqueSet.add("Ball");
        
        //Print an elements of the Set
        System.out.println("UniqueSet: " + uniqueSet);
        
        //Retrieve element from set using Iterator
        Iterator<String> iterator = uniqueSet.iterator();
        while (iterator.hasNext()) {
          System.out.println("Iterator: " + iterator.next());
        }
      }
    }

    The output will be as follows:

    UniqueSet: [Ball, Cat, Apple]
    Iterator: Ball
    Iterator: Cat
    Iterator: Apple
    
  • List: This is an ordered collection (sometimes called a sequence). Lists can contain duplicate elements. The user of a list generally has precise control over where in the list each element is inserted and can access elements by their integer index (position). The following is an example of code on lists:

    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
    
    public class ListCollection {
      public static void main(String args[]) {
        //Create a List
        List<String> list = new ArrayList<String>();
        // Add an elements to List
        list.add("Apple");
        list.add("Cat");
        list.add("Apple");
        list.add("Ball");
        
        //Print an elements of the List
        System.out.println("List: " + list);
        
        //Retrieve element from list using Iterator
        Iterator<String> iterator = list.iterator();
        while (iterator.hasNext()) {
          System.out.println("Iterator: " + iterator.next());
        }
      }
    }

    The output will be as follows:

    List: [Apple, Cat, Apple, Ball]
    Iterator: Apple
    Iterator: Cat
    Iterator: Apple
    Iterator: Ball
    
  • SortedSet: This is a set that maintains its elements in an ascending order. Several additional operations are provided to take advantage of the ordering. Sorted sets are used for naturally ordered sets, such as word lists and membership rolls. The following is an example of SortedSet:

    import java.util.Iterator;
    import java.util.SortedSet;
    import java.util.TreeSet;
    
    public class SortedSetCollection {
      public static void main(String args[]) {
        //Create a set
        SortedSet<String> sortSet = new TreeSet<String>();
        // Add an elements to set
        sortSet.add("Apple");
        sortSet.add("Cat");
        sortSet.add("Apple");
        sortSet.add("Ball");
        
        //Print an elements of the Set
        System.out.println("UniqueSet: " + sortSet);
        
        //Retrieve element from set using Iterator
        Iterator<String> iterator = sortSet.iterator();
        while (iterator.hasNext()) {
          System.out.println("Iterator: " + iterator.next());
        }
      }
    }

    The output will be as follows:

    SortedSet: [Apple, Ball, Cat]
    Iterator: Apple
    Iterator: Ball
    Iterator: Cat
    

Note

Following is a reference link for the Collection framework:

http://docs.oracle.com/javase/tutorial/collections/intro/