Collection Prior to Java 2, Java provided ad hoc classes such as Dictionary, Vector, Stack, and Properties to store and manipulate groups of objects. Although these classes were quite useful, they lacked a central, unifying theme. Thus, the way that you used Vector was different from the way that you used Properties. 2
Collection Framework The Collection in Java is a framework that provides an architecture to store and manipulate the group of objects. Java Collections can achieve all the operations that you perform on a data such as searching, sorting, insertion, manipulation, and deletion. Java Collection means a single unit of objects. Java Collection framework provides many interfaces (Set, List, Queue, Deque) and classes (ArrayList, Vector, LinkedList, PriorityQueue, HashSet, LinkedHashSet , TreeSet ). 3
Collection Framework The collections framework was designed to meet several goals, such as − The framework had to be high-performance. The implementations for the fundamental collections (dynamic arrays, linked lists, trees, and hashtables ) were to be highly efficient. The framework had to allow different types of collections to work in a similar manner and with a high degree of interoperability. The framework had to extend and/or adapt a collection easily. 4
What is a framework in Java? It provides readymade architecture. It represents a set of classes and interfaces. It is optional. 5
Collection Hierarchy 6
Collection Hierarchy 7 A collections framework is a unified architecture for representing and manipulating collections. All collections frameworks contain the following − Interfaces Implementations, i.e., Classes Algorithms
Interfaces 8 Iterable Collection List Queue Deque Set
List Interface 9 T he List interface extends Collection and declares the behavior of a collection that stores a sequence of elements. Elements can be inserted or accessed by their position in the list, using a zero-based index. A list may contain duplicate elements. In addition to the methods defined by Collection , List defines some of its own, which are summarized in the following table. Several of the list methods will throw an UnsupportedOperationException if the collection cannot be modified, and a ClassCastException is generated when one object is incompatible with another.
List Interface Implementations 10 Stack LinkedList ArrayList
Stack 11 Stack is a subclass of Vector that implements a standard last-in, first-out stack. Both Stack and Vector classes are available from Java 1.0. These two classes are called Legacy Classes. Both are ThreadSafe with Synchronized Methods. Stack only defines the default constructor, which creates an empty stack. Stack includes all the methods defined by Vector, and adds several of its own. Stack()
Methods in Stack 12 Method Description boolean empty() Tests if this stack is empty. Returns true if the stack is empty, and returns false if the stack contains elements. Object peek( ) Returns the element on the top of the stack, but does not remove it. Object pop( ) Returns the element on the top of the stack, removing it in the process. Object push(Object element) Pushes the element onto the stack. Element is also returned. int search(Object element) Searches for element in the stack. If found, its offset from the top of the stack is returned. Otherwise, -1 is returned.
Example 13 StackDemo.java
LinkedList 14 Java LinkedList class uses a doubly linked list to store the elements. It provides a linked-list data structure. It inherits the AbstractList class and implements List and Deque interfaces. The important points about Java LinkedList are: Java LinkedList class can contain duplicate elements. Java LinkedList class maintains insertion order. Java LinkedList class is non synchronized. In Java LinkedList class, manipulation is fast because no shifting needs to occur. Java LinkedList class can be used as a list, stack or queue.
LinkedList 15 Java LinkedList class uses a doubly linked list to store the elements. It provides a linked-list data structure. It inherits the AbstractList class and implements List and Deque interfaces. The important points about Java LinkedList are: Java LinkedList class can contain duplicate elements. Java LinkedList class maintains insertion order. Java LinkedList class is non synchronized. In Java LinkedList class, manipulation is fast because no shifting needs to occur. Java LinkedList class can be used as a list, stack or queue.
Methods of LinkedList 16 Method Description boolean add(E e) It is used to append the specified element to the end of a list. void add(int index, E element) It is used to insert the specified element at the specified position index in a list. void addFirst (E e) It is used to insert the given element at the beginning of a list. void addLast (E e) It is used to append the given element to the end of a list. boolean contains(Object o) It is used to return true if a list contains a specified element. E element() It is used to retrieve the first element of a list. E get(int index) It is used to return the element at the specified position in a list.
Methods of LinkedList 17 Method Description E get(int index) It is used to return the element at the specified position in a list E getFirst() It is used to return the first element in a list. E getLast () It is used to return the last element in a list. int indexOf (Object o) It is used to return the index in a list of the first occurrence of the specified element, or -1 if the list does not contain any element. int lastIndexOf(Object o) It is used to return the index in a list of the last occurrence of the specified element, or -1 if the list does not contain any element. E peek() It retrieves the first element of a list E remove() It is used to retrieve and removes the first element of a list. E removeFirst() It removes and returns the first element from a list.
Example 18 LLDemo.java
Arrays 19 The java.util.Arrays class contains a static factory that allows arrays to be viewed as lists. Following are the important points about Arrays − This class contains various methods for manipulating arrays (such as sorting and searching). The methods in this class throw a NullPointerException if the specified array reference is null.
Example 20 ArraysDemo.java
ArrayList 21 Java ArrayList class uses a dynamic array for storing the elements. It inherits AbstractList class and implements List interface. The important points about Java ArrayList class are: 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. In Java ArrayList class, manipulation is slow because a lot of shifting needs to occur if any element is removed from the array list.
ArrayList 22 The ArrayList contains the following Constructors to initialize. ArrayList () ArrayList (int capacity)
Example 23 ALDemo.java
Accessing a Collection 24 To access any Collection, We can use the Iterator interface. An iterator is an interface that is used in place of Enumerations in the Java Collection Framework. Moreover, an iterator differs from the enumerations in two ways: Iterator permits the caller to remove the given elements from the specified collection during the iteration of the elements. Method names have been enhanced. Iterator interface is a member connected with Java Collections Framework.
Methods of Iterator 25 Methods Description forEachRemaining(Consumer<? super E>action) Performs the given action on each of the element until and unless all the elements have been processed or unless an exception is thrown by the action. hasNext() Returns a true value if the more number of elements are encountered during iteration. next() Returns the next specified element during the iteration. remove() Removes the last element from the collection as provided by the iterator.