SlideShare a Scribd company logo
Java collections framework
Commonly reusable collection data structures
Java Collections Framework (JCF)
A.A. 2012/2013Tecniche di programmazione2
Collection
an object that represents a group of objects
Collection Framework
A unified architecture for representing and manipulating
collections
Such collections are manipulated independent of the details of
their representation
“JCF” vs.“ADT”
A little bit of history…
A.A. 2012/2013Tecniche di programmazione3
JDK < 1.2
Standard practice: Vector and Hashtable
Compatibility with C++ StandardTemplate Library (STL)
Doug Lea’s Collections package
ObjectSpace Generic Collection Library (JGL)
JDK ≥1.2
Sun drops compatibility with C++ STL
Joshua Bloch’s JCF
(now Chief Java Architect @ Google)
A little bit of history…
A.A. 2012/2013Tecniche di programmazione4
Java 5
Introduction of <generics>
Clean, safe definition of the Collection Interface
Trees, linked lists, stacks, hash tables, and other classes are
implementations of Collection
Arrays do not implement the Collection interface
Vector redefined to implement Collection
A little bit of history…
A.A. 2012/2013Tecniche di programmazione5
Doug Lea later developed a concurrency package
JCF’s Main Elements
A.A. 2012/2013Tecniche di programmazione6
Infrastructure
Interfaces that provide essential support for the collection
interfaces
General-purpose Implementations
Primary implementations (basic and bulk) of the collection
interfaces
Algorithms
A.A. 2012/2013Tecniche di programmazione7
Algorithms
Static methods that perform useful functions on collections,
such as sorting a list
ICF’s Utility Implementations
A.A. 2012/2013Tecniche di programmazione8
Legacy Implementations
The collection classes from earlier releases,Vector and
Hashtable, have been retrofitted to implement the collection
interfaces
Convenience Implementations
High-performance "mini-implementations" of the collection
interfaces
Wrapper Implementations
Add functionality, such as synchronization, to other
implementations
Abstract Implementations
A.A. 2012/2013Tecniche di programmazione9
Partial implementations (skeletons) of the collection
interfaces to facilitate custom implementations
Infrastructure
A.A. 2012/2013Tecniche di programmazione10
These interfaces form the basis of the framework
Some types of collections allow duplicate elements, others do not
Some types of collections are ordered, others are unordered
The Java platform doesn’t provide any direct implementations
of the Collection interface, but provides implementations of
more specific sub-interfaces, such as Set and List and Maps
Collection interface
A.A. 2012/2013Tecniche di programmazione11
A Collection represents a group of objects known
as its elements
The Collection interface is the least common
denominator that all collections implement.
It is Used
to pass collections around
to manipulate them when maximum generality is desire
Collection extends Iterable
A note on iterators
A.A. 2012/2013Tecniche di programmazione12
An Iterator is an object that enables you to traverse
through a collection (and to remove elements from the
collection selectively)
You get an Iterator for a collection by calling its iterator()
method.
Several languages supports “iterators”. E.g., C++, PHP,
Python, Ruby, Go…
public interface Iterator<E> {
boolean hasNext();
E next();
void remove(); //optional
}
Main Interfaces
A.A. 2012/2013Tecniche di programmazione13
List
A more flexible version of an array
Queue & Priority Queue
The order of arrival does matter, or the urgency
Set
No order, no duplicate elements
Map interface
A.A. 2012/2013Tecniche di programmazione14
A Map is an object that maps keys to values
A map cannot contain duplicate keys: each key can map
to at most one value
Map does not extend Iterable, but it is possible to
get an iterator through entrySet()
Notez bien: Maps do not extend from
java.util.Collection, but they’re still considered to
be part of the “collections framework”
Collection Family Tree
A.A. 2012/2013Tecniche di programmazione15
Collection interface
A.A. 2012/2013Tecniche di programmazione16
public interface Collection<E> extends Iterable<E> {
int size();
boolean isEmpty();
boolean contains(Object element);
boolean add(E element); //optional
boolean remove(Object element); //optional
Iterator<E> iterator();
boolean containsAll(Collection<?> c);
boolean addAll(Collection<? extends E> c); //optional
boolean removeAll(Collection<?> c); //optional
boolean retainAll(Collection<?> c); //optional
void clear(); //optional
Object[] toArray();
<T>T[] toArray(T[] a);
}
Collection interface
A.A. 2012/2013Tecniche di programmazione17
public interface Collection<E> extends Iterable<E> {
int size();
boolean isEmpty();
boolean contains(Object element);
boolean add(E element); //optional
boolean remove(Object element); //optional
Iterator<E> iterator();
boolean containsAll(Collection<?> c);
boolean addAll(Collection<? extends E> c); //optional
boolean removeAll(Collection<?> c); //optional
boolean retainAll(Collection<?> c); //optional
void clear(); //optional
Object[] toArray();
<T>T[] toArray(T[] a);
}
Basic Operations
generics
Collection interface
A.A. 2012/2013Tecniche di programmazione18
public interface Collection<E> extends Iterable<E> {
int size();
boolean isEmpty();
boolean contains(Object element);
boolean add(E element); //optional
boolean remove(Object element); //optional
Iterator<E> iterator();
boolean containsAll(Collection<?> c);
boolean addAll(Collection<? extends E> c); //optional
boolean removeAll(Collection<?> c); //optional
boolean retainAll(Collection<?> c); //optional
void clear(); //optional
Object[] toArray();
<T>T[] toArray(T[] a);
}
Bulk Operations
wildcard
either extends
or implements
Collection interface
A.A. 2012/2013Tecniche di programmazione19
public interface Collection<E> extends Iterable<E> {
int size();
boolean isEmpty();
boolean contains(Object element);
boolean add(E element); //optional
boolean remove(Object element); //optional
Iterator<E> iterator();
boolean containsAll(Collection<?> c);
boolean addAll(Collection<? extends E> c); //optional
boolean removeAll(Collection<?> c); //optional
boolean retainAll(Collection<?> c); //optional
void clear(); //optional
Object[] toArray();
<T>T[] toArray(T[] a);
}
Array Operations
Java collections framework
Licenza d’uso
A.A. 2012/2013Tecniche di programmazione21
Queste diapositive sono distribuite con licenza Creative Commons
“Attribuzione - Non commerciale - Condividi allo stesso modo (CC
BY-NC-SA)”
Sei libero:
di riprodurre, distribuire, comunicare al pubblico, esporre in pubblico,
rappresentare, eseguire e recitare quest'opera
di modificare quest'opera
Alle seguenti condizioni:
Attribuzione — Devi attribuire la paternità dell'opera agli autori
originali e in modo tale da non suggerire che essi avallino te o il modo in
cui tu usi l'opera.
Non commerciale — Non puoi usare quest'opera per fini
commerciali.
Condividi allo stesso modo — Se alteri o trasformi quest'opera, o se
la usi per crearne un'altra, puoi distribuire l'opera risultante solo con una
licenza identica o equivalente a questa.
http://creativecommons.org/licenses/by-nc-sa/3.0/

More Related Content

PPT
Best core & advanced java classes in mumbai
PPT
Collections in Java
PPT
java collections
PPTX
Java collections
PDF
Collections In Java
PPTX
Java generics
PPTX
Java - Collections framework
PDF
Java 8 lambda expressions
Best core & advanced java classes in mumbai
Collections in Java
java collections
Java collections
Collections In Java
Java generics
Java - Collections framework
Java 8 lambda expressions

What's hot (19)

PDF
Java 8 - Project Lambda
PDF
Refactoring to Java 8 (Devoxx BE)
PDF
Pragmatic functional refactoring with java 8
PDF
03. oop concepts
PDF
Java 8: the good parts!
PDF
Programming with Lambda Expressions in Java
PPSX
Collections - Array List
PDF
Understanding C# in .NET
PDF
Java Generics Introduction - Syntax Advantages and Pitfalls
PDF
07 java collection
PPT
Java 8 Streams
PPT
Executing Sql Commands
ODP
(7) c sharp introduction_advanvced_features_part_ii
PPTX
Java 8 Feature Preview
PDF
Java Collections | Collections Framework in Java | Java Tutorial For Beginner...
PDF
Java Fundamentals
PDF
Lambda Expressions in Java
Java 8 - Project Lambda
Refactoring to Java 8 (Devoxx BE)
Pragmatic functional refactoring with java 8
03. oop concepts
Java 8: the good parts!
Programming with Lambda Expressions in Java
Collections - Array List
Understanding C# in .NET
Java Generics Introduction - Syntax Advantages and Pitfalls
07 java collection
Java 8 Streams
Executing Sql Commands
(7) c sharp introduction_advanvced_features_part_ii
Java 8 Feature Preview
Java Collections | Collections Framework in Java | Java Tutorial For Beginner...
Java Fundamentals
Lambda Expressions in Java
Ad

Similar to Java collections framework (20)

PPT
JavaCollections.ppt
PPT
JavaCollections.ppt
PPTX
Java Collection Framework 2nd year B.Tech.pptx
PDF
Lecture 24
PDF
Advanced, Composable Collection Views, From CocoaCoders meetup Austin Feb 12,...
PDF
PPTX
Lambdas and-streams-s ritter-v3
PDF
SCALA - Functional domain
PPTX
New Features of JAVA SE8
PDF
Java collections the force awakens
PPT
Topic-G-JavaCollections Framework.ppt
PDF
C# / Java Language Comparison
PDF
Lambdas & Streams
PDF
Automatic Migration of Legacy Java Method Implementations to Interfaces
PPT
collections
PPTX
A Brief Conceptual Introduction to Functional Java 8 and its API
PPTX
Framework engineering JCO 2011
PDF
Java Collections
PPTX
Project Lambda: Functional Programming Constructs in Java - Simon Ritter (Ora...
JavaCollections.ppt
JavaCollections.ppt
Java Collection Framework 2nd year B.Tech.pptx
Lecture 24
Advanced, Composable Collection Views, From CocoaCoders meetup Austin Feb 12,...
Lambdas and-streams-s ritter-v3
SCALA - Functional domain
New Features of JAVA SE8
Java collections the force awakens
Topic-G-JavaCollections Framework.ppt
C# / Java Language Comparison
Lambdas & Streams
Automatic Migration of Legacy Java Method Implementations to Interfaces
collections
A Brief Conceptual Introduction to Functional Java 8 and its API
Framework engineering JCO 2011
Java Collections
Project Lambda: Functional Programming Constructs in Java - Simon Ritter (Ora...
Ad

Recently uploaded (20)

PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Encapsulation theory and applications.pdf
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Encapsulation_ Review paper, used for researhc scholars
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Approach and Philosophy of On baking technology
PDF
Machine learning based COVID-19 study performance prediction
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Electronic commerce courselecture one. Pdf
MIND Revenue Release Quarter 2 2025 Press Release
NewMind AI Weekly Chronicles - August'25-Week II
Programs and apps: productivity, graphics, security and other tools
Network Security Unit 5.pdf for BCA BBA.
Review of recent advances in non-invasive hemoglobin estimation
Encapsulation theory and applications.pdf
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Encapsulation_ Review paper, used for researhc scholars
“AI and Expert System Decision Support & Business Intelligence Systems”
Assigned Numbers - 2025 - Bluetooth® Document
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Dropbox Q2 2025 Financial Results & Investor Presentation
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Approach and Philosophy of On baking technology
Machine learning based COVID-19 study performance prediction
Spectral efficient network and resource selection model in 5G networks
Digital-Transformation-Roadmap-for-Companies.pptx
MYSQL Presentation for SQL database connectivity
Electronic commerce courselecture one. Pdf

Java collections framework

  • 1. Java collections framework Commonly reusable collection data structures
  • 2. Java Collections Framework (JCF) A.A. 2012/2013Tecniche di programmazione2 Collection an object that represents a group of objects Collection Framework A unified architecture for representing and manipulating collections Such collections are manipulated independent of the details of their representation “JCF” vs.“ADT”
  • 3. A little bit of history… A.A. 2012/2013Tecniche di programmazione3 JDK < 1.2 Standard practice: Vector and Hashtable Compatibility with C++ StandardTemplate Library (STL) Doug Lea’s Collections package ObjectSpace Generic Collection Library (JGL) JDK ≥1.2 Sun drops compatibility with C++ STL Joshua Bloch’s JCF (now Chief Java Architect @ Google)
  • 4. A little bit of history… A.A. 2012/2013Tecniche di programmazione4 Java 5 Introduction of <generics> Clean, safe definition of the Collection Interface Trees, linked lists, stacks, hash tables, and other classes are implementations of Collection Arrays do not implement the Collection interface Vector redefined to implement Collection
  • 5. A little bit of history… A.A. 2012/2013Tecniche di programmazione5 Doug Lea later developed a concurrency package
  • 6. JCF’s Main Elements A.A. 2012/2013Tecniche di programmazione6 Infrastructure Interfaces that provide essential support for the collection interfaces General-purpose Implementations Primary implementations (basic and bulk) of the collection interfaces
  • 7. Algorithms A.A. 2012/2013Tecniche di programmazione7 Algorithms Static methods that perform useful functions on collections, such as sorting a list
  • 8. ICF’s Utility Implementations A.A. 2012/2013Tecniche di programmazione8 Legacy Implementations The collection classes from earlier releases,Vector and Hashtable, have been retrofitted to implement the collection interfaces Convenience Implementations High-performance "mini-implementations" of the collection interfaces Wrapper Implementations Add functionality, such as synchronization, to other implementations
  • 9. Abstract Implementations A.A. 2012/2013Tecniche di programmazione9 Partial implementations (skeletons) of the collection interfaces to facilitate custom implementations
  • 10. Infrastructure A.A. 2012/2013Tecniche di programmazione10 These interfaces form the basis of the framework Some types of collections allow duplicate elements, others do not Some types of collections are ordered, others are unordered The Java platform doesn’t provide any direct implementations of the Collection interface, but provides implementations of more specific sub-interfaces, such as Set and List and Maps
  • 11. Collection interface A.A. 2012/2013Tecniche di programmazione11 A Collection represents a group of objects known as its elements The Collection interface is the least common denominator that all collections implement. It is Used to pass collections around to manipulate them when maximum generality is desire Collection extends Iterable
  • 12. A note on iterators A.A. 2012/2013Tecniche di programmazione12 An Iterator is an object that enables you to traverse through a collection (and to remove elements from the collection selectively) You get an Iterator for a collection by calling its iterator() method. Several languages supports “iterators”. E.g., C++, PHP, Python, Ruby, Go… public interface Iterator<E> { boolean hasNext(); E next(); void remove(); //optional }
  • 13. Main Interfaces A.A. 2012/2013Tecniche di programmazione13 List A more flexible version of an array Queue & Priority Queue The order of arrival does matter, or the urgency Set No order, no duplicate elements
  • 14. Map interface A.A. 2012/2013Tecniche di programmazione14 A Map is an object that maps keys to values A map cannot contain duplicate keys: each key can map to at most one value Map does not extend Iterable, but it is possible to get an iterator through entrySet() Notez bien: Maps do not extend from java.util.Collection, but they’re still considered to be part of the “collections framework”
  • 15. Collection Family Tree A.A. 2012/2013Tecniche di programmazione15
  • 16. Collection interface A.A. 2012/2013Tecniche di programmazione16 public interface Collection<E> extends Iterable<E> { int size(); boolean isEmpty(); boolean contains(Object element); boolean add(E element); //optional boolean remove(Object element); //optional Iterator<E> iterator(); boolean containsAll(Collection<?> c); boolean addAll(Collection<? extends E> c); //optional boolean removeAll(Collection<?> c); //optional boolean retainAll(Collection<?> c); //optional void clear(); //optional Object[] toArray(); <T>T[] toArray(T[] a); }
  • 17. Collection interface A.A. 2012/2013Tecniche di programmazione17 public interface Collection<E> extends Iterable<E> { int size(); boolean isEmpty(); boolean contains(Object element); boolean add(E element); //optional boolean remove(Object element); //optional Iterator<E> iterator(); boolean containsAll(Collection<?> c); boolean addAll(Collection<? extends E> c); //optional boolean removeAll(Collection<?> c); //optional boolean retainAll(Collection<?> c); //optional void clear(); //optional Object[] toArray(); <T>T[] toArray(T[] a); } Basic Operations generics
  • 18. Collection interface A.A. 2012/2013Tecniche di programmazione18 public interface Collection<E> extends Iterable<E> { int size(); boolean isEmpty(); boolean contains(Object element); boolean add(E element); //optional boolean remove(Object element); //optional Iterator<E> iterator(); boolean containsAll(Collection<?> c); boolean addAll(Collection<? extends E> c); //optional boolean removeAll(Collection<?> c); //optional boolean retainAll(Collection<?> c); //optional void clear(); //optional Object[] toArray(); <T>T[] toArray(T[] a); } Bulk Operations wildcard either extends or implements
  • 19. Collection interface A.A. 2012/2013Tecniche di programmazione19 public interface Collection<E> extends Iterable<E> { int size(); boolean isEmpty(); boolean contains(Object element); boolean add(E element); //optional boolean remove(Object element); //optional Iterator<E> iterator(); boolean containsAll(Collection<?> c); boolean addAll(Collection<? extends E> c); //optional boolean removeAll(Collection<?> c); //optional boolean retainAll(Collection<?> c); //optional void clear(); //optional Object[] toArray(); <T>T[] toArray(T[] a); } Array Operations
  • 21. Licenza d’uso A.A. 2012/2013Tecniche di programmazione21 Queste diapositive sono distribuite con licenza Creative Commons “Attribuzione - Non commerciale - Condividi allo stesso modo (CC BY-NC-SA)” Sei libero: di riprodurre, distribuire, comunicare al pubblico, esporre in pubblico, rappresentare, eseguire e recitare quest'opera di modificare quest'opera Alle seguenti condizioni: Attribuzione — Devi attribuire la paternità dell'opera agli autori originali e in modo tale da non suggerire che essi avallino te o il modo in cui tu usi l'opera. Non commerciale — Non puoi usare quest'opera per fini commerciali. Condividi allo stesso modo — Se alteri o trasformi quest'opera, o se la usi per crearne un'altra, puoi distribuire l'opera risultante solo con una licenza identica o equivalente a questa. http://creativecommons.org/licenses/by-nc-sa/3.0/