SlideShare a Scribd company logo
Java Collections
Sujit Kumar
Zenolocity LLC
Main Interfaces in Java Collection
Framework
• List
• Set
• Map
• Collection
• Iterator
• Queue
List Interface
• Ordered or sequenced collection of objects.
• You can access each element in the list via an
index.
• Examples of classes that implement the List
interface – ArrayList, LinkedList.
• You can have duplicate entries or null values.
Set Interface
• Cannot have duplicate elements.
• No guarantee on the order of elements.
• Can have atmost 1 null value.
• Classes that implement this interface are
HashSet, TreeSet and LinkedHashSet.
Map Interface
• Map has a collection of keys and values and
each key maps to a specific value.
• No duplicate keys.
• Each key can map to at most 1 value.
• Examples of classes that implement the Map
interface are HashMap, TreeMap and
LinkedHashMap.
Choosing an appropriate
Implementation
Class: java.util.Collections
• Contains only static methods that operate on
or return collections.
• Polymorphic algorithms that operate on
collections.
• Wrapper methods, which return a new
collection backed by a specified collection.
• Throw a NullPointerException if a null object is
passed as a parameter.
Important Methods of Collections
Class
• Wrapper Classes for Collections
• Thread Safe Collection:
Collections.synchronizedList(someListObject);
• Immutable Collection:
Collections.unmodifiableList(someListObject);
• Type Safe Collection:
Collections.checkedList(List<E> list, Class<E> type)
Other important methods of
Collections class
• addAll
• binarySearch
• min, max
• Sort
• reverseOrder
• Empty{List,Set,Map} methods.
• replaceAll
Difference between
ConcurrentHashMap and
Collections.synchronizedMap
• ConcurrentHashMap: allows concurrent modification of
the Map from several threads without the need to block
them. Use this option if performance is critical, and each
thread only inserts data to the map, with reads happening
less frequently.
• Collections.synchronizedMap(map) creates a blocking Map
which will degrade performance, albeit ensure consistency
(if used properly). Use this option if you need to ensure
data consistency, and each thread needs to have an up-to-
date view of the map. Use this when lot of reads and very
few writes.
Iterator interface
• Iterate over any of the collection classes and
access the elements in an iterative order.
• List.iterator() will provide an iterator.
• iterator.hasNext() – check if another element is
available.
• iterator.next() – retrieves the next element.
• Java Collection classes are fail-fast which means
that if the Collection is changed while some
thread is traversing over it using an iterator, the
iterator.next() will throw a
ConcurrentModificationException.

More Related Content

PPTX
C# Non generics collection
PPTX
Understanding the components of standard template library
PPTX
R DATA STRUCTURES 2
PDF
Collections and generics
PPT
L11 array list
PPTX
R DATA STRUCTURES 1
PPT
2CPP16 - STL
C# Non generics collection
Understanding the components of standard template library
R DATA STRUCTURES 2
Collections and generics
L11 array list
R DATA STRUCTURES 1
2CPP16 - STL

What's hot (20)

PPTX
ArrayList in JAVA
PPTX
Array vs array list
PPTX
Collections Array list
PPTX
Sorting
PPT
sets and maps
PPTX
Standard template library
PPTX
How to choose best containers in STL (C++)
PDF
The map interface (the java™ tutorials collections interfaces)
PDF
4 chapter3 list_stackqueuepart1
PPTX
Slide 1.-datastructure
PDF
STL in C++
DOCX
Collection in Java
PDF
Standard template library
PPTX
Matlab Organizing Data
PDF
Java ArrayList Tutorial | Edureka
PPTX
Dynamic memory allocation and linked lists
PPT
Intro to Lists
PPTX
Java Tutorial Lab 7
PDF
Lecture notesmap
ArrayList in JAVA
Array vs array list
Collections Array list
Sorting
sets and maps
Standard template library
How to choose best containers in STL (C++)
The map interface (the java™ tutorials collections interfaces)
4 chapter3 list_stackqueuepart1
Slide 1.-datastructure
STL in C++
Collection in Java
Standard template library
Matlab Organizing Data
Java ArrayList Tutorial | Edureka
Dynamic memory allocation and linked lists
Intro to Lists
Java Tutorial Lab 7
Lecture notesmap
Ad

Similar to Java collections (20)

PPTX
Collections Training
PPTX
Java util
PPTX
Collections framework in java
PPTX
Javasession7
PPTX
Java Hands-On Workshop
PPTX
Java Collections Framework - Interfaces, Classes and Algorithms
PDF
Java collections
PDF
Java collections-interview-questions
PPTX
collection framework.pptx
PPT
Java Collection slide ppt presentation..
PPTX
collectionsframework210616084411 (1).pptx
PPTX
U-III-part-1.pptxpart 1 of Java and hardware coding questions are answered
PPT
Md08 collection api
PPTX
Java Collections.pptx
PPT
PPT
description of Collections, seaching & Sorting
ODP
Java Collections
PPTX
Java Collection
PPTX
22CS305-UNIT-1.pptx ADVANCE JAVA PROGRAMMING
Collections Training
Java util
Collections framework in java
Javasession7
Java Hands-On Workshop
Java Collections Framework - Interfaces, Classes and Algorithms
Java collections
Java collections-interview-questions
collection framework.pptx
Java Collection slide ppt presentation..
collectionsframework210616084411 (1).pptx
U-III-part-1.pptxpart 1 of Java and hardware coding questions are answered
Md08 collection api
Java Collections.pptx
description of Collections, seaching & Sorting
Java Collections
Java Collection
22CS305-UNIT-1.pptx ADVANCE JAVA PROGRAMMING
Ad

More from Sujit Kumar (20)

PPTX
Introduction to OOP with java
PPTX
SFDC Database Basics
PPTX
SFDC Database Security
PPTX
SFDC Social Applications
PPTX
SFDC Other Platform Features
PPTX
SFDC Outbound Integrations
PPTX
SFDC Inbound Integrations
PPTX
SFDC UI - Advanced Visualforce
PPTX
SFDC UI - Introduction to Visualforce
PPTX
SFDC Deployments
PPTX
SFDC Batch Apex
PPTX
SFDC Data Loader
PPTX
SFDC Advanced Apex
PPTX
SFDC Introduction to Apex
PPTX
SFDC Database Additional Features
PPTX
Introduction to SalesForce
PPTX
More about java strings - Immutability and String Pool
PPTX
Hibernate First and Second level caches
PPTX
Java equals hashCode Contract
PPTX
Java Comparable and Comparator
Introduction to OOP with java
SFDC Database Basics
SFDC Database Security
SFDC Social Applications
SFDC Other Platform Features
SFDC Outbound Integrations
SFDC Inbound Integrations
SFDC UI - Advanced Visualforce
SFDC UI - Introduction to Visualforce
SFDC Deployments
SFDC Batch Apex
SFDC Data Loader
SFDC Advanced Apex
SFDC Introduction to Apex
SFDC Database Additional Features
Introduction to SalesForce
More about java strings - Immutability and String Pool
Hibernate First and Second level caches
Java equals hashCode Contract
Java Comparable and Comparator

Recently uploaded (20)

PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Electronic commerce courselecture one. Pdf
PPTX
Spectroscopy.pptx food analysis technology
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
KodekX | Application Modernization Development
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
Big Data Technologies - Introduction.pptx
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Unlocking AI with Model Context Protocol (MCP)
MIND Revenue Release Quarter 2 2025 Press Release
“AI and Expert System Decision Support & Business Intelligence Systems”
Advanced methodologies resolving dimensionality complications for autism neur...
Chapter 3 Spatial Domain Image Processing.pdf
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Network Security Unit 5.pdf for BCA BBA.
Electronic commerce courselecture one. Pdf
Spectroscopy.pptx food analysis technology
Programs and apps: productivity, graphics, security and other tools
Reach Out and Touch Someone: Haptics and Empathic Computing
Dropbox Q2 2025 Financial Results & Investor Presentation
Agricultural_Statistics_at_a_Glance_2022_0.pdf
KodekX | Application Modernization Development
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Big Data Technologies - Introduction.pptx
The AUB Centre for AI in Media Proposal.docx
Mobile App Security Testing_ A Comprehensive Guide.pdf
Unlocking AI with Model Context Protocol (MCP)

Java collections

  • 2. Main Interfaces in Java Collection Framework • List • Set • Map • Collection • Iterator • Queue
  • 3. List Interface • Ordered or sequenced collection of objects. • You can access each element in the list via an index. • Examples of classes that implement the List interface – ArrayList, LinkedList. • You can have duplicate entries or null values.
  • 4. Set Interface • Cannot have duplicate elements. • No guarantee on the order of elements. • Can have atmost 1 null value. • Classes that implement this interface are HashSet, TreeSet and LinkedHashSet.
  • 5. Map Interface • Map has a collection of keys and values and each key maps to a specific value. • No duplicate keys. • Each key can map to at most 1 value. • Examples of classes that implement the Map interface are HashMap, TreeMap and LinkedHashMap.
  • 7. Class: java.util.Collections • Contains only static methods that operate on or return collections. • Polymorphic algorithms that operate on collections. • Wrapper methods, which return a new collection backed by a specified collection. • Throw a NullPointerException if a null object is passed as a parameter.
  • 8. Important Methods of Collections Class • Wrapper Classes for Collections • Thread Safe Collection: Collections.synchronizedList(someListObject); • Immutable Collection: Collections.unmodifiableList(someListObject); • Type Safe Collection: Collections.checkedList(List<E> list, Class<E> type)
  • 9. Other important methods of Collections class • addAll • binarySearch • min, max • Sort • reverseOrder • Empty{List,Set,Map} methods. • replaceAll
  • 10. Difference between ConcurrentHashMap and Collections.synchronizedMap • ConcurrentHashMap: allows concurrent modification of the Map from several threads without the need to block them. Use this option if performance is critical, and each thread only inserts data to the map, with reads happening less frequently. • Collections.synchronizedMap(map) creates a blocking Map which will degrade performance, albeit ensure consistency (if used properly). Use this option if you need to ensure data consistency, and each thread needs to have an up-to- date view of the map. Use this when lot of reads and very few writes.
  • 11. Iterator interface • Iterate over any of the collection classes and access the elements in an iterative order. • List.iterator() will provide an iterator. • iterator.hasNext() – check if another element is available. • iterator.next() – retrieves the next element. • Java Collection classes are fail-fast which means that if the Collection is changed while some thread is traversing over it using an iterator, the iterator.next() will throw a ConcurrentModificationException.

Editor's Notes

  • #5: No nulls are allowed in TreeSet.