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)

JavaCollections.ppt
JavaCollections.pptJavaCollections.ppt
JavaCollections.ppt
boopathirajaraja1
 
collections
collectionscollections
collections
javeed_mhd
 
LJ_JAVA_FS_Collection.pptx
LJ_JAVA_FS_Collection.pptxLJ_JAVA_FS_Collection.pptx
LJ_JAVA_FS_Collection.pptx
Raneez2
 
M251_Meeting 8 (Sets and Maps_Java_).ppt
M251_Meeting 8 (Sets and Maps_Java_).pptM251_Meeting 8 (Sets and Maps_Java_).ppt
M251_Meeting 8 (Sets and Maps_Java_).ppt
smartashammari
 
M251_Meeting 8 (SetsandMap Advanced Java).ppt
M251_Meeting 8 (SetsandMap Advanced Java).pptM251_Meeting 8 (SetsandMap Advanced Java).ppt
M251_Meeting 8 (SetsandMap Advanced Java).ppt
smartashammari
 
Advanced Java - UNIT 3.pptx
Advanced Java - UNIT 3.pptxAdvanced Java - UNIT 3.pptx
Advanced Java - UNIT 3.pptx
eyemitra1
 
java unit 4 pdf - about java collections
java unit 4 pdf - about java collectionsjava unit 4 pdf - about java collections
java unit 4 pdf - about java collections
aapalaks
 
22CS305-UNIT-1.pptx ADVANCE JAVA PROGRAMMING
22CS305-UNIT-1.pptx   ADVANCE JAVA PROGRAMMING22CS305-UNIT-1.pptx   ADVANCE JAVA PROGRAMMING
22CS305-UNIT-1.pptx ADVANCE JAVA PROGRAMMING
logesswarisrinivasan
 
Updated_Java_Collections_Framework_Presentation.pptx
Updated_Java_Collections_Framework_Presentation.pptxUpdated_Java_Collections_Framework_Presentation.pptx
Updated_Java_Collections_Framework_Presentation.pptx
abhishekhatwal18
 
JAVA PROGRAMMING - The Collections Framework
JAVA PROGRAMMING - The Collections Framework JAVA PROGRAMMING - The Collections Framework
JAVA PROGRAMMING - The Collections Framework
Jyothishmathi Institute of Technology and Science Karimnagar
 
Collections
CollectionsCollections
Collections
Marwa Dosoky
 
Collections framework in java
Collections framework in javaCollections framework in java
Collections framework in java
yugandhar vadlamudi
 
Java Collections
Java  Collections Java  Collections
Java Collections
Kongu Engineering College, Perundurai, Erode
 
Collection Framework.power point presentation.......
Collection Framework.power point presentation.......Collection Framework.power point presentation.......
Collection Framework.power point presentation.......
Betty333100
 
Module-1 Updated Collection Framework.pptx
Module-1 Updated Collection Framework.pptxModule-1 Updated Collection Framework.pptx
Module-1 Updated Collection Framework.pptx
rekhakeerti19
 
Collections and generics
Collections and genericsCollections and generics
Collections and generics
Muthukumaran Subramanian
 
11000121065_NAITIK CHATTERJEE.ppt
11000121065_NAITIK CHATTERJEE.ppt11000121065_NAITIK CHATTERJEE.ppt
11000121065_NAITIK CHATTERJEE.ppt
NaitikChatterjee
 
Icom4015 lecture14-f16
Icom4015 lecture14-f16Icom4015 lecture14-f16
Icom4015 lecture14-f16
BienvenidoVelezUPR
 
VTUOOPMCA5THMODULECollection OverV .pptx
VTUOOPMCA5THMODULECollection OverV .pptxVTUOOPMCA5THMODULECollection OverV .pptx
VTUOOPMCA5THMODULECollection OverV .pptx
VeenaNaik23
 
mca5thCollection OverViCollection O.pptx
mca5thCollection OverViCollection O.pptxmca5thCollection OverViCollection O.pptx
mca5thCollection OverViCollection O.pptx
VeenaNaik23
 
LJ_JAVA_FS_Collection.pptx
LJ_JAVA_FS_Collection.pptxLJ_JAVA_FS_Collection.pptx
LJ_JAVA_FS_Collection.pptx
Raneez2
 
M251_Meeting 8 (Sets and Maps_Java_).ppt
M251_Meeting 8 (Sets and Maps_Java_).pptM251_Meeting 8 (Sets and Maps_Java_).ppt
M251_Meeting 8 (Sets and Maps_Java_).ppt
smartashammari
 
M251_Meeting 8 (SetsandMap Advanced Java).ppt
M251_Meeting 8 (SetsandMap Advanced Java).pptM251_Meeting 8 (SetsandMap Advanced Java).ppt
M251_Meeting 8 (SetsandMap Advanced Java).ppt
smartashammari
 
Advanced Java - UNIT 3.pptx
Advanced Java - UNIT 3.pptxAdvanced Java - UNIT 3.pptx
Advanced Java - UNIT 3.pptx
eyemitra1
 
java unit 4 pdf - about java collections
java unit 4 pdf - about java collectionsjava unit 4 pdf - about java collections
java unit 4 pdf - about java collections
aapalaks
 
22CS305-UNIT-1.pptx ADVANCE JAVA PROGRAMMING
22CS305-UNIT-1.pptx   ADVANCE JAVA PROGRAMMING22CS305-UNIT-1.pptx   ADVANCE JAVA PROGRAMMING
22CS305-UNIT-1.pptx ADVANCE JAVA PROGRAMMING
logesswarisrinivasan
 
Updated_Java_Collections_Framework_Presentation.pptx
Updated_Java_Collections_Framework_Presentation.pptxUpdated_Java_Collections_Framework_Presentation.pptx
Updated_Java_Collections_Framework_Presentation.pptx
abhishekhatwal18
 
Collection Framework.power point presentation.......
Collection Framework.power point presentation.......Collection Framework.power point presentation.......
Collection Framework.power point presentation.......
Betty333100
 
Module-1 Updated Collection Framework.pptx
Module-1 Updated Collection Framework.pptxModule-1 Updated Collection Framework.pptx
Module-1 Updated Collection Framework.pptx
rekhakeerti19
 
11000121065_NAITIK CHATTERJEE.ppt
11000121065_NAITIK CHATTERJEE.ppt11000121065_NAITIK CHATTERJEE.ppt
11000121065_NAITIK CHATTERJEE.ppt
NaitikChatterjee
 
VTUOOPMCA5THMODULECollection OverV .pptx
VTUOOPMCA5THMODULECollection OverV .pptxVTUOOPMCA5THMODULECollection OverV .pptx
VTUOOPMCA5THMODULECollection OverV .pptx
VeenaNaik23
 
mca5thCollection OverViCollection O.pptx
mca5thCollection OverViCollection O.pptxmca5thCollection OverViCollection O.pptx
mca5thCollection OverViCollection O.pptx
VeenaNaik23
 

Recently uploaded (20)

How to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 EmployeeHow to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 Employee
Celine George
 
Rai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptx
Rai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptxRai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptx
Rai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptx
Dr. Ravi Shankar Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
Unit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdfUnit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdf
KRUTIKA CHANNE
 
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdfFEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
ChristinaFortunova
 
Overview of Off Boarding in Odoo 18 Employees
Overview of Off Boarding in Odoo 18 EmployeesOverview of Off Boarding in Odoo 18 Employees
Overview of Off Boarding in Odoo 18 Employees
Celine George
 
Ray Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big CycleRay Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big Cycle
Dadang Solihin
 
ROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptx
ROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptxROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptx
ROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptx
Belicia R.S
 
Unit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptxUnit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptx
bobby205207
 
GEOGRAPHY-Study Material [ Class 10th] .pdf
GEOGRAPHY-Study Material [ Class 10th] .pdfGEOGRAPHY-Study Material [ Class 10th] .pdf
GEOGRAPHY-Study Material [ Class 10th] .pdf
SHERAZ AHMAD LONE
 
BINARY files CSV files JSON files with example.pptx
BINARY files CSV files JSON files with example.pptxBINARY files CSV files JSON files with example.pptx
BINARY files CSV files JSON files with example.pptx
Ramakrishna Reddy Bijjam
 
Chalukyas of Gujrat, Solanki Dynasty NEP.pptx
Chalukyas of Gujrat, Solanki Dynasty NEP.pptxChalukyas of Gujrat, Solanki Dynasty NEP.pptx
Chalukyas of Gujrat, Solanki Dynasty NEP.pptx
Dr. Ravi Shankar Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
Paper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil Disobedience
Paper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil DisobediencePaper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil Disobedience
Paper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil Disobedience
Rajdeep Bavaliya
 
How to Configure Vendor Management in Lunch App of Odoo 18
How to Configure Vendor Management in Lunch App of Odoo 18How to Configure Vendor Management in Lunch App of Odoo 18
How to Configure Vendor Management in Lunch App of Odoo 18
Celine George
 
What is FIle and explanation of text files.pptx
What is FIle and explanation of text files.pptxWhat is FIle and explanation of text files.pptx
What is FIle and explanation of text files.pptx
Ramakrishna Reddy Bijjam
 
Final Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptxFinal Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptx
bobby205207
 
Black and White Illustrative Group Project Presentation.pdf (1).pdf
Black and White Illustrative Group Project Presentation.pdf (1).pdfBlack and White Illustrative Group Project Presentation.pdf (1).pdf
Black and White Illustrative Group Project Presentation.pdf (1).pdf
AnnasofiaUrsini
 
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
Battle of Bookworms 2025 - U25 Literature Quiz by Pragya
Battle of Bookworms 2025 - U25 Literature Quiz by Pragya Battle of Bookworms 2025 - U25 Literature Quiz by Pragya
Battle of Bookworms 2025 - U25 Literature Quiz by Pragya
Pragya - UEM Kolkata Quiz Club
 
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition OecdEnergy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
razelitouali
 
Exploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle SchoolExploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle School
Marie
 
How to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 EmployeeHow to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 Employee
Celine George
 
Unit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdfUnit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdf
KRUTIKA CHANNE
 
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdfFEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
ChristinaFortunova
 
Overview of Off Boarding in Odoo 18 Employees
Overview of Off Boarding in Odoo 18 EmployeesOverview of Off Boarding in Odoo 18 Employees
Overview of Off Boarding in Odoo 18 Employees
Celine George
 
Ray Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big CycleRay Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big Cycle
Dadang Solihin
 
ROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptx
ROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptxROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptx
ROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptx
Belicia R.S
 
Unit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptxUnit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptx
bobby205207
 
GEOGRAPHY-Study Material [ Class 10th] .pdf
GEOGRAPHY-Study Material [ Class 10th] .pdfGEOGRAPHY-Study Material [ Class 10th] .pdf
GEOGRAPHY-Study Material [ Class 10th] .pdf
SHERAZ AHMAD LONE
 
BINARY files CSV files JSON files with example.pptx
BINARY files CSV files JSON files with example.pptxBINARY files CSV files JSON files with example.pptx
BINARY files CSV files JSON files with example.pptx
Ramakrishna Reddy Bijjam
 
Paper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil Disobedience
Paper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil DisobediencePaper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil Disobedience
Paper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil Disobedience
Rajdeep Bavaliya
 
How to Configure Vendor Management in Lunch App of Odoo 18
How to Configure Vendor Management in Lunch App of Odoo 18How to Configure Vendor Management in Lunch App of Odoo 18
How to Configure Vendor Management in Lunch App of Odoo 18
Celine George
 
What is FIle and explanation of text files.pptx
What is FIle and explanation of text files.pptxWhat is FIle and explanation of text files.pptx
What is FIle and explanation of text files.pptx
Ramakrishna Reddy Bijjam
 
Final Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptxFinal Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptx
bobby205207
 
Black and White Illustrative Group Project Presentation.pdf (1).pdf
Black and White Illustrative Group Project Presentation.pdf (1).pdfBlack and White Illustrative Group Project Presentation.pdf (1).pdf
Black and White Illustrative Group Project Presentation.pdf (1).pdf
AnnasofiaUrsini
 
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
Battle of Bookworms 2025 - U25 Literature Quiz by Pragya
Battle of Bookworms 2025 - U25 Literature Quiz by Pragya Battle of Bookworms 2025 - U25 Literature Quiz by Pragya
Battle of Bookworms 2025 - U25 Literature Quiz by Pragya
Pragya - UEM Kolkata Quiz Club
 
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition OecdEnergy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
razelitouali
 
Exploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle SchoolExploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle School
Marie
 
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/