Collections Framework

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 15

Collections Framework

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.

The collections framework was designed to meet several goals.

The framework had to be high-performance. The implementations


for the fundamental collections (dynamic arrays, linked lists, trees,
and hash tables) are highly efficient.

The framework had to allow different types of collections to work


in a similar manner and with a high degree of interoperability.

Extending and/or adapting a collection had to be easy.


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.
 The Collection Framework provides a well-designed set
of interface and classes for sorting and manipulating
groups of data as a single unit, a collection.

 The Collection Framework provides a standard


programming interface to many of the most common
abstractions, without burdening the programmer with
too many procedures and interfaces.

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 i.e. Classes: 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.
The collection Interfaces
• The core collection interfaces encapsulate
different types of collections, which are
shown in the figure below.
 Core collection interfaces are the foundation of the
Java Collections Framework.
 As you can see in the following figure, the core
collection interfaces form a hierarchy.
 A Set is a special kind of Collection, a SortedSet is a
special kind of Set, and so forth. Note also that the
hierarchy consists of two distinct trees — a Map is
not a true Collection.
 Note that all the core collection interfaces are
generic. For example, this is the declaration of the
Collection interface.
public interface Collection<E>...
 The <E> syntax tells you that the interface is generic.
The Collection Interfaces:
SN Interfaces with Description
The Collection Interface
1 This enables you to work with groups of objects; it is at the top of the collections
hierarchy.

The List Interface


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

The Set
3
This extends Collection to handle sets, which must contain unique elements
The SortedSet
4
This extends Set to handle sorted sets
The Map
5
This maps unique keys to values.
The Map.Entry
6
This describes an element (a key/value pair) in a map. This is an inner class of Map.
The SortedMap
7
This extends Map so that the keys are maintained in ascending order.

The Enumeration
This is legacy interface and defines the methods by which you can enumerate (obtain one
8
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.
Some of the classes provide full
implementations that can be used as-is and
others are abstract class, providing skeletal
implementations that are used as starting
points for creating concrete collections.
The standard collection classes are
summarized in the given table ( Click Table)
Standard Collection classes
•The ArrayList Class
•The LinkedList Class
•The HashSet Class
•The LinkedHashSet Class
•The TreeSet Class
How to use an Iterator ?
Often, you will want to cycle through the elements in a
collection. For example, you might want to display each
element.
The easiest way to do this is to employ an iterator, which
is an object that implements either the Iterator or the
ListIterator interface.
Iterator enables you to cycle through a collection,
obtaining or removing elements. ListIterator extends
Iterator to allow bidirectional traversal of a list, and the
modification of elements.
Here is a list of all the methods with examples provided
by Iterator and ListIterator interfaces.
The Methods Declared by Iterator:

Methods with Description


1> boolean hasNext( ) :Returns true if there are
more elements. Otherwise, returns false.
2> Object next( ) : Returns the next element.
Throws NoSuchElementException if there is not
a next element.
3> void remove( ) : Removes the current
element. Throws IllegalStateException if an
attempt is made to call remove( ) that is not
preceded by a call to next( ).
The Methods Declared by ListIterator:
1> void add(Object obj) : Inserts obj into the list in front of
the element that will be returned by the next call to next( ).
2> boolean hasNext( ) : Returns true if there is a next
element. Otherwise, returns false.
3> boolean hasPrevious( ) : Returns true if there is a
previous element. Otherwise, returns false.
4> Object next( ) : Returns the next element. A
NoSuchElementException is thrown if there is not a next
element.
5> int nextIndex( ) : Returns the index of the next element.
If there is not a next element, returns the size of the list.
6> Object previous( ) : Returns the previous
element. A NoSuchElementException is thrown if
there is not a previous element.
7> int previousIndex( ) : Returns the index of the
previous element. If there is not a previous element,
returns -1.
8> void remove( ) : Removes the current element
from the list. An IllegalStateException is thrown if
remove( ) is called before next( ) or previous( ) is
invoked.
9> void set(Object obj) : Assigns obj to the current
element. This is the element last returned by a call
to either next( ) or previous( ).
How to use an Comparator ?
Both TreeSet and TreeMap store elements in
sorted order. However, it is the comparator that
defines precisely what sorted order means.
This interface lets us sort a given collection
any number of different ways. Also this
interface can be used to sort any instances of
any class.(even classes we cannot modify).
The Comparator interface defines two
methods: compare( ) and equals( ).

You might also like