SlideShare a Scribd company logo
Java Collections
APIs and Versions
• Number one hint for programming with Java
Collections: use the API
▫ http://java.sun.com/j2se/1.5.0/docs/api/java/util
/Collection.html

• Be sure to use the 1.5.0 APIs to get the version
with generics
Java Collections Framework
• The Java language API provides many of the
data structures from this class for you.
• It defines a “collection” as “an object that
represents a group of objects”.
• It defines a collections framework as “a unified
architecture for representing and manipulating
collections, allowing them to be manipulated
independent of the details of their
representation.”
Collections Framework (cont)
• Collection Interfaces - Represent different types of collections, such as sets, lists
and maps. These interfaces form the basis of the framework.
• General-purpose Implementations - Primary implementations of the collection
interfaces.
• Legacy Implementations - The collection classes from earlier releases, Vector and
Hashtable, have been retrofitted to implement the collection interfaces.
• Wrapper Implementations - Add functionality, such as synchronization, to other
implementations.
• Convenience Implementations - High-performance "mini-implementations" of
the collection interfaces.
• Abstract Implementations - Partial implementations of the collection interfaces
to facilitate custom implementations.
• Algorithms - Static methods that perform useful functions on collections, such as
sorting a list.
• Infrastructure - Interfaces that provide essential support for the collection
interfaces.
• Array Utilities - Utility functions for arrays of primitives and reference objects.
Not, strictly speaking, a part of the Collections Framework, this functionality is being
added to the Java platform at the same time and relies on some of the same
infrastructure.
Collection interfaces
• The core collection interfaces encapsulate
different types of collections. They represent the
abstract data types that are part of the
collections framework. They are interfaces so
they do not provide an implementation!
public interface Collection<E>
extends Iterable<E>
• Collection — the root of the collection hierarchy. A
collection represents a group of objects known as its
elements. The Collection interface is the least common
denominator that all collections implement and is used
to pass collections around and to manipulate them when
maximum generality is desired. Some types of
collections allow duplicate elements, and others do not.
Some are ordered and others are unordered. The Java
platform doesn't provide any direct implementations of
this interface but provides implementations of more
specific subinterfaces, such as Set and List.
A note on iterators
• An Iterator is an object that enables you to traverse
through a collection and to remove elements from the
collection selectively, if desired. You get an Iterator
for a collection by calling its iterator() method. The
following is the Iterator interface.
public interface Iterator<E> {
boolean hasNext();
E next();
void remove(); //optional
}
public interface Set<E>
• Set — a Collection<E>
extendscollection that cannot contain duplicate
elements. This interface models the
mathematical set abstraction and is used to
represent sets, such as the cards comprising a
poker hand, the courses making up a student's
schedule, or the processes running on a
machine.
public interface List<E>
extends Collection<E> (sometimes called a
• List — an ordered collection
sequence). Lists can contain duplicate elements.
The user of a List generally has precise control
over where in the list each element is inserted
and can access elements by their integer index
(position). If you've used Vector, you're familiar
with the general flavor of List.
public interface Queue<E>
extends Collection<E>

• Queue — a collection used to hold multiple
elements prior to processing. Besides basic
Collection operations, a Queue provides
additional insertion, extraction, and inspection
operations.
Note on Comparator interface
• Comparator is another interface (in addition to
Comparable) provided by the Java API which
can be used to order objects.
• You can use this interface to define an order that
is different from the Comparable (natural)
order.
public interface SortedMap<K,V>
extends Map<K,V>

• SortedMap — a Map that maintains its
mappings in ascending key order. This is the
Map analog of SortedSet. Sorted maps are used
for naturally ordered collections of key/value
pairs, such as dictionaries and telephone
directories.
General-purpose Implementations
Interfaces

Implementations
Hash table

Set

Resizable array

Linked list

TreeSet
(sorted)

HashSet

List

Tree
(sorted)

Hash table + Linked list

LinkedHashSet

LinkedList

ArrayList

Queue
Map

HashMap

TreeMap
(sorted)

LinkedHashMap

Note the naming convention
LinkedList also implements queue and there is a PriorityQueue implementation (implemented with heap)
Other implementations in the API
• Wrapper implementations delegate all their real
work to a specified collection but add (or
remove) extra functionality on top of what the
collection offers.
▫ Synchronization Wrappers
▫ Unmodifiable Wrappers

• Convenience implementations are miniimplementations that can be more convenient
and more efficient than general-purpose
implementations when you don't need their full
power
▫
▫
▫
▫

List View of an Array
Immutable Multiple-Copy List
Immutable Singleton Set
Empty Set, List, and Map Constants
public interface Map<K,V>
• Map — an object that maps keys to values. A
Map cannot contain duplicate keys; each key can
map to at most one value. If you've used
Hashtable, you're already familiar with the
basics of Map.
public interface SortedSet<E>
extends Set<E> that maintains its elements in
• SortedSet — a Set
ascending order. Several additional operations
are provided to take advantage of the ordering.
Sorted sets are used for naturally ordered sets,
such as word lists and membership rolls.
Making your own implementations
• Most of the time you can use the
implementations provided for you in the Java
API.
• In case the existing implementations do not
satisfy your needs, you can write your own by
extending the abstract classes provided in the
collections framework.

More Related Content

What's hot

Java Collections Framework Inroduction with Video Tutorial
Java Collections Framework Inroduction with Video TutorialJava Collections Framework Inroduction with Video Tutorial
Java Collections Framework Inroduction with Video Tutorial
Marcus Biel
 
Java collection
Java collectionJava collection
Java collection
Deepak Kumar
 
Collections Java e Google Collections
Collections Java e Google CollectionsCollections Java e Google Collections
Collections Java e Google Collections
André Faria Gomes
 
Collections in Java
Collections in JavaCollections in Java
Collections in Java
Khasim Cise
 
Java Collections
Java CollectionsJava Collections
Java Collectionsparag
 
Java Collections Tutorials
Java Collections TutorialsJava Collections Tutorials
Java Collections Tutorials
Prof. Erwin Globio
 
Java.util
Java.utilJava.util
Java.util
Ramakrishna kapa
 
Java collections
Java collectionsJava collections
Java collectionspadmad2291
 
JAVA Collections frame work ppt
 JAVA Collections frame work ppt JAVA Collections frame work ppt
JAVA Collections frame work ppt
Ranjith Alappadan
 
Quick Intro to Java Collections
Quick Intro to Java CollectionsQuick Intro to Java Collections
Quick Intro to Java CollectionsJussi Pohjolainen
 
Collection Framework in java
Collection Framework in javaCollection Framework in java
Collection Framework in java
CPD INDIA
 
Java Collection framework
Java Collection frameworkJava Collection framework
Java Collection framework
ankitgarg_er
 
Java collections concept
Java collections conceptJava collections concept
Java collections concept
kumar gaurav
 
Java Collections Framework
Java Collections FrameworkJava Collections Framework
Java Collections Framework
Sony India Software Center
 
Collections In Java
Collections In JavaCollections In Java
Collections In JavaBinoj T E
 
Collections - Lists, Sets
Collections - Lists, Sets Collections - Lists, Sets
Collections - Lists, Sets
Hitesh-Java
 
07 java collection
07 java collection07 java collection
07 java collection
Abhishek Khune
 
Java - Collections framework
Java - Collections frameworkJava - Collections framework
Java - Collections framework
Riccardo Cardin
 
Collection framework
Collection frameworkCollection framework
Collection framework
BindhuBhargaviTalasi
 
Java Collections | Collections Framework in Java | Java Tutorial For Beginner...
Java Collections | Collections Framework in Java | Java Tutorial For Beginner...Java Collections | Collections Framework in Java | Java Tutorial For Beginner...
Java Collections | Collections Framework in Java | Java Tutorial For Beginner...
Edureka!
 

What's hot (20)

Java Collections Framework Inroduction with Video Tutorial
Java Collections Framework Inroduction with Video TutorialJava Collections Framework Inroduction with Video Tutorial
Java Collections Framework Inroduction with Video Tutorial
 
Java collection
Java collectionJava collection
Java collection
 
Collections Java e Google Collections
Collections Java e Google CollectionsCollections Java e Google Collections
Collections Java e Google Collections
 
Collections in Java
Collections in JavaCollections in Java
Collections in Java
 
Java Collections
Java CollectionsJava Collections
Java Collections
 
Java Collections Tutorials
Java Collections TutorialsJava Collections Tutorials
Java Collections Tutorials
 
Java.util
Java.utilJava.util
Java.util
 
Java collections
Java collectionsJava collections
Java collections
 
JAVA Collections frame work ppt
 JAVA Collections frame work ppt JAVA Collections frame work ppt
JAVA Collections frame work ppt
 
Quick Intro to Java Collections
Quick Intro to Java CollectionsQuick Intro to Java Collections
Quick Intro to Java Collections
 
Collection Framework in java
Collection Framework in javaCollection Framework in java
Collection Framework in java
 
Java Collection framework
Java Collection frameworkJava Collection framework
Java Collection framework
 
Java collections concept
Java collections conceptJava collections concept
Java collections concept
 
Java Collections Framework
Java Collections FrameworkJava Collections Framework
Java Collections Framework
 
Collections In Java
Collections In JavaCollections In Java
Collections In Java
 
Collections - Lists, Sets
Collections - Lists, Sets Collections - Lists, Sets
Collections - Lists, Sets
 
07 java collection
07 java collection07 java collection
07 java collection
 
Java - Collections framework
Java - Collections frameworkJava - Collections framework
Java - Collections framework
 
Collection framework
Collection frameworkCollection framework
Collection framework
 
Java Collections | Collections Framework in Java | Java Tutorial For Beginner...
Java Collections | Collections Framework in Java | Java Tutorial For Beginner...Java Collections | Collections Framework in Java | Java Tutorial For Beginner...
Java Collections | Collections Framework in Java | Java Tutorial For Beginner...
 

Similar to Java collections

Learn advanced java programming
Learn advanced java programmingLearn advanced java programming
Learn advanced java programming
TOPS Technologies
 
JavaCollections.ppt
JavaCollections.pptJavaCollections.ppt
JavaCollections.ppt
boopathirajaraja1
 
JavaCollections.ppt
JavaCollections.pptJavaCollections.ppt
JavaCollections.ppt
Irfanhabeeb18
 
Collection framework
Collection frameworkCollection framework
Collection framework
Jainul Musani
 
Collectn framework copy
Collectn framework   copyCollectn framework   copy
Collectn framework copy
charan kumar
 
Collectn framework
Collectn frameworkCollectn framework
Collectn framework
charan kumar
 
Advanced Java - UNIT 3.pptx
Advanced Java - UNIT 3.pptxAdvanced Java - UNIT 3.pptx
Advanced Java - UNIT 3.pptx
eyemitra1
 
Topic-G-JavaCollections Framework.ppt
Topic-G-JavaCollections    Framework.pptTopic-G-JavaCollections    Framework.ppt
Topic-G-JavaCollections Framework.ppt
swapnilslide2019
 
Collections Training
Collections TrainingCollections Training
Collections Training
Ramindu Deshapriya
 
Week_2_Lec_6-10_with_watermarking_(1).pdf
Week_2_Lec_6-10_with_watermarking_(1).pdfWeek_2_Lec_6-10_with_watermarking_(1).pdf
Week_2_Lec_6-10_with_watermarking_(1).pdf
PrabhaK22
 
Collections
CollectionsCollections
Collections
Rajkattamuri
 
Java Unit 2 (Part 2)
Java Unit 2 (Part 2)Java Unit 2 (Part 2)
Java Unit 2 (Part 2)
SURBHI SAROHA
 
Java 8
Java 8Java 8
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
 
Collections
CollectionsCollections
Collections
Manav Prasad
 
Generic types and collections GUIs.pptx
Generic types and collections GUIs.pptxGeneric types and collections GUIs.pptx
Generic types and collections GUIs.pptx
Avirup Pal
 
Java Collections
Java  Collections Java  Collections
JAVA(UNIT 4)
JAVA(UNIT 4)JAVA(UNIT 4)
JAVA(UNIT 4)
SURBHI SAROHA
 
1. Java Collections Framework Introduction
1. Java Collections Framework Introduction1. Java Collections Framework Introduction
1. Java Collections Framework Introduction
Bharat Savani
 
Java Collection Interview Questions [Updated]
Java Collection Interview Questions [Updated]Java Collection Interview Questions [Updated]
Java Collection Interview Questions [Updated]
SoniaMathias2
 

Similar to Java collections (20)

Learn advanced java programming
Learn advanced java programmingLearn advanced java programming
Learn advanced java programming
 
JavaCollections.ppt
JavaCollections.pptJavaCollections.ppt
JavaCollections.ppt
 
JavaCollections.ppt
JavaCollections.pptJavaCollections.ppt
JavaCollections.ppt
 
Collection framework
Collection frameworkCollection framework
Collection framework
 
Collectn framework copy
Collectn framework   copyCollectn framework   copy
Collectn framework copy
 
Collectn framework
Collectn frameworkCollectn framework
Collectn framework
 
Advanced Java - UNIT 3.pptx
Advanced Java - UNIT 3.pptxAdvanced Java - UNIT 3.pptx
Advanced Java - UNIT 3.pptx
 
Topic-G-JavaCollections Framework.ppt
Topic-G-JavaCollections    Framework.pptTopic-G-JavaCollections    Framework.ppt
Topic-G-JavaCollections Framework.ppt
 
Collections Training
Collections TrainingCollections Training
Collections Training
 
Week_2_Lec_6-10_with_watermarking_(1).pdf
Week_2_Lec_6-10_with_watermarking_(1).pdfWeek_2_Lec_6-10_with_watermarking_(1).pdf
Week_2_Lec_6-10_with_watermarking_(1).pdf
 
Collections
CollectionsCollections
Collections
 
Java Unit 2 (Part 2)
Java Unit 2 (Part 2)Java Unit 2 (Part 2)
Java Unit 2 (Part 2)
 
Java 8
Java 8Java 8
Java 8
 
Slide Dasar Materi Java Collection Framework
Slide Dasar Materi Java Collection FrameworkSlide Dasar Materi Java Collection Framework
Slide Dasar Materi Java Collection Framework
 
Collections
CollectionsCollections
Collections
 
Generic types and collections GUIs.pptx
Generic types and collections GUIs.pptxGeneric types and collections GUIs.pptx
Generic types and collections GUIs.pptx
 
Java Collections
Java  Collections Java  Collections
Java Collections
 
JAVA(UNIT 4)
JAVA(UNIT 4)JAVA(UNIT 4)
JAVA(UNIT 4)
 
1. Java Collections Framework Introduction
1. Java Collections Framework Introduction1. Java Collections Framework Introduction
1. Java Collections Framework Introduction
 
Java Collection Interview Questions [Updated]
Java Collection Interview Questions [Updated]Java Collection Interview Questions [Updated]
Java Collection Interview Questions [Updated]
 

Recently uploaded

Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Product School
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Product School
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
Elena Simperl
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Tobias Schneck
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
g2nightmarescribd
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
RTTS
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Jeffrey Haguewood
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
Alison B. Lowndes
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Thijs Feryn
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
Elena Simperl
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
DianaGray10
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
Dorra BARTAGUIZ
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
Ana-Maria Mihalceanu
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
Product School
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
KatiaHIMEUR1
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
DianaGray10
 

Recently uploaded (20)

Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
 

Java collections

  • 2. APIs and Versions • Number one hint for programming with Java Collections: use the API ▫ http://java.sun.com/j2se/1.5.0/docs/api/java/util /Collection.html • Be sure to use the 1.5.0 APIs to get the version with generics
  • 3. Java Collections Framework • The Java language API provides many of the data structures from this class for you. • It defines a “collection” as “an object that represents a group of objects”. • It defines a collections framework as “a unified architecture for representing and manipulating collections, allowing them to be manipulated independent of the details of their representation.”
  • 4. Collections Framework (cont) • Collection Interfaces - Represent different types of collections, such as sets, lists and maps. These interfaces form the basis of the framework. • General-purpose Implementations - Primary implementations of the collection interfaces. • Legacy Implementations - The collection classes from earlier releases, Vector and Hashtable, have been retrofitted to implement the collection interfaces. • Wrapper Implementations - Add functionality, such as synchronization, to other implementations. • Convenience Implementations - High-performance "mini-implementations" of the collection interfaces. • Abstract Implementations - Partial implementations of the collection interfaces to facilitate custom implementations. • Algorithms - Static methods that perform useful functions on collections, such as sorting a list. • Infrastructure - Interfaces that provide essential support for the collection interfaces. • Array Utilities - Utility functions for arrays of primitives and reference objects. Not, strictly speaking, a part of the Collections Framework, this functionality is being added to the Java platform at the same time and relies on some of the same infrastructure.
  • 5. Collection interfaces • The core collection interfaces encapsulate different types of collections. They represent the abstract data types that are part of the collections framework. They are interfaces so they do not provide an implementation!
  • 6. public interface Collection<E> extends Iterable<E> • Collection — the root of the collection hierarchy. A collection represents a group of objects known as its elements. The Collection interface is the least common denominator that all collections implement and is used to pass collections around and to manipulate them when maximum generality is desired. Some types of collections allow duplicate elements, and others do not. Some are ordered and others are unordered. The Java platform doesn't provide any direct implementations of this interface but provides implementations of more specific subinterfaces, such as Set and List.
  • 7. A note on iterators • An Iterator is an object that enables you to traverse through a collection and to remove elements from the collection selectively, if desired. You get an Iterator for a collection by calling its iterator() method. The following is the Iterator interface. public interface Iterator<E> { boolean hasNext(); E next(); void remove(); //optional }
  • 8. public interface Set<E> • Set — a Collection<E> extendscollection that cannot contain duplicate elements. This interface models the mathematical set abstraction and is used to represent sets, such as the cards comprising a poker hand, the courses making up a student's schedule, or the processes running on a machine.
  • 9. public interface List<E> extends Collection<E> (sometimes called a • List — an ordered collection sequence). Lists can contain duplicate elements. The user of a List generally has precise control over where in the list each element is inserted and can access elements by their integer index (position). If you've used Vector, you're familiar with the general flavor of List.
  • 10. public interface Queue<E> extends Collection<E> • Queue — a collection used to hold multiple elements prior to processing. Besides basic Collection operations, a Queue provides additional insertion, extraction, and inspection operations.
  • 11. Note on Comparator interface • Comparator is another interface (in addition to Comparable) provided by the Java API which can be used to order objects. • You can use this interface to define an order that is different from the Comparable (natural) order.
  • 12. public interface SortedMap<K,V> extends Map<K,V> • SortedMap — a Map that maintains its mappings in ascending key order. This is the Map analog of SortedSet. Sorted maps are used for naturally ordered collections of key/value pairs, such as dictionaries and telephone directories.
  • 13. General-purpose Implementations Interfaces Implementations Hash table Set Resizable array Linked list TreeSet (sorted) HashSet List Tree (sorted) Hash table + Linked list LinkedHashSet LinkedList ArrayList Queue Map HashMap TreeMap (sorted) LinkedHashMap Note the naming convention LinkedList also implements queue and there is a PriorityQueue implementation (implemented with heap)
  • 14. Other implementations in the API • Wrapper implementations delegate all their real work to a specified collection but add (or remove) extra functionality on top of what the collection offers. ▫ Synchronization Wrappers ▫ Unmodifiable Wrappers • Convenience implementations are miniimplementations that can be more convenient and more efficient than general-purpose implementations when you don't need their full power ▫ ▫ ▫ ▫ List View of an Array Immutable Multiple-Copy List Immutable Singleton Set Empty Set, List, and Map Constants
  • 15. public interface Map<K,V> • Map — an object that maps keys to values. A Map cannot contain duplicate keys; each key can map to at most one value. If you've used Hashtable, you're already familiar with the basics of Map.
  • 16. public interface SortedSet<E> extends Set<E> that maintains its elements in • SortedSet — a Set ascending order. Several additional operations are provided to take advantage of the ordering. Sorted sets are used for naturally ordered sets, such as word lists and membership rolls.
  • 17. Making your own implementations • Most of the time you can use the implementations provided for you in the Java API. • In case the existing implementations do not satisfy your needs, you can write your own by extending the abstract classes provided in the collections framework.