0% found this document useful (0 votes)
2 views

24-Collections Framework and Generics

The document provides an overview of the Collections Framework in Java, which is a unified architecture for representing and manipulating collections of objects. It details the key components of the framework, including interfaces, implementations, and algorithms, as well as the benefits of using the framework. Additionally, it introduces generics in Java, explaining generic methods and classes with examples.

Uploaded by

TARINI MISHRA
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

24-Collections Framework and Generics

The document provides an overview of the Collections Framework in Java, which is a unified architecture for representing and manipulating collections of objects. It details the key components of the framework, including interfaces, implementations, and algorithms, as well as the benefits of using the framework. Additionally, it introduces generics in Java, explaining generic methods and classes with examples.

Uploaded by

TARINI MISHRA
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Introduction to Collections(Collection Framework)

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.

What Is a Collections Framework?

A collections framework is a unified architecture for representing and manipulating collections.

All collections frameworks contain the following:

 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

Increases program speed and quality

Allows interoperability among unrelated APIs

Reduces effort to learn and to use new APIs

Reduces effort to design new APIs

Software reuse

Interfaces

The Collection Interface

This enables you to work with groups of objects; it is at the top of the collections hierarchy.

The List Interface


This extends Collection and an instance of List stores an ordered collection of elements.

The Set

This extends Collection to handle sets, which must contain unique elements.

The SortedSet

This extends Set to handle sorted sets.

The Map

This maps unique keys to values.

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.

The Collection Classes


Java provides a set of standard collection classes that implement Collection interfaces.

AbstractCollection

Implements most of the Collection interface.

AbstractList

Extends AbstractCollection and implements most of the List interface.

AbstractSequentialList

Extends AbstractList for use by a collection that uses sequential rather than random access of its
elements.

LinkedList

Implements a linked list by extending AbstractSequentialList.


ArrayList

Implements a dynamic array by extending AbstractList.

AbstractSet

Extends AbstractCollection and implements most of the Set interface.

HashSet

Extends AbstractSet for use with a hash table.

LinkedHashSet

Extends HashSet to allow insertion-order iterations.

Example
importjava.util.*;
class TestCollection1{
publicstaticvoid main(String args[]){

ArrayList <String> al=new ArrayList <String> ();//creating arraylist


al.add("Silicon");//adding object in arraylist
al.add("Institute");
al.add("of");
al.add("Technology");

Iterator itr=al.iterator();//getting Iterator from arraylist to traverse


elements
while(itr.hasNext()){
System.out.println(itr.next());
}
}
}

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.

Following are the rules to define Generic Methods –

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();
}

publicstaticvoid main(String args[]) {


// Create arrays of Integer, Double and Character
Integer[] intArray = { 1, 2, 3, 4, 5 };
Double[] doubleArray = { 1.1, 2.2, 3.3, 4.4 };
Character[] charArray = { 'H', 'E', 'L', 'L', 'O' };

System.out.println("Array integerArray contains:");


printArray(intArray); // pass an Integer array

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;
}

publicstaticvoid main(String[] args) {


Box<Integer>integerBox = new Box<Integer>();
Box<String>stringBox = new Box<String>();

integerBox.add(new Integer(10));
stringBox.add(new String("Hello World"));

System.out.printf("Integer Value :%d\n\n", integerBox.get());


System.out.printf("String Value :%s\n", stringBox.get());
}
}

You might also like