SlideShare a Scribd company logo
Collections and Generics
Collection framework Overview
• The collection is an object that represents a group of objects.
• A collections framework is a unified architecture for representing and
manipulating collections, allowing them to be manipulated
independently.
Cont’d
The collection framework consists
of
• Collection interfaces
• General purpose implementations
• Legacy implementations
• Special purpose implementations
• Concurrent implementations
• Wrapper implementations
• Convenience implementations
• Abstract implementations
• Algorithms
• Infrastructure
• Array utilities
Advantages of collection framework
• Reduces programming effort
• Increases performance
• Provides interoperability between unrelated APIs
• Reduces the effort required to learn APIs
• Reduces the effort required to design and implement APIs
• Fosters software reuse
Collection interfaces
• The collection interface is the primary which is implemented by all
the collection classes.
• There are totally 9 collection interfaces.
• Some of them are
• List interface
• Set interface
• Sortedset interface
Collection classes
Class Description
AbstractCollection Implements most of the collections interface
AbstractList Extends AbstractCollection and implements most of
the List interface
AbstractSequentialList Extends AbstractList for use by a collection that uses
sequential rather than random access of its elements
LinkedList Implements a linked list by extending
AbstractSequentialList
ArrayList Implements a dynamic array by extending AbstractList
AbstractSet Extends AnstractCollection and Implements most of
the Set interfaces
HashSet Extends AbstractSet for use with a hash table
TreeSet Implements a set stored in a tree. Extends AbstractSet
ArrayList
• The ArrayList class extends AbstractList and implements the List
interface.
• The constructors provided by the ArrayList class is
• ArrayList() – empty constructor
• ArrayList(Collection c) - initialized with the elements of the collection c
• ArrayList(int capacity) – specifying the initial capacity
• ArrayList<String> stringList = new ArrayList<String>();
ArrayList example
LinkedList
• LinkedList class implements the List interface.
• The LinkedList class provides uniformly named methods to get,
remove and insert an element at the beginning and end of the list.
• LinkedList also can be used as stack, queue, double ended queue
using these operations.
• The LinkedList() and LinkedList(collection c) are the constructors used
in this class.
• LinkedList<type> obj = new LinkedList<type>();
Methods used in LinkedList
Method Description
Void addFirst(Object ob) To add elements to the start of the list
Void addLast(Object ob) To add elements to the end of the list
Object getFirst() To obtain the first element
Object getLast() To obtain the last element
Object removeFirst() To remove the first element in the list
Object removeLast() To remove the last element in the list
LinkedList Example
HashSet Class
• This class implements the set interface, backed by a hash table.
• It provides constant time performance for operations
like add, remove, contains and size
HashSet example
TreeSet Class
• The TreeSet class implements the set interface, backed by a TreeMap
instance.
• TreeSet is an excellent choice when storing large amounts of sorted
information that must be found quickly.
TreeSet example
Iterator and ListIterator interface
• The iterator enables you to cycle through a collection obtaining or
removing elements.
• The ListIterator extends Iterator to allow multidirectional traversal of
a list and the modification of the element.
Methods of iterator interface
Method Description
Boolean hasNext() Returns true if the iteration has more elements
Object next() Returns the next element in the iteration
Void remove() Removes the underlying collection the last element
returned by the iterator
Methods of ListIterator interface
Method Description
Void add(Object e) Inserts the specified element into the list
Boolean hasNext() Returns true if this list iterator has more elements
when traversing the list in the forward direction
Boolean hasPrevious() Returns true if this list iterator has more elements
when traversing the list in the reverse direction
Object next() Returns the next element in the list
Int nextIndex() Returns the index of the element that would be
returned by a subsequent call to next
Object previous() Returns the previous element in the list
Int previousIndex() Returns the index of the element that would e
returned by a subsequent call to previous
Void remove() Removes from the list the last element that was
returned by next or previous
Void set(Object o) Replaces the last element returned by next or
previous with the specified element
Iterator and ListIterator example
User Defined Classes in Collection
Random Access Interface
• The RandomAccess interface identifies List implementations which
are faster to iterate using the List.get() method rather than using the
Iterator.next() method.
Working with maps
• Map interface:
• Map interface is used to map keys to value.
• A map cannot contain duplicate keys, each key can map to at most one value.
• A key is an object that is used to retrieve a value later.
• Given a key and a value, one can store the value in a map object.
• The value can be retrieved using the key.
• The get() and put() are two basic operations in map interface
• The put() is used to put a value into the map
• The get() is used to obtain a value passing the key as an argument
Methods provided by map interface
Method Description
Void clear() Removes all mappings from this map
Boolean containsKey(Object key) Returns true if the map contains a mapping for the
specified key
Boolean containsValue(Object key) Returns true if this map maps one or more keys to the
specified value.
Set entrySet() Returns a set view of the mappings contained in this
map
Boolean equals(Object o) Compares the specified object with this map for
equality
Object get(Object key) Returns the value to which this map mpas the
specified key
Cont’d
Method Description
Int hashCode() Returns the hash code value for this map
Boolean isEmpty() Returns true if this map contains no key-value
mappings
Set keyset() Returns a set view of the keys contained in this map
Object put(Object key, Object value) Associates the specified value with the specified key in
this map
Void putAll(Map t) Copies all of the mappings from the specified map to
this map
Object remove(Object key) Removes the mappings for this key from this map if it
is present
Int size() Returns number of key-value mappings in this map
Collection values() Returns a collection view of the values contained in
the map
HashMap class
• HashMap class is used for hash table based implementation of the
map interface.
• This implementation provides all the optional map operations and
permits null values and the null key.
• The HashMap class has two parameters
• Capacity – the capacity is the number of buckets in the hash table
• Load factor – is the measure of how full the hash table is allowed to get
before its capacity is automatically increased
Example for HashMap class
Any queries?
Thank you

More Related Content

PPTX
java interface and packages
PPTX
JAVA AWT
PDF
Strings in python
PPTX
Basics of Object Oriented Programming in Python
PPT
Java interfaces & abstract classes
PPTX
Java swing
PDF
Collections In Java
java interface and packages
JAVA AWT
Strings in python
Basics of Object Oriented Programming in Python
Java interfaces & abstract classes
Java swing
Collections In Java

What's hot (20)

PPTX
Java abstract class & abstract methods
PPTX
constructors in java ppt
PPTX
Linked list
PPTX
Basic Concepts of OOPs (Object Oriented Programming in Java)
PDF
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
PDF
Arrays in Java
PPTX
Chapter 05 classes and objects
PPTX
Class, object and inheritance in python
PPTX
Oop c++class(final).ppt
PPT
Inheritance in java
PPTX
Java – lexical issues
PPTX
Data structure and algorithm using java
PPT
Object-oriented concepts
PPT
Bubble sort
PPTX
Super keyword in java
PDF
Class and Objects in Java
PPT
Operator Overloading
PPTX
Member Function in C++
PPS
Introduction to class in java
Java abstract class & abstract methods
constructors in java ppt
Linked list
Basic Concepts of OOPs (Object Oriented Programming in Java)
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Arrays in Java
Chapter 05 classes and objects
Class, object and inheritance in python
Oop c++class(final).ppt
Inheritance in java
Java – lexical issues
Data structure and algorithm using java
Object-oriented concepts
Bubble sort
Super keyword in java
Class and Objects in Java
Operator Overloading
Member Function in C++
Introduction to class in java
Ad

Viewers also liked (7)

PPT
Generics collections
PPT
Collection Framework in java
PDF
07 java collection
PDF
Java collections the force awakens
PDF
Java Collections API
PPTX
Java - Collections framework
PDF
Java Collection framework
Generics collections
Collection Framework in java
07 java collection
Java collections the force awakens
Java Collections API
Java - Collections framework
Java Collection framework
Ad

Similar to Collections and generics (20)

PPT
Collection Framework.power point presentation.......
PPTX
oop lecture framework,list,maps,collection
PDF
javacollections.pdf
PPT
Collections
PPT
collections
PPT
Collections
PPT
Collections in Java
PPT
description of Collections, seaching & Sorting
PDF
java unit 4 pdf - about java collections
PPTX
Collections in object oriented programming
PPTX
Collections lecture 35 40
PPT
Set_TreeSet_etc comparison in java collection.ppt
PPT
PDF
Collections Api - Java
PPTX
Nature Activities Binder _ by Slidesgo.pptx
PPT
Java collection
PPTX
Collection Framework-1.pptx
PPTX
Array list(1)
PPT
1 list datastructures
PPT
Best core & advanced java classes in mumbai
Collection Framework.power point presentation.......
oop lecture framework,list,maps,collection
javacollections.pdf
Collections
collections
Collections
Collections in Java
description of Collections, seaching & Sorting
java unit 4 pdf - about java collections
Collections in object oriented programming
Collections lecture 35 40
Set_TreeSet_etc comparison in java collection.ppt
Collections Api - Java
Nature Activities Binder _ by Slidesgo.pptx
Java collection
Collection Framework-1.pptx
Array list(1)
1 list datastructures
Best core & advanced java classes in mumbai

More from Muthukumaran Subramanian (20)

PPTX
Struts introduction
PDF
Operators in java
PDF
Threads concept in java
PDF
StringTokenizer in java
PDF
Overriding methods
PDF
Oops (inheritance&interface)
PDF
Non access modifiers
PDF
Jdbc connectivity in java
PDF
Garbage collection in java
PDF
PDF
Exception handling
PDF
Currency class
PDF
Timer class in java
PDF
Calendar class in java
PDF
Classes and objects in java
PDF
intorduction to Arrays in java
PDF
Access modifiers in java
PDF
Abstract classes
PDF
Java introduction
Struts introduction
Operators in java
Threads concept in java
StringTokenizer in java
Overriding methods
Oops (inheritance&interface)
Non access modifiers
Jdbc connectivity in java
Garbage collection in java
Exception handling
Currency class
Timer class in java
Calendar class in java
Classes and objects in java
intorduction to Arrays in java
Access modifiers in java
Abstract classes
Java introduction

Recently uploaded (20)

PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPT
Mechanical Engineering MATERIALS Selection
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
Welding lecture in detail for understanding
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PPTX
OOP with Java - Java Introduction (Basics)
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PDF
Digital Logic Computer Design lecture notes
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
bas. eng. economics group 4 presentation 1.pptx
UNIT-1 - COAL BASED THERMAL POWER PLANTS
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
R24 SURVEYING LAB MANUAL for civil enggi
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Mechanical Engineering MATERIALS Selection
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
CH1 Production IntroductoryConcepts.pptx
Welding lecture in detail for understanding
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
OOP with Java - Java Introduction (Basics)
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
Digital Logic Computer Design lecture notes
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
CYBER-CRIMES AND SECURITY A guide to understanding
Model Code of Practice - Construction Work - 21102022 .pdf

Collections and generics

  • 2. Collection framework Overview • The collection is an object that represents a group of objects. • A collections framework is a unified architecture for representing and manipulating collections, allowing them to be manipulated independently.
  • 3. Cont’d The collection framework consists of • Collection interfaces • General purpose implementations • Legacy implementations • Special purpose implementations • Concurrent implementations • Wrapper implementations • Convenience implementations • Abstract implementations • Algorithms • Infrastructure • Array utilities
  • 4. Advantages of collection framework • Reduces programming effort • Increases performance • Provides interoperability between unrelated APIs • Reduces the effort required to learn APIs • Reduces the effort required to design and implement APIs • Fosters software reuse
  • 5. Collection interfaces • The collection interface is the primary which is implemented by all the collection classes. • There are totally 9 collection interfaces. • Some of them are • List interface • Set interface • Sortedset interface
  • 6. Collection classes Class Description AbstractCollection Implements most of the collections interface AbstractList Extends AbstractCollection and implements most of the List interface AbstractSequentialList Extends AbstractList for use by a collection that uses sequential rather than random access of its elements LinkedList Implements a linked list by extending AbstractSequentialList ArrayList Implements a dynamic array by extending AbstractList AbstractSet Extends AnstractCollection and Implements most of the Set interfaces HashSet Extends AbstractSet for use with a hash table TreeSet Implements a set stored in a tree. Extends AbstractSet
  • 7. ArrayList • The ArrayList class extends AbstractList and implements the List interface. • The constructors provided by the ArrayList class is • ArrayList() – empty constructor • ArrayList(Collection c) - initialized with the elements of the collection c • ArrayList(int capacity) – specifying the initial capacity • ArrayList<String> stringList = new ArrayList<String>();
  • 9. LinkedList • LinkedList class implements the List interface. • The LinkedList class provides uniformly named methods to get, remove and insert an element at the beginning and end of the list. • LinkedList also can be used as stack, queue, double ended queue using these operations. • The LinkedList() and LinkedList(collection c) are the constructors used in this class. • LinkedList<type> obj = new LinkedList<type>();
  • 10. Methods used in LinkedList Method Description Void addFirst(Object ob) To add elements to the start of the list Void addLast(Object ob) To add elements to the end of the list Object getFirst() To obtain the first element Object getLast() To obtain the last element Object removeFirst() To remove the first element in the list Object removeLast() To remove the last element in the list
  • 12. HashSet Class • This class implements the set interface, backed by a hash table. • It provides constant time performance for operations like add, remove, contains and size
  • 14. TreeSet Class • The TreeSet class implements the set interface, backed by a TreeMap instance. • TreeSet is an excellent choice when storing large amounts of sorted information that must be found quickly.
  • 16. Iterator and ListIterator interface • The iterator enables you to cycle through a collection obtaining or removing elements. • The ListIterator extends Iterator to allow multidirectional traversal of a list and the modification of the element.
  • 17. Methods of iterator interface Method Description Boolean hasNext() Returns true if the iteration has more elements Object next() Returns the next element in the iteration Void remove() Removes the underlying collection the last element returned by the iterator
  • 18. Methods of ListIterator interface Method Description Void add(Object e) Inserts the specified element into the list Boolean hasNext() Returns true if this list iterator has more elements when traversing the list in the forward direction Boolean hasPrevious() Returns true if this list iterator has more elements when traversing the list in the reverse direction Object next() Returns the next element in the list Int nextIndex() Returns the index of the element that would be returned by a subsequent call to next Object previous() Returns the previous element in the list Int previousIndex() Returns the index of the element that would e returned by a subsequent call to previous Void remove() Removes from the list the last element that was returned by next or previous Void set(Object o) Replaces the last element returned by next or previous with the specified element
  • 20. User Defined Classes in Collection
  • 21. Random Access Interface • The RandomAccess interface identifies List implementations which are faster to iterate using the List.get() method rather than using the Iterator.next() method.
  • 22. Working with maps • Map interface: • Map interface is used to map keys to value. • A map cannot contain duplicate keys, each key can map to at most one value. • A key is an object that is used to retrieve a value later. • Given a key and a value, one can store the value in a map object. • The value can be retrieved using the key. • The get() and put() are two basic operations in map interface • The put() is used to put a value into the map • The get() is used to obtain a value passing the key as an argument
  • 23. Methods provided by map interface Method Description Void clear() Removes all mappings from this map Boolean containsKey(Object key) Returns true if the map contains a mapping for the specified key Boolean containsValue(Object key) Returns true if this map maps one or more keys to the specified value. Set entrySet() Returns a set view of the mappings contained in this map Boolean equals(Object o) Compares the specified object with this map for equality Object get(Object key) Returns the value to which this map mpas the specified key
  • 24. Cont’d Method Description Int hashCode() Returns the hash code value for this map Boolean isEmpty() Returns true if this map contains no key-value mappings Set keyset() Returns a set view of the keys contained in this map Object put(Object key, Object value) Associates the specified value with the specified key in this map Void putAll(Map t) Copies all of the mappings from the specified map to this map Object remove(Object key) Removes the mappings for this key from this map if it is present Int size() Returns number of key-value mappings in this map Collection values() Returns a collection view of the values contained in the map
  • 25. HashMap class • HashMap class is used for hash table based implementation of the map interface. • This implementation provides all the optional map operations and permits null values and the null key. • The HashMap class has two parameters • Capacity – the capacity is the number of buckets in the hash table • Load factor – is the measure of how full the hash table is allowed to get before its capacity is automatically increased