SlideShare a Scribd company logo
1 of 6
Download to read offline
Core Java

Debasish Pratihari

Collection:


A collection is a generic term for any object that
represents a set of objects grouped together by
some means or mechanism inside the memory.



Collection Framework is a specification by which
we can store and manage objects of
heterogeneous kinds in memory.



 Collection Classes
are also called
Container classes

We can define collection as a java data structure
capable of storing & managing different kinds of
objects.



Note :

A collection has a capacity to grow dynamically.

Collection Framework Provides:


Interface



Implementation



Algorithms

Core Interfaces :

Collection

Set

List

Map

SortedMap

Queue

SortedSet

Lecture/core/collection/24

Page #1

feel the Technology…
Core Java

Debasish Pratihari

Types of Collection:


The way a collection (group of objects) is
implemented in memory, accordingly it is
categorized into following categories.

 Set
 List
 Map

Traversing Collections:


The Followings are the ways to traverse a
collection:





For-each loop
Iterator
ListItetor
Using Methods taking index as
Parameter

Collection Interfaces and Classes :
Interface Implementation
HashSet
Set
TreeSet
ArrayList
List
LinkedList
HashMap
Map
TreeMap

Lecture/core/collection/24

Historical

Vector
Stack
Hashtable
Properties

Page #2

feel the Technology…
Core Java

Debasish Pratihari

Collection Interface:
Methods
public
public
public
public
public
public
public
public
public











Iterator iterator ()
boolean add (Object obj)
boolean addAll (Collection other)
boolean remove (Object obj)
void clear( )
boolean isEmpty ( )
boolean contains (Object obj)
boolean containsAll (collection other)
int size ( )

Iterator interface :


Used for traverse a Collection in forward
direction.

Methods





public
public
public
public

boolean hasNext( )
Object next( )
Object next( )
Object remove ( )

ListIterator interface :


ListIterator interface in extended from Iterator
interface.
 In contrast to Iterator interface ListIterator
interface is used to traverse a collection in
forward or backward.
Method





public
public
public
public

Object next( )
 public boolean hasPrevious( )
boolean hasNext( )  public Object remove( )
Object remove( )
 public boolean add(Object obj)
Object previous( )  public boolean set(Object obj)

Lecture/core/collection/24

Page #3

feel the Technology…
Core Java

Debasish Pratihari

List Interface

Methods







public
public
public
public
public
public

ListIterator listIterator( )
void add(int index, Object obj)
void addAll(int i, Collection list)
Object remove(int indx)
Object set(int indx, Object obj)
int indexOf (Object obj)

Using Set :
25% 

Support basic operation of Collection Interface



Can’t contain duplicate elements.



Implementations





HasSet
TreeSet
LinkedHashSet

Example :
import java.util.*;
class SetDemo{
public static void main(String args[]){
HashSet <String>set= new HashSet<String>();
set.add("one");
set.add("two");
set.add("three");
set.add("four");
set.add("five");
System.out.println(set);
}
}

Lecture/core/collection/24

Page #4

feel the Technology…
Core Java

Debasish Pratihari

Using List :


Supports basic operations of Collection
Interface.



Facilitate positional access and Searching.



Supports range operation.



Implementations:



ArrayList
LinkedList

Example:

import java.util.*;
class ListDemo{
public static void main(String args[]){
LinkedList<Integer> list= new LinkedList<Integer>();

list.add(10);
list.add(20);
list.add(30);
System.out.println(list);
ListIterator itr= list.listIterator();
while(itr.hasNext())
System.out.println(itr.next());
while(itr.hasPrevious())
System.out.println(itr.previous());
}
}

Lecture/core/collection/24

Page #5

feel the Technology…
Core Java

Debasish Pratihari

Using Map :


Elements are stored as key-value pair



A map can’t contain duplicate keys



Accept null as key value.



Implementations:




HashMap
TreeMap
LinkedHashMap

Example :
import java.util.*;
class MapDemo{
public static void main(String args[]){
HashMap<String,String> map
= new HashMap<String,String>();

map.put("one","java");
map.put("two","j2ee");
map.put("three","struts");
map.put("four","hibernate");
map.put("","EJB");
System.out.println(map);
}
}
HashTable vs HashMap :
HashTable
 Synchronized
 Doesn’t accept null
 Order is maintained

Lecture/core/collection/24

vs

HashMap




Un-synchronized
Accept null
No guarantee of
order

Page #6

feel the Technology…

More Related Content

What's hot

Collections In Java
Collections In JavaCollections In Java
Collections In Java
Binoj T E
 
Java Collections Framework
Java  Collections  FrameworkJava  Collections  Framework
Java Collections Framework
guestd8c458
 

What's hot (20)

Object and class
Object and classObject and class
Object and class
 
Wrapper classes
Wrapper classes Wrapper classes
Wrapper classes
 
Java Unit 2(Part 1)
Java Unit 2(Part 1)Java Unit 2(Part 1)
Java Unit 2(Part 1)
 
Java Unit 2 (Part 2)
Java Unit 2 (Part 2)Java Unit 2 (Part 2)
Java Unit 2 (Part 2)
 
Java collections notes
Java collections notesJava collections notes
Java collections notes
 
Collections - Array List
Collections - Array List Collections - Array List
Collections - Array List
 
Collections - Lists, Sets
Collections - Lists, Sets Collections - Lists, Sets
Collections - Lists, Sets
 
ITFT-Classes and object in java
ITFT-Classes and object in javaITFT-Classes and object in java
ITFT-Classes and object in java
 
Collections in Java Notes
Collections in Java NotesCollections in Java Notes
Collections in Java Notes
 
Java Serialization Deep Dive
Java Serialization Deep DiveJava Serialization Deep Dive
Java Serialization Deep Dive
 
Java Unit 2(part 3)
Java Unit 2(part 3)Java Unit 2(part 3)
Java Unit 2(part 3)
 
Scala Collections : Java 8 on Steroids
Scala Collections : Java 8 on SteroidsScala Collections : Java 8 on Steroids
Scala Collections : Java 8 on Steroids
 
Collection framework (completenotes) zeeshan
Collection framework (completenotes) zeeshanCollection framework (completenotes) zeeshan
Collection framework (completenotes) zeeshan
 
Class, object and inheritance in python
Class, object and inheritance in pythonClass, object and inheritance in python
Class, object and inheritance in python
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Collections In Java
Collections In JavaCollections In Java
Collections In Java
 
Wrapper class
Wrapper classWrapper class
Wrapper class
 
Collections - Sorting, Comparing Basics
Collections - Sorting, Comparing Basics Collections - Sorting, Comparing Basics
Collections - Sorting, Comparing Basics
 
Java Collections Framework
Java  Collections  FrameworkJava  Collections  Framework
Java Collections Framework
 
Core & advanced java classes in mumbai
Core & advanced java classes in mumbaiCore & advanced java classes in mumbai
Core & advanced java classes in mumbai
 

Viewers also liked

Pedalando - 09.12.07
Pedalando - 09.12.07Pedalando - 09.12.07
Pedalando - 09.12.07
Jubrac Jacui
 
090703 Sns Miyamura
090703 Sns Miyamura090703 Sns Miyamura
090703 Sns Miyamura
yuu_2003
 
The Eastern Origins Of Western Civilization Editted
The Eastern Origins Of Western Civilization EdittedThe Eastern Origins Of Western Civilization Editted
The Eastern Origins Of Western Civilization Editted
guestecd0c6
 
Google既有商業模式的破壞者3
Google既有商業模式的破壞者3Google既有商業模式的破壞者3
Google既有商業模式的破壞者3
guestc1eec3
 

Viewers also liked (20)

Cradle To Cradle Kort
Cradle To Cradle KortCradle To Cradle Kort
Cradle To Cradle Kort
 
Pedalando - 09.12.07
Pedalando - 09.12.07Pedalando - 09.12.07
Pedalando - 09.12.07
 
Gai
GaiGai
Gai
 
090703 Sns Miyamura
090703 Sns Miyamura090703 Sns Miyamura
090703 Sns Miyamura
 
IT User Apprenticeship at Happy Computers
IT User Apprenticeship at Happy ComputersIT User Apprenticeship at Happy Computers
IT User Apprenticeship at Happy Computers
 
Social media school 2011 webversie
Social media school 2011 webversieSocial media school 2011 webversie
Social media school 2011 webversie
 
javaday 2006 - Tiger
javaday 2006 - Tigerjavaday 2006 - Tiger
javaday 2006 - Tiger
 
Deborah Limb, Learning Pool
Deborah Limb, Learning PoolDeborah Limb, Learning Pool
Deborah Limb, Learning Pool
 
It is All True
It is All TrueIt is All True
It is All True
 
The Eastern Origins Of Western Civilization Editted
The Eastern Origins Of Western Civilization EdittedThe Eastern Origins Of Western Civilization Editted
The Eastern Origins Of Western Civilization Editted
 
Ana Myspace
Ana MyspaceAna Myspace
Ana Myspace
 
Time Saving Techniques Using Author-It
Time Saving Techniques Using Author-ItTime Saving Techniques Using Author-It
Time Saving Techniques Using Author-It
 
Acampa2007
Acampa2007Acampa2007
Acampa2007
 
Visual Art
Visual ArtVisual Art
Visual Art
 
Python e 10 motivos por que devo conhece-la ?
Python e 10 motivos por que devo conhece-la ?Python e 10 motivos por que devo conhece-la ?
Python e 10 motivos por que devo conhece-la ?
 
My Brain Hurts by Y&R
My Brain Hurts by Y&RMy Brain Hurts by Y&R
My Brain Hurts by Y&R
 
Pelajaran 1 Bm
Pelajaran 1 BmPelajaran 1 Bm
Pelajaran 1 Bm
 
Web-Technologies 26.06.2003
Web-Technologies 26.06.2003Web-Technologies 26.06.2003
Web-Technologies 26.06.2003
 
Scmad Chapter08
Scmad Chapter08Scmad Chapter08
Scmad Chapter08
 
Google既有商業模式的破壞者3
Google既有商業模式的破壞者3Google既有商業模式的破壞者3
Google既有商業模式的破壞者3
 

Similar to Lecture 24

Advanced Java - UNIT 3.pptx
Advanced Java - UNIT 3.pptxAdvanced Java - UNIT 3.pptx
Advanced Java - UNIT 3.pptx
eyemitra1
 
Collections in java
Collections in javaCollections in java
Collections in java
kejpretkopet
 
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
 
oop lecture framework,list,maps,collection
oop lecture framework,list,maps,collectionoop lecture framework,list,maps,collection
oop lecture framework,list,maps,collection
ssuseredfbe9
 
Generic Programming &amp; Collection
Generic Programming &amp; CollectionGeneric Programming &amp; Collection
Generic Programming &amp; Collection
Arya
 
Generic Programming &amp; Collection
Generic Programming &amp; CollectionGeneric Programming &amp; Collection
Generic Programming &amp; Collection
Arya
 

Similar to Lecture 24 (20)

Collection framework
Collection frameworkCollection framework
Collection framework
 
11000121065_NAITIK CHATTERJEE.ppt
11000121065_NAITIK CHATTERJEE.ppt11000121065_NAITIK CHATTERJEE.ppt
11000121065_NAITIK CHATTERJEE.ppt
 
Advanced Java - UNIT 3.pptx
Advanced Java - UNIT 3.pptxAdvanced Java - UNIT 3.pptx
Advanced Java - UNIT 3.pptx
 
collections
collectionscollections
collections
 
Collections
CollectionsCollections
Collections
 
Collections in java
Collections in javaCollections in java
Collections in java
 
DSJ_Unit III_Collection framework.pdf
DSJ_Unit III_Collection framework.pdfDSJ_Unit III_Collection framework.pdf
DSJ_Unit III_Collection framework.pdf
 
Collection Framework in java
Collection Framework in javaCollection Framework in java
Collection Framework in java
 
Collections - Lists & sets
Collections - Lists & setsCollections - Lists & sets
Collections - Lists & sets
 
Collections and generic class
Collections and generic classCollections and generic class
Collections and generic class
 
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
 
oop lecture framework,list,maps,collection
oop lecture framework,list,maps,collectionoop lecture framework,list,maps,collection
oop lecture framework,list,maps,collection
 
collection framework.pptx
collection framework.pptxcollection framework.pptx
collection framework.pptx
 
Java Collections Tutorials
Java Collections TutorialsJava Collections Tutorials
Java Collections Tutorials
 
Generic Programming &amp; Collection
Generic Programming &amp; CollectionGeneric Programming &amp; Collection
Generic Programming &amp; Collection
 
Generic Programming &amp; Collection
Generic Programming &amp; CollectionGeneric Programming &amp; Collection
Generic Programming &amp; Collection
 
java collections
java collectionsjava collections
java collections
 
Collectn framework copy
Collectn framework   copyCollectn framework   copy
Collectn framework copy
 
Collectn framework
Collectn frameworkCollectn framework
Collectn framework
 
JavaCollections.ppt
JavaCollections.pptJavaCollections.ppt
JavaCollections.ppt
 

More from Debasish Pratihari (19)

Lecture 23
Lecture 23Lecture 23
Lecture 23
 
Lecture 22
Lecture 22Lecture 22
Lecture 22
 
Lecture 21
Lecture 21Lecture 21
Lecture 21
 
Lecture 20
Lecture 20Lecture 20
Lecture 20
 
Lecture 19
Lecture 19Lecture 19
Lecture 19
 
Lecture 18
Lecture 18Lecture 18
Lecture 18
 
Lecture 17
Lecture 17Lecture 17
Lecture 17
 
Lecture 16
Lecture 16Lecture 16
Lecture 16
 
Lecture 14
Lecture 14Lecture 14
Lecture 14
 
Lecture 10
Lecture 10Lecture 10
Lecture 10
 
Lecture 8
Lecture 8Lecture 8
Lecture 8
 
Lecture 7
Lecture 7Lecture 7
Lecture 7
 
Lecture 6
Lecture 6Lecture 6
Lecture 6
 
Lecture 5
Lecture 5Lecture 5
Lecture 5
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
Lecture 3
Lecture 3Lecture 3
Lecture 3
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
 
Lecture 1
Lecture 1Lecture 1
Lecture 1
 
Lecture25
Lecture25Lecture25
Lecture25
 

Recently uploaded

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 

Recently uploaded (20)

Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 

Lecture 24

  • 1. Core Java Debasish Pratihari Collection:  A collection is a generic term for any object that represents a set of objects grouped together by some means or mechanism inside the memory.  Collection Framework is a specification by which we can store and manage objects of heterogeneous kinds in memory.   Collection Classes are also called Container classes We can define collection as a java data structure capable of storing & managing different kinds of objects.  Note : A collection has a capacity to grow dynamically. Collection Framework Provides:  Interface  Implementation  Algorithms Core Interfaces : Collection Set List Map SortedMap Queue SortedSet Lecture/core/collection/24 Page #1 feel the Technology…
  • 2. Core Java Debasish Pratihari Types of Collection:  The way a collection (group of objects) is implemented in memory, accordingly it is categorized into following categories.  Set  List  Map Traversing Collections:  The Followings are the ways to traverse a collection:     For-each loop Iterator ListItetor Using Methods taking index as Parameter Collection Interfaces and Classes : Interface Implementation HashSet Set TreeSet ArrayList List LinkedList HashMap Map TreeMap Lecture/core/collection/24 Historical Vector Stack Hashtable Properties Page #2 feel the Technology…
  • 3. Core Java Debasish Pratihari Collection Interface: Methods public public public public public public public public public          Iterator iterator () boolean add (Object obj) boolean addAll (Collection other) boolean remove (Object obj) void clear( ) boolean isEmpty ( ) boolean contains (Object obj) boolean containsAll (collection other) int size ( ) Iterator interface :  Used for traverse a Collection in forward direction. Methods     public public public public boolean hasNext( ) Object next( ) Object next( ) Object remove ( ) ListIterator interface :  ListIterator interface in extended from Iterator interface.  In contrast to Iterator interface ListIterator interface is used to traverse a collection in forward or backward. Method     public public public public Object next( )  public boolean hasPrevious( ) boolean hasNext( )  public Object remove( ) Object remove( )  public boolean add(Object obj) Object previous( )  public boolean set(Object obj) Lecture/core/collection/24 Page #3 feel the Technology…
  • 4. Core Java Debasish Pratihari List Interface Methods       public public public public public public ListIterator listIterator( ) void add(int index, Object obj) void addAll(int i, Collection list) Object remove(int indx) Object set(int indx, Object obj) int indexOf (Object obj) Using Set : 25%  Support basic operation of Collection Interface  Can’t contain duplicate elements.  Implementations    HasSet TreeSet LinkedHashSet Example : import java.util.*; class SetDemo{ public static void main(String args[]){ HashSet <String>set= new HashSet<String>(); set.add("one"); set.add("two"); set.add("three"); set.add("four"); set.add("five"); System.out.println(set); } } Lecture/core/collection/24 Page #4 feel the Technology…
  • 5. Core Java Debasish Pratihari Using List :  Supports basic operations of Collection Interface.  Facilitate positional access and Searching.  Supports range operation.  Implementations:   ArrayList LinkedList Example: import java.util.*; class ListDemo{ public static void main(String args[]){ LinkedList<Integer> list= new LinkedList<Integer>(); list.add(10); list.add(20); list.add(30); System.out.println(list); ListIterator itr= list.listIterator(); while(itr.hasNext()) System.out.println(itr.next()); while(itr.hasPrevious()) System.out.println(itr.previous()); } } Lecture/core/collection/24 Page #5 feel the Technology…
  • 6. Core Java Debasish Pratihari Using Map :  Elements are stored as key-value pair  A map can’t contain duplicate keys  Accept null as key value.  Implementations:    HashMap TreeMap LinkedHashMap Example : import java.util.*; class MapDemo{ public static void main(String args[]){ HashMap<String,String> map = new HashMap<String,String>(); map.put("one","java"); map.put("two","j2ee"); map.put("three","struts"); map.put("four","hibernate"); map.put("","EJB"); System.out.println(map); } } HashTable vs HashMap : HashTable  Synchronized  Doesn’t accept null  Order is maintained Lecture/core/collection/24 vs HashMap    Un-synchronized Accept null No guarantee of order Page #6 feel the Technology…