SlideShare a Scribd company logo
SEG4110 – Advanced Software Design
and Reengineering
TOPIC G
Java Collections Framework
SEG4110 - Topic G - Java Collections Framework 2
Collections Frameworks
• A collection is an object that represents a group of objects
• A collections framework is a unified architecture for
representing and manipulating collections
- the manipulation should be independent of the implementation details
• Advantages of having a collections framework
- reduces programming effort by providing useful data structures and
algorithms
- increases performance by providing high-performance data structures
and algorithms
- interoperability between the different collections
- Having a common language for manipulating the collections
SEG4110 - Topic G - Java Collections Framework 3
The components of Java Collections Framework
from the user’s perspective
• Collection Interfaces: form the basis of the framework
- such as sets, lists and maps
• Implementations Classes: implementations of the collection interfaces
• Utilities - Utility functions for manipulating collections such as
sorting, searching…
• The Java Collections Framework also includes:
- algorithms
- wrapper implementations
- add functionality to other implementations such as synchronization
SEG4110 - Topic G - Java Collections Framework 4
Collections Interfaces
SortedMap
Map
Collection
Set
SortedSet
List
• Java allows using:
• Lists
• Sets
• Hash tables (maps)
SEG4110 - Topic G - Java Collections Framework 5
Interface: Collection
• The Collection interface is the root
of the collection hierarchy
• Some Collection implementations
allow
- duplicate elements and others do not
- the elements to be ordered or not
• JDK doesn't provide any direct
implementations of this interface
- It provides implementations of more
specific sub interfaces like Set and List
public interface Collection {
// Basic Operations
int size();
boolean isEmpty();
boolean contains(Object element);
boolean add(Object element);
boolean remove(Object element);
Iterator iterator();
// Bulk Operations
boolean containsAll(Collection c);
boolean addAll(Collection c);
boolean removeAll(Collection c);
boolean retainAll(Collection c);
void clear();
// Array Operations
Object[] toArray();
Object[] toArray(Object a[]);
}
SEG4110 - Topic G - Java Collections Framework 6
Interface: Set
• A Set is a collection that
cannot contain duplicate
elements
• A set is not ordered
- however, some subsets
maintain order using extra data
structures
• This is similar to the
mathematical concept of sets
public interface Set {
// Basic Operations
int size();
boolean isEmpty();
boolean contains(Object element);
boolean add(Object element);
boolean remove(Object element);
Iterator iterator();
// Bulk Operations
boolean containsAll(Collection c);
boolean addAll(Collection c);
boolean removeAll(Collection c);
boolean retainAll(Collection c);
void clear();
// Array Operations
Object[] toArray();
Object[] toArray(Object a[]);
}
SEG4110 - Topic G - Java Collections Framework 7
Interface: List
• A List is ordered (also
called a sequence)
• Lists can contain duplicate
elements
• The user can access
elements by their integer
index (position)
public interface List extends Collection {
// Positional Access
Object get(int index);
Object set(int index, Object element);
void add(int index, Object element);
Object remove(int index);
abstract boolean addAll(int index, Collection c);
// Search
int indexOf(Object o);
int lastIndexOf(Object o);
// Iteration
ListIterator listIterator();
ListIterator listIterator(int index);
// Range-view
List subList(int from, int to);
}
SEG4110 - Topic G - Java Collections Framework 8
Interface: Map
• A Map is an object that maps
keys to values
• Maps cannot contain
duplicate keys
• Each key can map to at most
one value
• Hashtables are an example of
Maps
public interface Map {
// Basic Operations
Object put(Object key, Object value);
Object get(Object key);
Object remove(Object key);
boolean containsKey(Object key);
boolean containsValue(Object value);
int size();
boolean isEmpty();
// Bulk Operations
void putAll(Map t);
void clear();
// Collection Views
public Set keySet();
public Collection values();
public Set entrySet();
// Interface for entrySet elements
public interface Entry {
Object getKey();
Object getValue();
Object setValue(Object value);
}
}
SEG4110 - Topic G - Java Collections Framework 9
Interface: SortedSet
• A SortedSet is a Set that
maintains its elements in an
order
• Several additional operations
are provided to take
advantage of the ordering
public interface SortedSet extends Set {
// Range-view
SortedSet subSet(Object fromElement, Object
toElement);
SortedSet headSet(Object toElement);
SortedSet tailSet(Object fromElement);
// Endpoints
Object first();
Object last();
// Comparator access
Comparator comparator();
}
SEG4110 - Topic G - Java Collections Framework 10
Interface: SortedMap
• A SortedMap is a Map that
maintains its mappings in
ascending key order
• The SortedMap interface is
used for apps like dictionaries
and telephone directories
public interface SortedMap extends Map {
Comparator comparator();
SortedMap subMap(Object fromKey, Object
toKey);
SortedMap headMap(Object toKey);
SortedMap tailMap(Object fromKey);
Object firstKey();
Object lastKey();
}
SEG4110 - Topic G - Java Collections Framework 11
Java
Collections Object
(java.lang)
AbstractList
LinkedHashMap
HashMap
AbstractSequentialList
ArrayList
Stack
Vector
LinkedHashSet
HashSet
TreeSet
AbstractCollection
Collection
List
AbstractSet
Set
SortedSet
Map
SortedMap
TreeMap
LinkedList
AbstractMap
Dictionary
Map
Hashtable
SEG4110 - Topic G - Java Collections Framework 12
Java Lists: LinkedList
• Java uses a doubly-linked list
- it can be traversed from the beginning or
the end
• LinkedList provides methods to get,
remove and insert an element at the
beginning and end of the list
- these operations allow a linked list to be
used as a stack or a queue
• LinkedList is not synchronized
- problems if multiple threads access a list
concurrently
- LinkedList must be synchronized externally
AbstractList
AbstractSequentialList
ArrayList
Stack
Vector
LinkedList
List
AbstractCollection
Collection
SEG4110 - Topic G - Java Collections Framework 13
Java Lists: ArrayList
• Uses an array to store the elements
• In addition to the methods of the
interface List
• it provides methods to manipulate the
size of the array (e.g. ensureCapacity)
• More efficient than LinkedList for
methods involving indices – get(),
set()
• It is not synchronized
AbstractList
AbstractSequentialList
ArrayList
Stack
Vector
LinkedList
List
AbstractCollection
Collection
SEG4110 - Topic G - Java Collections Framework 14
Java Lists: Vector
• Same as an the class ArrayList
• The main difference is that:
- The methods of Vector are
synchronized
AbstractList
AbstractSequentialList
ArrayList
Stack
Vector
LinkedList
List
AbstractCollection
Collection
SEG4110 - Topic G - Java Collections Framework 15
Java Lists: Stack
• The Stack class represents a last-in-
first-out (LIFO) stack of objects
• The common push and pop operations
are provided
• As well as a method to peek at the top
item on the stack is also provided
• Note: It is considered bad design to
make Stack a Subclass of vector
• Vector operations should not be
accessible in a Stack
• It is designed this way because of
a historical mistake
AbstractList
AbstractSequentialList
ArrayList
Stack
Vector
LinkedList
List
AbstractCollection
Collection
SEG4110 - Topic G - Java Collections Framework 16
Java Sets: HashSet
• HashSet uses a hash table as the data
structure to represent a set
• HashSet is a good choice for representing
sets if order of the elements is not
important
• HashSet methods are not synchronized
LinkedHashSet
HashSet
TreeSet
AbstractSet
Set
SortedSet
AbstractCollection
Collection
SEG4110 - Topic G - Java Collections Framework 17
Java Sets: LinkedHashSet
• Hash table and linked list
implementation of the Set interface
• LinkedHashSet differs from HashSet
in that the order is maintained
• Performance is below that of
HashSet, due to the expense of
maintaining the linked list
• Its methods are not synchronized
LinkedHashSet
HashSet
TreeSet
AbstractSet
Set
SortedSet
AbstractCollection
Collection
SEG4110 - Topic G - Java Collections Framework 18
Java Sets: TreeSet
• Stores the elements in a balanced
binary tree
- A binary tree is a tree in which each
node has at most two children
• TreeSet elements are sorted
• Less efficient than HashSet in
insertion due to the use of a binary
tree
• Its methods are not synchronized
LinkedHashSet
HashSet
TreeSet
AbstractSet
Set
SortedSet
AbstractCollection
Collection
SEG4110 - Topic G - Java Collections Framework 19
Java Maps: HashMap
• Stores the entries in a hash
table
• Efficient in inserting (put())
and retrieving elements (get())
• The order is not maintained
• Unlike HashTable, HashMap’s
methods are not synchronized
LinkedHashMap
HashMap
Map
SortedMap
TreeMap
AbstractMap
SEG4110 - Topic G - Java Collections Framework 20
Java Maps: LinkedHashMap
• Hash table and linked list
implementation of the Map
interface
• LinkedHashMap differs from
HashMap in that the order is
maintained
• Performance is below that of
HashMap, due to the expense of
maintaining the linked list
• Its methods are not synchronized
LinkedHashMap
HashMap
Map
SortedMap
TreeMap
AbstractMap
SEG4110 - Topic G - Java Collections Framework 21
Java Maps: TreeMap
• Red-Black tree based
implementation of the SortedMap
interface
• The keys are sorted according to
their natural order
• Less efficient than HashMap for
insertion and mapping
• Its methods are not synchronized
LinkedHashMap
HashMap
Map
SortedMap
TreeMap
AbstractMap
SEG4110 - Topic G - Java Collections Framework 22
The class Hashtable
• Same as HashMap except that its
methods are synchronized
• The class Hashtable was
introduced in JDK 1.0 and uses
Enumerations instead of Iterators
Dictionary
Map
Hashtable
SEG4110 - Topic G - Java Collections Framework 23
The Iterator Interface
• JCF provides a uniform way to iterate through the collection elements
using the Iterator Interface
• The Iterator interface contains the following methods
- boolean hasNext(): returns true if the iteration has more elements.
- Object next(): returns the next element in the iteration.
- void remove(): removes from the collection the last element returned by the
iterator
• Iterator replaces Enumeration in the Java Collections Framework.
Iterators differ from enumerations in two ways:
- They allow the caller to remove elements from the collection during the
iteration with well-defined semantics (a major drawback of Enumerations)
- Method names have been improved
SEG4110 - Topic G - Java Collections Framework 24
The class Arrays
• This class contains various static methods for manipulating arrays
such as:
- Sorting, comparing collections, binary searching…
• Refer to: http://java.sun.com/j2se/1.4.2/docs/api/ for a complete list of
utilities
SEG4110 - Topic G - Java Collections Framework 25
References
The Java Collections Framework:
http://java.sun.com/j2se/1.4.2/docs/guide/collections/inde
x.html
The Java 1.4.2 APIs:
http://java.sun.com/j2se/1.4.2/docs/api/
Ad

More Related Content

Similar to Topic-G-JavaCollections Framework.ppt (20)

Best core & advanced java classes in mumbai
Best core & advanced java classes in mumbaiBest core & advanced java classes in mumbai
Best core & advanced java classes in mumbai
Vibrant Technologies & Computers
 
JavaCollections.ppt
JavaCollections.pptJavaCollections.ppt
JavaCollections.ppt
boopathirajaraja1
 
JavaCollections.ppt
JavaCollections.pptJavaCollections.ppt
JavaCollections.ppt
Irfanhabeeb18
 
Collections
CollectionsCollections
Collections
Gujarat Technological University
 
Java Collection Framework 2nd year B.Tech.pptx
Java Collection Framework 2nd year B.Tech.pptxJava Collection Framework 2nd year B.Tech.pptx
Java Collection Framework 2nd year B.Tech.pptx
kmd198809
 
Slide Dasar Materi Java Collection Framework
Slide Dasar Materi Java Collection FrameworkSlide Dasar Materi Java Collection Framework
Slide Dasar Materi Java Collection Framework
Bayu Rimba
 
Collection framework
Collection frameworkCollection framework
Collection framework
Jainul Musani
 
Collections
CollectionsCollections
Collections
Marwa Dosoky
 
Collection framework
Collection frameworkCollection framework
Collection framework
BindhuBhargaviTalasi
 
Pptchdtdtfygugyxthgihhihigugufydtdfzrzrzrtdyfyfy
PptchdtdtfygugyxthgihhihigugufydtdfzrzrzrtdyfyfyPptchdtdtfygugyxthgihhihigugufydtdfzrzrzrtdyfyfy
Pptchdtdtfygugyxthgihhihigugufydtdfzrzrzrtdyfyfy
dnthulk
 
Lecture 24
Lecture 24Lecture 24
Lecture 24
Debasish Pratihari
 
Java Collections
Java  Collections Java  Collections
Java Collections
Kongu Engineering College, Perundurai, Erode
 
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
 
VTUOOPMCA5THMODULECollection OverVi.pptx
VTUOOPMCA5THMODULECollection OverVi.pptxVTUOOPMCA5THMODULECollection OverVi.pptx
VTUOOPMCA5THMODULECollection OverVi.pptx
VeenaNaik23
 
VTUOOPMCA5THMODULEvCollection OverV.pptx
VTUOOPMCA5THMODULEvCollection OverV.pptxVTUOOPMCA5THMODULEvCollection OverV.pptx
VTUOOPMCA5THMODULEvCollection OverV.pptx
VeenaNaik23
 
Queue collection of Frame work in oops through java
Queue collection of Frame work in oops through javaQueue collection of Frame work in oops through java
Queue collection of Frame work in oops through java
bhavanibhanu3456
 
Java collection
Java collectionJava collection
Java collection
Deepak Kumar
 
collectionsframework210616084411 (1).pptx
collectionsframework210616084411 (1).pptxcollectionsframework210616084411 (1).pptx
collectionsframework210616084411 (1).pptx
ArunPatrick2
 
Advanced Java - UNIT 3.pptx
Advanced Java - UNIT 3.pptxAdvanced Java - UNIT 3.pptx
Advanced Java - UNIT 3.pptx
eyemitra1
 
Java Collection Framework 2nd year B.Tech.pptx
Java Collection Framework 2nd year B.Tech.pptxJava Collection Framework 2nd year B.Tech.pptx
Java Collection Framework 2nd year B.Tech.pptx
kmd198809
 
Slide Dasar Materi Java Collection Framework
Slide Dasar Materi Java Collection FrameworkSlide Dasar Materi Java Collection Framework
Slide Dasar Materi Java Collection Framework
Bayu Rimba
 
Collection framework
Collection frameworkCollection framework
Collection framework
Jainul Musani
 
Pptchdtdtfygugyxthgihhihigugufydtdfzrzrzrtdyfyfy
PptchdtdtfygugyxthgihhihigugufydtdfzrzrzrtdyfyfyPptchdtdtfygugyxthgihhihigugufydtdfzrzrzrtdyfyfy
Pptchdtdtfygugyxthgihhihigugufydtdfzrzrzrtdyfyfy
dnthulk
 
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
 
VTUOOPMCA5THMODULECollection OverVi.pptx
VTUOOPMCA5THMODULECollection OverVi.pptxVTUOOPMCA5THMODULECollection OverVi.pptx
VTUOOPMCA5THMODULECollection OverVi.pptx
VeenaNaik23
 
VTUOOPMCA5THMODULEvCollection OverV.pptx
VTUOOPMCA5THMODULEvCollection OverV.pptxVTUOOPMCA5THMODULEvCollection OverV.pptx
VTUOOPMCA5THMODULEvCollection OverV.pptx
VeenaNaik23
 
Queue collection of Frame work in oops through java
Queue collection of Frame work in oops through javaQueue collection of Frame work in oops through java
Queue collection of Frame work in oops through java
bhavanibhanu3456
 
collectionsframework210616084411 (1).pptx
collectionsframework210616084411 (1).pptxcollectionsframework210616084411 (1).pptx
collectionsframework210616084411 (1).pptx
ArunPatrick2
 
Advanced Java - UNIT 3.pptx
Advanced Java - UNIT 3.pptxAdvanced Java - UNIT 3.pptx
Advanced Java - UNIT 3.pptx
eyemitra1
 

More from swapnilslide2019 (13)

Collectionsand GenericsInJavaProgramming.ppt
Collectionsand GenericsInJavaProgramming.pptCollectionsand GenericsInJavaProgramming.ppt
Collectionsand GenericsInJavaProgramming.ppt
swapnilslide2019
 
Flow Network Introduction
Flow Network IntroductionFlow Network Introduction
Flow Network Introduction
swapnilslide2019
 
kdtrees.pdf
kdtrees.pdfkdtrees.pdf
kdtrees.pdf
swapnilslide2019
 
AA_Unit_6.pptx
AA_Unit_6.pptxAA_Unit_6.pptx
AA_Unit_6.pptx
swapnilslide2019
 
AA_Unit_4.pptx
AA_Unit_4.pptxAA_Unit_4.pptx
AA_Unit_4.pptx
swapnilslide2019
 
AA_Unit_3.pptx
AA_Unit_3.pptxAA_Unit_3.pptx
AA_Unit_3.pptx
swapnilslide2019
 
AA_Unit_2.pptx
AA_Unit_2.pptxAA_Unit_2.pptx
AA_Unit_2.pptx
swapnilslide2019
 
AA_Unit 1_part-II.pdf
AA_Unit 1_part-II.pdfAA_Unit 1_part-II.pdf
AA_Unit 1_part-II.pdf
swapnilslide2019
 
AA_Unit 1_part-I.pptx
AA_Unit 1_part-I.pptxAA_Unit 1_part-I.pptx
AA_Unit 1_part-I.pptx
swapnilslide2019
 
Programming Laboratory Unit 1.pdf
Programming Laboratory Unit 1.pdfProgramming Laboratory Unit 1.pdf
Programming Laboratory Unit 1.pdf
swapnilslide2019
 
CI.pdf
CI.pdfCI.pdf
CI.pdf
swapnilslide2019
 
Ad

Recently uploaded (20)

Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Library Association of Ireland
 
To study the nervous system of insect.pptx
To study the nervous system of insect.pptxTo study the nervous system of insect.pptx
To study the nervous system of insect.pptx
Arshad Shaikh
 
Geography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjectsGeography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjects
ProfDrShaikhImran
 
2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx
contactwilliamm2546
 
Quality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdfQuality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdf
Dr. Bindiya Chauhan
 
Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025
Mebane Rash
 
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
larencebapu132
 
Political History of Pala dynasty Pala Rulers NEP.pptx
Political History of Pala dynasty Pala Rulers NEP.pptxPolitical History of Pala dynasty Pala Rulers NEP.pptx
Political History of Pala dynasty Pala Rulers NEP.pptx
Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 AccountingHow to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
Celine George
 
Presentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem KayaPresentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem Kaya
MIPLM
 
How to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odooHow to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odoo
Celine George
 
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Library Association of Ireland
 
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Celine George
 
P-glycoprotein pamphlet: iteration 4 of 4 final
P-glycoprotein pamphlet: iteration 4 of 4 finalP-glycoprotein pamphlet: iteration 4 of 4 final
P-glycoprotein pamphlet: iteration 4 of 4 final
bs22n2s
 
Social Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy StudentsSocial Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy Students
DrNidhiAgarwal
 
To study Digestive system of insect.pptx
To study Digestive system of insect.pptxTo study Digestive system of insect.pptx
To study Digestive system of insect.pptx
Arshad Shaikh
 
New Microsoft PowerPoint Presentation.pptx
New Microsoft PowerPoint Presentation.pptxNew Microsoft PowerPoint Presentation.pptx
New Microsoft PowerPoint Presentation.pptx
milanasargsyan5
 
Understanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s GuideUnderstanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s Guide
GS Virdi
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-3-2025.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 5-3-2025.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 5-3-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-3-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
SPRING FESTIVITIES - UK AND USA -
SPRING FESTIVITIES - UK AND USA            -SPRING FESTIVITIES - UK AND USA            -
SPRING FESTIVITIES - UK AND USA -
Colégio Santa Teresinha
 
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Library Association of Ireland
 
To study the nervous system of insect.pptx
To study the nervous system of insect.pptxTo study the nervous system of insect.pptx
To study the nervous system of insect.pptx
Arshad Shaikh
 
Geography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjectsGeography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjects
ProfDrShaikhImran
 
2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx
contactwilliamm2546
 
Quality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdfQuality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdf
Dr. Bindiya Chauhan
 
Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025
Mebane Rash
 
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
larencebapu132
 
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 AccountingHow to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
Celine George
 
Presentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem KayaPresentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem Kaya
MIPLM
 
How to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odooHow to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odoo
Celine George
 
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Library Association of Ireland
 
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Celine George
 
P-glycoprotein pamphlet: iteration 4 of 4 final
P-glycoprotein pamphlet: iteration 4 of 4 finalP-glycoprotein pamphlet: iteration 4 of 4 final
P-glycoprotein pamphlet: iteration 4 of 4 final
bs22n2s
 
Social Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy StudentsSocial Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy Students
DrNidhiAgarwal
 
To study Digestive system of insect.pptx
To study Digestive system of insect.pptxTo study Digestive system of insect.pptx
To study Digestive system of insect.pptx
Arshad Shaikh
 
New Microsoft PowerPoint Presentation.pptx
New Microsoft PowerPoint Presentation.pptxNew Microsoft PowerPoint Presentation.pptx
New Microsoft PowerPoint Presentation.pptx
milanasargsyan5
 
Understanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s GuideUnderstanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s Guide
GS Virdi
 
Ad

Topic-G-JavaCollections Framework.ppt

  • 1. SEG4110 – Advanced Software Design and Reengineering TOPIC G Java Collections Framework
  • 2. SEG4110 - Topic G - Java Collections Framework 2 Collections Frameworks • A collection is an object that represents a group of objects • A collections framework is a unified architecture for representing and manipulating collections - the manipulation should be independent of the implementation details • Advantages of having a collections framework - reduces programming effort by providing useful data structures and algorithms - increases performance by providing high-performance data structures and algorithms - interoperability between the different collections - Having a common language for manipulating the collections
  • 3. SEG4110 - Topic G - Java Collections Framework 3 The components of Java Collections Framework from the user’s perspective • Collection Interfaces: form the basis of the framework - such as sets, lists and maps • Implementations Classes: implementations of the collection interfaces • Utilities - Utility functions for manipulating collections such as sorting, searching… • The Java Collections Framework also includes: - algorithms - wrapper implementations - add functionality to other implementations such as synchronization
  • 4. SEG4110 - Topic G - Java Collections Framework 4 Collections Interfaces SortedMap Map Collection Set SortedSet List • Java allows using: • Lists • Sets • Hash tables (maps)
  • 5. SEG4110 - Topic G - Java Collections Framework 5 Interface: Collection • The Collection interface is the root of the collection hierarchy • Some Collection implementations allow - duplicate elements and others do not - the elements to be ordered or not • JDK doesn't provide any direct implementations of this interface - It provides implementations of more specific sub interfaces like Set and List public interface Collection { // Basic Operations int size(); boolean isEmpty(); boolean contains(Object element); boolean add(Object element); boolean remove(Object element); Iterator iterator(); // Bulk Operations boolean containsAll(Collection c); boolean addAll(Collection c); boolean removeAll(Collection c); boolean retainAll(Collection c); void clear(); // Array Operations Object[] toArray(); Object[] toArray(Object a[]); }
  • 6. SEG4110 - Topic G - Java Collections Framework 6 Interface: Set • A Set is a collection that cannot contain duplicate elements • A set is not ordered - however, some subsets maintain order using extra data structures • This is similar to the mathematical concept of sets public interface Set { // Basic Operations int size(); boolean isEmpty(); boolean contains(Object element); boolean add(Object element); boolean remove(Object element); Iterator iterator(); // Bulk Operations boolean containsAll(Collection c); boolean addAll(Collection c); boolean removeAll(Collection c); boolean retainAll(Collection c); void clear(); // Array Operations Object[] toArray(); Object[] toArray(Object a[]); }
  • 7. SEG4110 - Topic G - Java Collections Framework 7 Interface: List • A List is ordered (also called a sequence) • Lists can contain duplicate elements • The user can access elements by their integer index (position) public interface List extends Collection { // Positional Access Object get(int index); Object set(int index, Object element); void add(int index, Object element); Object remove(int index); abstract boolean addAll(int index, Collection c); // Search int indexOf(Object o); int lastIndexOf(Object o); // Iteration ListIterator listIterator(); ListIterator listIterator(int index); // Range-view List subList(int from, int to); }
  • 8. SEG4110 - Topic G - Java Collections Framework 8 Interface: Map • A Map is an object that maps keys to values • Maps cannot contain duplicate keys • Each key can map to at most one value • Hashtables are an example of Maps public interface Map { // Basic Operations Object put(Object key, Object value); Object get(Object key); Object remove(Object key); boolean containsKey(Object key); boolean containsValue(Object value); int size(); boolean isEmpty(); // Bulk Operations void putAll(Map t); void clear(); // Collection Views public Set keySet(); public Collection values(); public Set entrySet(); // Interface for entrySet elements public interface Entry { Object getKey(); Object getValue(); Object setValue(Object value); } }
  • 9. SEG4110 - Topic G - Java Collections Framework 9 Interface: SortedSet • A SortedSet is a Set that maintains its elements in an order • Several additional operations are provided to take advantage of the ordering public interface SortedSet extends Set { // Range-view SortedSet subSet(Object fromElement, Object toElement); SortedSet headSet(Object toElement); SortedSet tailSet(Object fromElement); // Endpoints Object first(); Object last(); // Comparator access Comparator comparator(); }
  • 10. SEG4110 - Topic G - Java Collections Framework 10 Interface: SortedMap • A SortedMap is a Map that maintains its mappings in ascending key order • The SortedMap interface is used for apps like dictionaries and telephone directories public interface SortedMap extends Map { Comparator comparator(); SortedMap subMap(Object fromKey, Object toKey); SortedMap headMap(Object toKey); SortedMap tailMap(Object fromKey); Object firstKey(); Object lastKey(); }
  • 11. SEG4110 - Topic G - Java Collections Framework 11 Java Collections Object (java.lang) AbstractList LinkedHashMap HashMap AbstractSequentialList ArrayList Stack Vector LinkedHashSet HashSet TreeSet AbstractCollection Collection List AbstractSet Set SortedSet Map SortedMap TreeMap LinkedList AbstractMap Dictionary Map Hashtable
  • 12. SEG4110 - Topic G - Java Collections Framework 12 Java Lists: LinkedList • Java uses a doubly-linked list - it can be traversed from the beginning or the end • LinkedList provides methods to get, remove and insert an element at the beginning and end of the list - these operations allow a linked list to be used as a stack or a queue • LinkedList is not synchronized - problems if multiple threads access a list concurrently - LinkedList must be synchronized externally AbstractList AbstractSequentialList ArrayList Stack Vector LinkedList List AbstractCollection Collection
  • 13. SEG4110 - Topic G - Java Collections Framework 13 Java Lists: ArrayList • Uses an array to store the elements • In addition to the methods of the interface List • it provides methods to manipulate the size of the array (e.g. ensureCapacity) • More efficient than LinkedList for methods involving indices – get(), set() • It is not synchronized AbstractList AbstractSequentialList ArrayList Stack Vector LinkedList List AbstractCollection Collection
  • 14. SEG4110 - Topic G - Java Collections Framework 14 Java Lists: Vector • Same as an the class ArrayList • The main difference is that: - The methods of Vector are synchronized AbstractList AbstractSequentialList ArrayList Stack Vector LinkedList List AbstractCollection Collection
  • 15. SEG4110 - Topic G - Java Collections Framework 15 Java Lists: Stack • The Stack class represents a last-in- first-out (LIFO) stack of objects • The common push and pop operations are provided • As well as a method to peek at the top item on the stack is also provided • Note: It is considered bad design to make Stack a Subclass of vector • Vector operations should not be accessible in a Stack • It is designed this way because of a historical mistake AbstractList AbstractSequentialList ArrayList Stack Vector LinkedList List AbstractCollection Collection
  • 16. SEG4110 - Topic G - Java Collections Framework 16 Java Sets: HashSet • HashSet uses a hash table as the data structure to represent a set • HashSet is a good choice for representing sets if order of the elements is not important • HashSet methods are not synchronized LinkedHashSet HashSet TreeSet AbstractSet Set SortedSet AbstractCollection Collection
  • 17. SEG4110 - Topic G - Java Collections Framework 17 Java Sets: LinkedHashSet • Hash table and linked list implementation of the Set interface • LinkedHashSet differs from HashSet in that the order is maintained • Performance is below that of HashSet, due to the expense of maintaining the linked list • Its methods are not synchronized LinkedHashSet HashSet TreeSet AbstractSet Set SortedSet AbstractCollection Collection
  • 18. SEG4110 - Topic G - Java Collections Framework 18 Java Sets: TreeSet • Stores the elements in a balanced binary tree - A binary tree is a tree in which each node has at most two children • TreeSet elements are sorted • Less efficient than HashSet in insertion due to the use of a binary tree • Its methods are not synchronized LinkedHashSet HashSet TreeSet AbstractSet Set SortedSet AbstractCollection Collection
  • 19. SEG4110 - Topic G - Java Collections Framework 19 Java Maps: HashMap • Stores the entries in a hash table • Efficient in inserting (put()) and retrieving elements (get()) • The order is not maintained • Unlike HashTable, HashMap’s methods are not synchronized LinkedHashMap HashMap Map SortedMap TreeMap AbstractMap
  • 20. SEG4110 - Topic G - Java Collections Framework 20 Java Maps: LinkedHashMap • Hash table and linked list implementation of the Map interface • LinkedHashMap differs from HashMap in that the order is maintained • Performance is below that of HashMap, due to the expense of maintaining the linked list • Its methods are not synchronized LinkedHashMap HashMap Map SortedMap TreeMap AbstractMap
  • 21. SEG4110 - Topic G - Java Collections Framework 21 Java Maps: TreeMap • Red-Black tree based implementation of the SortedMap interface • The keys are sorted according to their natural order • Less efficient than HashMap for insertion and mapping • Its methods are not synchronized LinkedHashMap HashMap Map SortedMap TreeMap AbstractMap
  • 22. SEG4110 - Topic G - Java Collections Framework 22 The class Hashtable • Same as HashMap except that its methods are synchronized • The class Hashtable was introduced in JDK 1.0 and uses Enumerations instead of Iterators Dictionary Map Hashtable
  • 23. SEG4110 - Topic G - Java Collections Framework 23 The Iterator Interface • JCF provides a uniform way to iterate through the collection elements using the Iterator Interface • The Iterator interface contains the following methods - boolean hasNext(): returns true if the iteration has more elements. - Object next(): returns the next element in the iteration. - void remove(): removes from the collection the last element returned by the iterator • Iterator replaces Enumeration in the Java Collections Framework. Iterators differ from enumerations in two ways: - They allow the caller to remove elements from the collection during the iteration with well-defined semantics (a major drawback of Enumerations) - Method names have been improved
  • 24. SEG4110 - Topic G - Java Collections Framework 24 The class Arrays • This class contains various static methods for manipulating arrays such as: - Sorting, comparing collections, binary searching… • Refer to: http://java.sun.com/j2se/1.4.2/docs/api/ for a complete list of utilities
  • 25. SEG4110 - Topic G - Java Collections Framework 25 References The Java Collections Framework: http://java.sun.com/j2se/1.4.2/docs/guide/collections/inde x.html The Java 1.4.2 APIs: http://java.sun.com/j2se/1.4.2/docs/api/