Collections is nothing but the group of Objects. In Java, collection framework is made up of below classes and interfaces. Important interfaces in Collection framework are given below.
  • List – stores all types of values
  • Set – stores only unique values
  • Queue – stores values in Queue data structure
  • Map – Stores key-value pairs
Important concrete classes that implement above interfaces are given below.
  • ArrayList, LinkedList, Vector, Stack
  • HashSet, TreeSet, LinkedHashSet
  • PriorityQueue, ArrayDeque
  • HashMap, TreeMap, LinkedHashMap, HashTable, SortedMap
  • Collections – provides some static methods (algorithms) to work with any collection type
  • Iterable – This interface is used for iterating through elements in the collection object
Below image shows various classes and interfaces hierarchy of collections framework in Java.Below image helps us to understand which collection class should be used and when.Here is an example ArrayList.
 
package corejava;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

/**
 * Created by Sagar on 10-04-2016.
 */
public class collections {

    public static void main(String [] args){
        List<String> myList = new ArrayList<String>();
        myList.add("Brisbane");
        myList.add("Paris");
        myList.add("chicago");
        myList.add("London");
        Collections.sort(myList);

        //Iterating using new for loop
        for (String x : myList){
            System.out.println(x);
        }

        myList.remove("Paris");
        System.out.println("*********After removing Paris*********");
        //Iterating using Itr class
        Iterator<String> iter = myList.iterator();
        while (iter.hasNext()) {
            String str = iter.next();
            System.out.println(str);
        }
        System.out.println("List size is -> " + myList.size());
        System.out.println("List contains London? -> " + myList.contains("London"));

        //Iterating using Lambda expressions in Java 8
        myList.forEach( value -> System.out.println(value));

    }
}
Here is the output of above example.

Brisbane
London
Paris
chicago
*********After removing Paris*********
Brisbane
London
chicago
List size is -> 3
List contains London? -> true
Brisbane
London
chicago

Process finished with exit code 0

Web development and Automation testing

solutions delivered!!