SlideShare a Scribd company logo
Collections Framework
Learning Outcome
• To understand the need and definitions,
hierarchy of collection framework
• Various interfaces
– Iterable
– Collection
– List
– Set
– Queue
– Deque
Collections 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).
Hierarchy of interfaces
Class implements various interfaces
Implements Map
java.util
• classes that generate pseudorandom numbers,
manage date and time, observe events, manipulate
sets of bits, tokenize strings, and handle formatted
data
• The java.util package also contains one of Java’s most
powerful subsystems: the Collections Framework.
• The Collections Framework is a sophisticated
hierarchy of interfaces and classes that provide state-
of-the-art technology for managing groups of objects
java.util classes
java.util interfaces
Collections Framework
• The framework should provide high-performance.
• The framework 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.
• Entire Collections Framework is built upon a set of standard
interfaces
• Integration of standard arrays into the Collections Framework.
• Algorithms operate on collections and are defined as static
methods within the Collections class.
• All collections are now generic, and many of the methods that
operate on collections take generic type parameters. Ensure
Type Safety
Iterator Interface
• An iterator offers a general-purpose,
standardized way of accessing the elements
within a collection, one at a time.
• An iterator provides a means of enumerating
the contents of a collection.
• Maps store key/value pairs.
Lists and Sets
A list is a collection that maintains the
order of its elements.
• Ordered Lists
– ArrayList  List
• Stores a list of items in a dynamically sized array
– LinkedList  List, Queue
• Allows speedy insertion and removal of items from the list
Copyright © 2013 by John Wiley & Sons.
All rights reserved.
Lists and Sets
A set is an unordered collection of
unique elements.
• Unordered Sets
– HashSet  Set
• Uses hash tables to speed up finding, adding, and
removing elements
– TreeSet  SortedSet
• Uses a binary tree to speed up finding, adding, and
removing elements
Copyright © 2013 by John Wiley & Sons.
All rights reserved.
Set Vs List
import java.util.*;
public class MyClass {
public static void main(String args[]) {
Set<String> s=new HashSet<String>();
s.add("fggg");
s.add("Wewe");
s.add("EREW");
s.add("Wewer");
s.add("rtrer");
System.out.println("Set of elements " + s);
List<String> l=new ArrayList<String>();
l.add("fggg");
l.add("Wewe");
l.add("EREW");
l.add("Wewer");
l.add("rtrer");
System.out.println("List of elements " + l);
}}
Output:
Set of elements [rtrer, Wewe, fggg, EREW, Wewer]
List of elements [fggg, Wewe, EREW, Wewer, rtrer]
Stacks and Queues
• Another way of gaining efficiency in a collection is to
reduce the number of operations
available
• Two examples are:
– Stack
• Remembers the order of its elements, but it does not allow
you to insert elements in every position
• You can only add and remove elements at the
top
– Queue
• Add items to one end (the tail)
• Remove them from the other end (the head)
• Example: A line of people waiting for a bank teller
Copyright © 2013 by John Wiley & Sons.
All rights reserved.
Maps
• A map stores keys, values, and the associations between
them
– Example:
– Barcode keys and books
• Keys
– Provides an easy way to represent an object (such as a numeric bar
code)
• Values
– The actual object that is associated with the key
A map keeps associations
between key and value objects.
Copyright © 2013 by John Wiley & Sons.
All rights reserved.
Internal working of Set
Collection Interfaces
Interface Description
Collection Enables you to work with groups of objects; it is at the top of the
collections hierarchy.
List Extends Collection to handle sequences (lists of objects).
Set Extends Collection to handle sets, which must contain unique
elements.
Queue Extends Collection to handle special types of lists in which elements
are removed only from the head.
SortedSet Extends Set to handle sorted sets.
NavigableSet Extends SortedSet to handle retrieval of elements based on closest-
match searches.
Deque Extends Queue to handle a double-ended queue.
Other Interfaces mainly used
• Collections also use the Comparator,
RandomAccess, Iterator, and ListIterator interfaces.
• Comparator defines how two objects are
compared;
• Iterator and ListIterator enumerate the objects
within a collection.
• By implementing RandomAccess, a list indicates
that it supports efficient, random access to its
elements.
Collection Interface – Extends Iterable interface
• public interface Collection<E> extends Iterable<E>
Collection Interface
Exceptions in Collection
• Several of these methods can throw an
• UnsupportedOperationException - occurs if a collection cannot
be modified.
• ClassCastException – occurs when one object is incompatible
with another, such as when an attempt is made to add an
incompatible object to a collection.
• NullPointerException - occurs if an attempt is made to store a null
object and null elements are not allowed in the collection.
• IllegalArgumentException - occurs if an invalid argument is used.
• IllegalStateException - occurs if an attempt is made to add an
element to a fixed-length collection that is full.
List Interface
• public interface List<E> extends Collection<E>
• It stores a sequence of elements
• Elements are inserted or accessed based on
their position in list
• List can contain duplicate elements
Java Collections Framework - Interfaces, Classes and Algorithms
Set Interface
• Set interface defines a set
• It does not allow duplicate elements
• public interface Set<E> extends Collection<E>
• add() method returns false if an attempt is
made to add duplicate elements to a set
• Same methods as Collection interface
Queue Interface
• The Queue interface extends Collection and
declares the behavior of a queue, which is
often a first-in, first-out list
• public interface Queue<E> extends
Collection<E>
boolean add(E obj) --- add element else throw IllegalStateException
Deque Interface
• The Deque interface extends Queue and
declares the behavior of a double-ended
queue.
• Double-ended queues can function as
standard, first-in, first-out queues or as last-in,
firstout stacks.
• public interface Deque<E> extends Queue<E>
Deque methods
• addFirst, addLast, getFirst, getLast, offerFirst,
offerLast, peekFirst, peekLast, pollFirst,
pollLast, pop, push, removeFirst, removeLast,
removeFirstOccurrence,
removeLastOccurrence
References
• Herbert Schildt,“Java:The Complete
Reference”, 8th Edition,McGrawHill Education,
2011.
• https://docs.oracle.com/javase/7/docs/api/jav
a/util/

More Related Content

Similar to Java Collections Framework - Interfaces, Classes and Algorithms (20)

PPT
JavaCollections.ppt
boopathirajaraja1
 
PPT
collections
javeed_mhd
 
PPTX
LJ_JAVA_FS_Collection.pptx
Raneez2
 
PPT
M251_Meeting 8 (Sets and Maps_Java_).ppt
smartashammari
 
PPT
M251_Meeting 8 (SetsandMap Advanced Java).ppt
smartashammari
 
PPTX
Advanced Java - UNIT 3.pptx
eyemitra1
 
PDF
java unit 4 pdf - about java collections
aapalaks
 
PPTX
22CS305-UNIT-1.pptx ADVANCE JAVA PROGRAMMING
logesswarisrinivasan
 
PPT
description of Collections, seaching & Sorting
mdimberu
 
PPTX
Updated_Java_Collections_Framework_Presentation.pptx
abhishekhatwal18
 
PDF
JAVA PROGRAMMING - The Collections Framework
Jyothishmathi Institute of Technology and Science Karimnagar
 
PPTX
Collections
Marwa Dosoky
 
PPTX
Collections framework in java
yugandhar vadlamudi
 
PPT
Collection Framework.power point presentation.......
Betty333100
 
PPTX
Module-1 Updated Collection Framework.pptx
rekhakeerti19
 
PDF
Collections and generics
Muthukumaran Subramanian
 
PPT
11000121065_NAITIK CHATTERJEE.ppt
NaitikChatterjee
 
PPTX
Icom4015 lecture14-f16
BienvenidoVelezUPR
 
PPTX
VTUOOPMCA5THMODULECollection OverV .pptx
VeenaNaik23
 
JavaCollections.ppt
boopathirajaraja1
 
collections
javeed_mhd
 
LJ_JAVA_FS_Collection.pptx
Raneez2
 
M251_Meeting 8 (Sets and Maps_Java_).ppt
smartashammari
 
M251_Meeting 8 (SetsandMap Advanced Java).ppt
smartashammari
 
Advanced Java - UNIT 3.pptx
eyemitra1
 
java unit 4 pdf - about java collections
aapalaks
 
22CS305-UNIT-1.pptx ADVANCE JAVA PROGRAMMING
logesswarisrinivasan
 
description of Collections, seaching & Sorting
mdimberu
 
Updated_Java_Collections_Framework_Presentation.pptx
abhishekhatwal18
 
JAVA PROGRAMMING - The Collections Framework
Jyothishmathi Institute of Technology and Science Karimnagar
 
Collections
Marwa Dosoky
 
Collections framework in java
yugandhar vadlamudi
 
Collection Framework.power point presentation.......
Betty333100
 
Module-1 Updated Collection Framework.pptx
rekhakeerti19
 
Collections and generics
Muthukumaran Subramanian
 
11000121065_NAITIK CHATTERJEE.ppt
NaitikChatterjee
 
Icom4015 lecture14-f16
BienvenidoVelezUPR
 
VTUOOPMCA5THMODULECollection OverV .pptx
VeenaNaik23
 

Recently uploaded (20)

PDF
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
PPTX
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PPTX
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
PPTX
PPT-Q1-WEEK-3-SCIENCE-ERevised Matatag Grade 3.pptx
reijhongidayawan02
 
PPTX
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
PDF
Horarios de distribución de agua en julio
pegazohn1978
 
PDF
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
PDF
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
PPTX
Quarter 1_PPT_PE & HEALTH 8_WEEK 3-4.pptx
ronajadolpnhs
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PPTX
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
PPTX
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
PPTX
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PPTX
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
PDF
Mahidol_Change_Agent_Note_2025-06-27-29_MUSEF
Tassanee Lerksuthirat
 
PPT
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
PPT-Q1-WEEK-3-SCIENCE-ERevised Matatag Grade 3.pptx
reijhongidayawan02
 
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
Horarios de distribución de agua en julio
pegazohn1978
 
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
Quarter 1_PPT_PE & HEALTH 8_WEEK 3-4.pptx
ronajadolpnhs
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
Mahidol_Change_Agent_Note_2025-06-27-29_MUSEF
Tassanee Lerksuthirat
 
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
Ad

Java Collections Framework - Interfaces, Classes and Algorithms

  • 2. Learning Outcome • To understand the need and definitions, hierarchy of collection framework • Various interfaces – Iterable – Collection – List – Set – Queue – Deque
  • 3. Collections 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).
  • 7. java.util • classes that generate pseudorandom numbers, manage date and time, observe events, manipulate sets of bits, tokenize strings, and handle formatted data • The java.util package also contains one of Java’s most powerful subsystems: the Collections Framework. • The Collections Framework is a sophisticated hierarchy of interfaces and classes that provide state- of-the-art technology for managing groups of objects
  • 10. Collections Framework • The framework should provide high-performance. • The framework 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. • Entire Collections Framework is built upon a set of standard interfaces • Integration of standard arrays into the Collections Framework. • Algorithms operate on collections and are defined as static methods within the Collections class. • All collections are now generic, and many of the methods that operate on collections take generic type parameters. Ensure Type Safety
  • 11. Iterator Interface • An iterator offers a general-purpose, standardized way of accessing the elements within a collection, one at a time. • An iterator provides a means of enumerating the contents of a collection. • Maps store key/value pairs.
  • 12. Lists and Sets A list is a collection that maintains the order of its elements. • Ordered Lists – ArrayList  List • Stores a list of items in a dynamically sized array – LinkedList  List, Queue • Allows speedy insertion and removal of items from the list Copyright © 2013 by John Wiley & Sons. All rights reserved.
  • 13. Lists and Sets A set is an unordered collection of unique elements. • Unordered Sets – HashSet  Set • Uses hash tables to speed up finding, adding, and removing elements – TreeSet  SortedSet • Uses a binary tree to speed up finding, adding, and removing elements Copyright © 2013 by John Wiley & Sons. All rights reserved.
  • 14. Set Vs List import java.util.*; public class MyClass { public static void main(String args[]) { Set<String> s=new HashSet<String>(); s.add("fggg"); s.add("Wewe"); s.add("EREW"); s.add("Wewer"); s.add("rtrer"); System.out.println("Set of elements " + s); List<String> l=new ArrayList<String>(); l.add("fggg"); l.add("Wewe"); l.add("EREW"); l.add("Wewer"); l.add("rtrer"); System.out.println("List of elements " + l); }} Output: Set of elements [rtrer, Wewe, fggg, EREW, Wewer] List of elements [fggg, Wewe, EREW, Wewer, rtrer]
  • 15. Stacks and Queues • Another way of gaining efficiency in a collection is to reduce the number of operations available • Two examples are: – Stack • Remembers the order of its elements, but it does not allow you to insert elements in every position • You can only add and remove elements at the top – Queue • Add items to one end (the tail) • Remove them from the other end (the head) • Example: A line of people waiting for a bank teller Copyright © 2013 by John Wiley & Sons. All rights reserved.
  • 16. Maps • A map stores keys, values, and the associations between them – Example: – Barcode keys and books • Keys – Provides an easy way to represent an object (such as a numeric bar code) • Values – The actual object that is associated with the key A map keeps associations between key and value objects. Copyright © 2013 by John Wiley & Sons. All rights reserved.
  • 18. Collection Interfaces Interface Description Collection Enables you to work with groups of objects; it is at the top of the collections hierarchy. List Extends Collection to handle sequences (lists of objects). Set Extends Collection to handle sets, which must contain unique elements. Queue Extends Collection to handle special types of lists in which elements are removed only from the head. SortedSet Extends Set to handle sorted sets. NavigableSet Extends SortedSet to handle retrieval of elements based on closest- match searches. Deque Extends Queue to handle a double-ended queue.
  • 19. Other Interfaces mainly used • Collections also use the Comparator, RandomAccess, Iterator, and ListIterator interfaces. • Comparator defines how two objects are compared; • Iterator and ListIterator enumerate the objects within a collection. • By implementing RandomAccess, a list indicates that it supports efficient, random access to its elements.
  • 20. Collection Interface – Extends Iterable interface • public interface Collection<E> extends Iterable<E>
  • 22. Exceptions in Collection • Several of these methods can throw an • UnsupportedOperationException - occurs if a collection cannot be modified. • ClassCastException – occurs when one object is incompatible with another, such as when an attempt is made to add an incompatible object to a collection. • NullPointerException - occurs if an attempt is made to store a null object and null elements are not allowed in the collection. • IllegalArgumentException - occurs if an invalid argument is used. • IllegalStateException - occurs if an attempt is made to add an element to a fixed-length collection that is full.
  • 23. List Interface • public interface List<E> extends Collection<E> • It stores a sequence of elements • Elements are inserted or accessed based on their position in list • List can contain duplicate elements
  • 25. Set Interface • Set interface defines a set • It does not allow duplicate elements • public interface Set<E> extends Collection<E> • add() method returns false if an attempt is made to add duplicate elements to a set • Same methods as Collection interface
  • 26. Queue Interface • The Queue interface extends Collection and declares the behavior of a queue, which is often a first-in, first-out list • public interface Queue<E> extends Collection<E>
  • 27. boolean add(E obj) --- add element else throw IllegalStateException
  • 28. Deque Interface • The Deque interface extends Queue and declares the behavior of a double-ended queue. • Double-ended queues can function as standard, first-in, first-out queues or as last-in, firstout stacks. • public interface Deque<E> extends Queue<E>
  • 29. Deque methods • addFirst, addLast, getFirst, getLast, offerFirst, offerLast, peekFirst, peekLast, pollFirst, pollLast, pop, push, removeFirst, removeLast, removeFirstOccurrence, removeLastOccurrence
  • 30. References • Herbert Schildt,“Java:The Complete Reference”, 8th Edition,McGrawHill Education, 2011. • https://docs.oracle.com/javase/7/docs/api/jav a/util/