24-Collections Framework and Generics
24-Collections Framework and Generics
A collection — sometimes called a container — 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.
Interfaces: These are abstract data types that represent collections. Interfaces allow
collections to be manipulated independently of the details of their representation. In
object-oriented languages, interfaces generally form a hierarchy.
Implementations: These are the concrete implementations of the collection interfaces. In
essence, they are reusable data structures.
Algorithms: These are the methods that perform useful computations, such as searching
and sorting, on objects that implement collection interfaces. The algorithms are said to be
polymorphic: that is, the same method can be used on many different implementations of
the appropriate collection interface. In essence, algorithms are reusable functionality.
Java Collection simply means a single unit of objects. Java Collection framework provides many
interfaces (Set, List, Queue, Deque etc.) and classes (ArrayList, Vector, LinkedList, PriorityQueue,
HashSet, LinkedHashSet, TreeSetetc).
Benefits of Framework
Reduces programming effort
Software reuse
Interfaces
This enables you to work with groups of objects; it is at the top of the collections hierarchy.
The Set
This extends Collection to handle sets, which must contain unique elements.
The SortedSet
The Map
The Map.Entry
This describes an element (a key/value pair) in a map. This is an inner class of Map.
The SortedMap
This extends Map so that the keys are maintained in an ascending order.
The Enumeration
This is legacy interface defines the methods by which you can enumerate (obtain one at a time)
the elements in a collection of objects. This legacy interface has been superceded by Iterator.
AbstractCollection
AbstractList
AbstractSequentialList
Extends AbstractList for use by a collection that uses sequential rather than random access of its
elements.
LinkedList
AbstractSet
HashSet
LinkedHashSet
Example
importjava.util.*;
class TestCollection1{
publicstaticvoid main(String args[]){
Generics in Java
Generic Methods
You can write a single generic method declaration that can be called with arguments of different types.
Based on the types of the arguments passed to the generic method, the compiler handles each method
call appropriately.
i. All generic method declarations have a type parameter section delimited by angle brackets
(< and >) that precedes the method's return type
ii. Each type parameter section contains one or more type parameters separated by commas.
A type parameter, also known as a type variable, is an identifier that specifies a generic type
name.
iii. The type parameters can be used to declare the return type and act as placeholders for the
types of the arguments passed to the generic method, which are known as actual type
arguments.
iv. A generic method's body is declared like that of any other method.
Example
publicclassGenericMethodTest {
// generic method printArray
publicstatic< E >voidprintArray( E[] inputArray ) {
// Display array elements
for(E element : inputArray) {
System.out.printf("%s ", element);
}
System.out.println();
}
System.out.println("\nArraydoubleArray contains:");
printArray(doubleArray); // pass a Double array
System.out.println("\nArraycharacterArray contains:");
printArray(charArray); // pass a Character array
}
}
Generic Classes
public class Box<T> {
private T t;
publicvoid add(T t) {
this.t = t;
}
public T get() {
returnt;
}
integerBox.add(new Integer(10));
stringBox.add(new String("Hello World"));