Definition
• A collectionin java is represented as a
container that groups all the elements
into a single unit.
• For example, a mail folder (group of
emails), a telephone directory (mapping of
names to phone numbers).
• A framework is a basic foundation or layout
on which you start working by using the
different classes and interfaces provided.
3.
Collections Framework inJava
• All objects are grouped into a single object along with an architecture that
represents and provides different methods for manipulating collections.
• So Collections framework in Java provides different data structures already
implemented for storing data and methods, to manipulate them with features such
as sorting, searching, deletion, and insertion.
• import java.util.*;
4.
Benefits of CollectionsFramework in Java
It has the following benefits.
1.Reusability
2.Already Implemented (time-saving).
3.Performance Efficiency (speed and quality).
4.Reduces effort to learn and use new APIs.
5.
Hierarchy of CollectionFramework
Each collection class implements an interface from a hierarchy. Each
class is designed for a specific type of storage.
1.Interfaces
2.Classes (implementation)
3.Algorithms
7.
• Abstract implementations-
Partial implementations of the collection interfaces to facilitate custom implementations
such as:
AbstractCollection, AbstractSet, AbstractList, AbstractSequentialList and AbstractMap.
• Algorithms -
Static methods that perform useful functions on collections, such as sorting a list.
8.
The collections frameworkconsists of:
• Collection interfaces
These interfaces represent different types of collections, such as Set, List, and Map. These
interfaces form the basis of the framework.
• General-purpose implementations
These are primary implementations of the collection interfaces such
as ArrayList, LinkedList, HashSet, LinkedHashSet, HashMap etc.
• Legacy implementations
The collection classes from earlier releases, Vector and Hashtable, were retrofitted to
implement the collection interfaces.
Collection Interfaces
• Presentin java.util.package to define several utility methods like sorting,
searching for collection objects. E.g. Collections.Sort(x);
interface Collection<E>
11.
Common Methods ofthese Interfaces
Methods Description
public boolean add(E e)
Used to insert an element into the
collection
public boolean remove(Object element)
Used to remove an element from the
collection
public int size()
Returns the number of elements in a
collection
public boolean contains(Object element) Used to search for an element
public boolean isEmpty() Checks if the collection is empty
public boolean equals(Object element) Checks for equality
12.
Collection Interfaces
Interface Description
CollectionEnables you to work with groups of objects; it is at the top of the collections
hierarchy.
Deque Extends Queue to handle a double-ended queue.
List Extends Collection to handle sequences (lists of objects).
NavigableSet Extends SortedSet to handle retrieval of elements based on closest-match
searches.
Queue Extends Collection to handle special types of lists in which elements are
removed only from the head.
Set Extends Collection to handle sets, which must contain unique elements.
SortedSet Extends Set to handle sorted sets.
13.
List
• Child interfaceof collection.
• Extends Collection to handle sequences (lists of objects).
interface List<E>
• E specifies the type of objects that list will hold.
• Duplicates are allowed and insertion order preserved.
Set
• Extends Collectionto handle sets, which must contain unique elements.
interface Set<E>
• Duplicates are not allowed and insertion order not preserved.
• Therefore, add() method returns false if an attempt to made to add duplicate
elements.
SortedSet Interface
• ExtendsSet to handle sorted sets.
interface SortedSet<E>
• Duplicates are allowed but all objects should be inserted according to some
sorting order.