SlideShare a Scribd company logo
COLLECTION
FRAMEWORK
C O L L E C TION
FRAMEWORK
The collections framework is a unified architecture for
representing and manipulating collections, enabling
them to be manipulated independently of the details
of their representation. It reduces programming effort
while increasing performance. It enables
interoperability among unrelated APIs, reduces effort
in designing and learning new APIs, and fosters
software reuse. The framework is based on more than
a dozen collection interfaces. It includes
implementations of these interfaces and algorithms to
manipulate them.
CONTENTS
𝗈 What is C ollection?
𝗈 C ollections Framework
𝗈 C ollections Hierarchy
𝗈 Collections Implementations
● Set
● List
● Map
OB JE CTIVE S
𝗈 Define a collection
𝗈 Describe the collections framework
𝗈 Describe the collections hierarchy
𝗈 Demonstrate each collection implementation
WHAT IS A COLLECTION?
𝗈 A Collection (also known as container) is an object that contains a
group of objects treated as a single unit.
𝗈 Any type of objects can be stored, retrieved and manipulated as
elements of collections.
CO L L ECT IO N S FRAMEWORK
Collections Framework is a unified architecture for
managing collections
Main Parts of Collections Framework
1. Interfaces
Core interfaces defining common functionality exhibited by
collections
1. Implementations
Concrete classes of the core interfaces providing data structures
1. Operations
Methods that perform various operations on collections
C OLLE C TIONS FRAMEWORK
INTERFACES
CoreInterface Description
Collection
Set
SortedSet
List
specifies contract that all collections should implement
defines functionality for a set of unique elements
defines functionality for a set where elements are sorted
defines functionality for an ordered list of non- unique elements
Map defines functionality for mapping of unique keys to values
SortedMap defines functionality for a map where its keys are sorted
COLLECTIONS FRAMEWORK
IMPLEMENTATIONS
Set List Map
HashSet ArrayList HashMap
LinkedHashSet LinkedList LinkedHashMap
TreeSet Vector Hashtable
Tree Map
Note: Hashtable uses a lower-case “t”
OPE RATIONS
Basic collection operations:
𝗈 Check if collection is empty
𝗈 Check if an object exists in collection.
𝗈 Retrieve an object from collection
𝗈 Add object to collection
𝗈 Remove object from collection
𝗈 Iterate collection and inspect each object
● Each operation has a corresponding method implementation for
each collection type
CO L L ECT IO N S
C HARAC T E RISTIC S
𝗈 Ordered
● Elements are stored and accessed in a specific
order
𝗈 Sorted
● Elements are stored and accessed in a sorted
order
𝗈 Indexed
● Elements can be accessed using an index
𝗈 Unique
● Collection does not allow duplicates
ITERATOR
𝗈 An iterator is an object used to mark a position in a
collection of data and to move from item to item within
the collection
Syntax:
Iterator <variable> = <CollectionObject>.iterator();
COLLECTIONS HIERARCHY
SET AND LIST
HashSet
Collection
SortedSet
List
Set
LinkedHashSet TreeSet LinkedList Vector ArrayList
implements
implements
implements
implements extends
extends
COLLECTIONS HIERARCHY
MAP
SortedMap
Hashtable HashMap
LinkedHashMap
implements
TreeMap
Map
extends
implements
extends
CO L L ECT IO N IMPLEMENTATIONS
Set : Unique things (classes that implement Set)
Map : Things with a unique ID (classes that implement Map)
List : Lists of things (classes that implement List)
L IST
A List cares about the index.
“Paul”
“Paul” “Mark”
“Mark” “John”
“John” “Paul”
“Paul” “Luke”
“Luke”
value
index 0 1 2 3 4
LinkedList
Vector
ArrayList
LIST IMPLEMENTATIONS
ARRAY LIST
import java.util.ArrayList;
public class MyArrayList {
public static void main(String args[ ]) {
ArrayList alist = new ArrayList( );
alist.add(new String("One"));
alist.add(new String("Two"));
alist.add(new String("Three"));
System.out.println(alist.get(0));
System.out.println(alist.get(1));
System.out.println(alist.get(2));
}
}
One
Two
Three
LIST IMPLEMENTATIONS
VECTOR
import java.util.Vector;
public class MyVector {
public static void main(String args[ ]) {
Vector vecky = new Vector( );
vecky.add(new Integer(1));
vecky.add(new Integer(2));
vecky.add(new Integer(3));
for(int x=0; x<3; x++) {
System.out.println(vecky.get(x));
}
}
}
1
2
3
LIST IMPLEMENTATIONS
LINKED LIST
import java.util.LinkedList;
public class MyLinkedList {
public static void main(String args[ ]) {
LinkedList link = new LinkedList( );
link.add(new Double(2.0));
link.addLast(new Double(3.0));
link.addFirst(new Double(1.0));
Object array[ ] = link.toArray( );
for(int x=0; x<3; x++) {
System.out.println(array[x]);
}
}
}
1.0
2.0
3.0
S E T
A Set cares about uniqueness, it doesn’t allow duplicates.
“Paul”
“Paul”
“Mark
”
“Mark”
“John”
“John”
“Luke”
“Luke”
“Fred”
“Fred”
“Peter”
“Peter”
TreeSet
LinkedHashSet
HashSet
SET IMPLEMENTATIONS
HASH SET
import java.util.*;
public class MyHashSet {
public static void main(String args[ ]) {
HashSet hash = new HashSet( );
hash.add("a");
hash.add("b");
hash.add("c");
hash.add("d");
Iterator iterator = hash.iterator( );
while(iterator.hasNext( )) {
System.out.println(iterator.next( ));
}
}
}
d
a
c
b
SET IMPLEMENTATIONS
LINKED HASH SET
import java.util.LinkedHashSet;
public class MyLinkedHashSet {
public static void main(String args[ ]) {
LinkedHashSet lhs = new LinkedHashSet();
lhs.add(new String("One"));
lhs.add(new String("Two"));
lhs.add(new String("Three"));
Object array[] = lhs.toArray( );
for(int x=0; x<3; x++) {
System.out.println(array[x]);
}
}
}
One
Two
Three
SET IMPLEMENTATIONS
TREE SET
import java.util.TreeSet;
import java.util.Iterator;
public class MyTreeSet {
public static void main(String args[ ]) {
TreeSet tree = new TreeSet();
tree.add("Jody");
tree.add("Remiel");
tree.add("Reggie");
tree.add("Philippe");
Iterator iterator = tree.iterator( );
while(iterator.hasNext( )) {
System.out.println(iterator.next( ).toString( ));
}
}
}
Jody
Philippe
Reggie
Remiel
MAP
A Map cares about unique identifiers.
“Paul”
“Paul” “Mark
”
“Mark” “John”
“John” “Paul”
“Paul” “Luke”
“Luke”
key
value
“Pl” “Ma” “Jn” “ul” “Le”
LinkedHashMap TreeMap
Hashtable
HashMap
MAP IMPLEMENTATIONS
HASH MAP
import java.util.HashMap;
public class MyHashMap {
public static void main(String args[ ]) {
HashMap map = new HashMap( );
map.put("name", "Jody");
map.put("id", new Integer(446));
map.put("address", "Manila");
System.out.println("Name: " + map.get("name"));
System.out.println("ID: " + map.get("id"));
System.out.println("Address: " + map.get("address"));
}
}
Name: Jody
ID: 446
Address: Manila
MAP IMPLEMENTATIONS
HASH TABLE
import java.util.Hashtable;
public class MyHashtable {
public static void main(String args[ ]) {
Hashtable table = new Hashtable( );
table.put("name", "Jody");
table.put("id", new Integer(1001));
table.put("address", new String("Manila"));
System.out.println("Table of Contents:" + table);
}
}
Table of Contents:
{address=Manila, name=Jody, id=1001}
MAP IMPLEMENTATIONS
LINKED HASH MAP
import java.util.*;
public class MyLinkedHashMap {
public static void main(String args[ ]) {
int iNum = 0;
LinkedHashMap myMap = new LinkedHashMap( );
myMap.put("name", "Jody");
myMap.put("id", new Integer(446));
myMap.put("address", "Manila");
myMap.put("type", "Savings");
Collection values = myMap.values( );
Iterator iterator = values.iterator( );
while(iterator.hasNext()) {
System.out.println(iterator.next( ));
}
}
}
Jody
446
Manila
Savings
MAP IMPLEMENTATIONS
TREE MAP
public class MyTreeMap {
public static void main(String args[]) {
TreeMap treeMap = new TreeMap( );
treeMap.put("name", "Jody");
treeMap.put("id", new Integer(446));
treeMap.put("address", "Manila");
Collection values = treeMap.values()
Iterator iterator = values.iterator( );
System.out.println("Printing the VALUES....");
while (iterator.hasNext()) {
System.out.println(iterator.next( ));
}
}
}
import java.util.*;
Printing the VALUES....
Manila
446
Jody
CO L L ECT IO N C L A S S E S SUMMARY
Class Map Set List Ordered Sorted
HashMap X No No
Hashtable X No No
TreeMap X Sorted By natural order or
custom comparison rules
LinkedHashMap X By insertion order or
last access order
No
HashSet X No No
TreeSet X Sorted By natural order or
custom comparison rules
LinkedHashSet X By insertion order or
last access order
No
ArrayList X By index No
Vector X By index No
LinkedList X By index No
KEY POINTS
𝗈 Collections Framework contains:
1. Interfaces
2. Implementations
3. Operations
A list cares about the index.
A set cares about uniqueness, it does not allow
duplicates.
A map cares about unique identifiers.
𝗈
𝗈
𝗈

More Related Content

Similar to collectionframework-141116005344-conversion-gate01.pptx

22.ppt
22.ppt22.ppt
Collections Api - Java
Collections Api - JavaCollections Api - Java
Collections Api - Java
Drishti Bhalla
 
Collections and generics
Collections and genericsCollections and generics
Collections and generics
Muthukumaran Subramanian
 
Collections generic
Collections genericCollections generic
Collections generic
sandhish
 
ArrayList.docx
ArrayList.docxArrayList.docx
ArrayList.docx
veerendranath12
 
oop lecture framework,list,maps,collection
oop lecture framework,list,maps,collectionoop lecture framework,list,maps,collection
oop lecture framework,list,maps,collection
ssuseredfbe9
 
07 java collection
07 java collection07 java collection
07 java collection
Abhishek Khune
 
List in java
List in javaList in java
List in java
nitin kumar
 
Java Collections
Java  Collections Java  Collections
Collections Framework
Collections FrameworkCollections Framework
Collections Framework
Sunil OS
 
COLLECTIONS.pptx
COLLECTIONS.pptxCOLLECTIONS.pptx
COLLECTIONS.pptx
SruthyPJ
 
Best core & advanced java classes in mumbai
Best core & advanced java classes in mumbaiBest core & advanced java classes in mumbai
Best core & advanced java classes in mumbai
Vibrant Technologies & Computers
 
Java Collections Tutorials
Java Collections TutorialsJava Collections Tutorials
Java Collections Tutorials
Prof. Erwin Globio
 
Lists
ListsLists
Collections Framework Beginners Guide 2
Collections Framework Beginners Guide 2Collections Framework Beginners Guide 2
Collections Framework Beginners Guide 2
Kenji HASUNUMA
 
Collections Framework Begineers guide 2
Collections Framework Begineers guide 2Collections Framework Begineers guide 2
Collections Framework Begineers guide 2
Kenji HASUNUMA
 
lec6.ppt
lec6.pptlec6.ppt
lec6.ppt
cawarir
 
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 collectionframework-141116005344-conversion-gate01.pptx (20)

22.ppt
22.ppt22.ppt
22.ppt
 
Collections Api - Java
Collections Api - JavaCollections Api - Java
Collections Api - Java
 
Collections and generics
Collections and genericsCollections and generics
Collections and generics
 
Collections generic
Collections genericCollections generic
Collections generic
 
ArrayList.docx
ArrayList.docxArrayList.docx
ArrayList.docx
 
oop lecture framework,list,maps,collection
oop lecture framework,list,maps,collectionoop lecture framework,list,maps,collection
oop lecture framework,list,maps,collection
 
07 java collection
07 java collection07 java collection
07 java collection
 
List in java
List in javaList in java
List in java
 
Java Collections
Java  Collections Java  Collections
Java Collections
 
Collections Framework
Collections FrameworkCollections Framework
Collections Framework
 
COLLECTIONS.pptx
COLLECTIONS.pptxCOLLECTIONS.pptx
COLLECTIONS.pptx
 
Best core & advanced java classes in mumbai
Best core & advanced java classes in mumbaiBest core & advanced java classes in mumbai
Best core & advanced java classes in mumbai
 
Java Collections Tutorials
Java Collections TutorialsJava Collections Tutorials
Java Collections Tutorials
 
Collections
CollectionsCollections
Collections
 
Lists
ListsLists
Lists
 
Collections Framework Beginners Guide 2
Collections Framework Beginners Guide 2Collections Framework Beginners Guide 2
Collections Framework Beginners Guide 2
 
Collections Framework Begineers guide 2
Collections Framework Begineers guide 2Collections Framework Begineers guide 2
Collections Framework Begineers guide 2
 
lec6.ppt
lec6.pptlec6.ppt
lec6.ppt
 
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
 

Recently uploaded

NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
Amil Baba Dawood bangali
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
JoytuBarua2
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
gerogepatton
 
Fundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptxFundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptx
manasideore6
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
ViniHema
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
FluxPrime1
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
Osamah Alsalih
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
Divya Somashekar
 
The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdf
Pipe Restoration Solutions
 
Hierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power SystemHierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power System
Kerry Sado
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
Pratik Pawar
 
ML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptxML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptx
Vijay Dialani, PhD
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
obonagu
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
seandesed
 
AP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specificAP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specific
BrazilAccount1
 
WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
AafreenAbuthahir2
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
Robbie Edward Sayers
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
bakpo1
 
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
MLILAB
 
Runway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptxRunway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptx
SupreethSP4
 

Recently uploaded (20)

NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
 
Fundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptxFundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptx
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
 
The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdf
 
Hierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power SystemHierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power System
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
 
ML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptxML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptx
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
 
AP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specificAP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specific
 
WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
 
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
 
Runway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptxRunway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptx
 

collectionframework-141116005344-conversion-gate01.pptx

  • 2. C O L L E C TION FRAMEWORK The collections framework is a unified architecture for representing and manipulating collections, enabling them to be manipulated independently of the details of their representation. It reduces programming effort while increasing performance. It enables interoperability among unrelated APIs, reduces effort in designing and learning new APIs, and fosters software reuse. The framework is based on more than a dozen collection interfaces. It includes implementations of these interfaces and algorithms to manipulate them.
  • 3. CONTENTS 𝗈 What is C ollection? 𝗈 C ollections Framework 𝗈 C ollections Hierarchy 𝗈 Collections Implementations ● Set ● List ● Map
  • 4. OB JE CTIVE S 𝗈 Define a collection 𝗈 Describe the collections framework 𝗈 Describe the collections hierarchy 𝗈 Demonstrate each collection implementation
  • 5. WHAT IS A COLLECTION? 𝗈 A Collection (also known as container) is an object that contains a group of objects treated as a single unit. 𝗈 Any type of objects can be stored, retrieved and manipulated as elements of collections.
  • 6. CO L L ECT IO N S FRAMEWORK Collections Framework is a unified architecture for managing collections Main Parts of Collections Framework 1. Interfaces Core interfaces defining common functionality exhibited by collections 1. Implementations Concrete classes of the core interfaces providing data structures 1. Operations Methods that perform various operations on collections
  • 7. C OLLE C TIONS FRAMEWORK INTERFACES CoreInterface Description Collection Set SortedSet List specifies contract that all collections should implement defines functionality for a set of unique elements defines functionality for a set where elements are sorted defines functionality for an ordered list of non- unique elements Map defines functionality for mapping of unique keys to values SortedMap defines functionality for a map where its keys are sorted
  • 8. COLLECTIONS FRAMEWORK IMPLEMENTATIONS Set List Map HashSet ArrayList HashMap LinkedHashSet LinkedList LinkedHashMap TreeSet Vector Hashtable Tree Map Note: Hashtable uses a lower-case “t”
  • 9. OPE RATIONS Basic collection operations: 𝗈 Check if collection is empty 𝗈 Check if an object exists in collection. 𝗈 Retrieve an object from collection 𝗈 Add object to collection 𝗈 Remove object from collection 𝗈 Iterate collection and inspect each object ● Each operation has a corresponding method implementation for each collection type
  • 10. CO L L ECT IO N S C HARAC T E RISTIC S 𝗈 Ordered ● Elements are stored and accessed in a specific order 𝗈 Sorted ● Elements are stored and accessed in a sorted order 𝗈 Indexed ● Elements can be accessed using an index 𝗈 Unique ● Collection does not allow duplicates
  • 11. ITERATOR 𝗈 An iterator is an object used to mark a position in a collection of data and to move from item to item within the collection Syntax: Iterator <variable> = <CollectionObject>.iterator();
  • 12. COLLECTIONS HIERARCHY SET AND LIST HashSet Collection SortedSet List Set LinkedHashSet TreeSet LinkedList Vector ArrayList implements implements implements implements extends extends
  • 14. CO L L ECT IO N IMPLEMENTATIONS Set : Unique things (classes that implement Set) Map : Things with a unique ID (classes that implement Map) List : Lists of things (classes that implement List)
  • 15. L IST A List cares about the index. “Paul” “Paul” “Mark” “Mark” “John” “John” “Paul” “Paul” “Luke” “Luke” value index 0 1 2 3 4 LinkedList Vector ArrayList
  • 16. LIST IMPLEMENTATIONS ARRAY LIST import java.util.ArrayList; public class MyArrayList { public static void main(String args[ ]) { ArrayList alist = new ArrayList( ); alist.add(new String("One")); alist.add(new String("Two")); alist.add(new String("Three")); System.out.println(alist.get(0)); System.out.println(alist.get(1)); System.out.println(alist.get(2)); } } One Two Three
  • 17. LIST IMPLEMENTATIONS VECTOR import java.util.Vector; public class MyVector { public static void main(String args[ ]) { Vector vecky = new Vector( ); vecky.add(new Integer(1)); vecky.add(new Integer(2)); vecky.add(new Integer(3)); for(int x=0; x<3; x++) { System.out.println(vecky.get(x)); } } } 1 2 3
  • 18. LIST IMPLEMENTATIONS LINKED LIST import java.util.LinkedList; public class MyLinkedList { public static void main(String args[ ]) { LinkedList link = new LinkedList( ); link.add(new Double(2.0)); link.addLast(new Double(3.0)); link.addFirst(new Double(1.0)); Object array[ ] = link.toArray( ); for(int x=0; x<3; x++) { System.out.println(array[x]); } } } 1.0 2.0 3.0
  • 19. S E T A Set cares about uniqueness, it doesn’t allow duplicates. “Paul” “Paul” “Mark ” “Mark” “John” “John” “Luke” “Luke” “Fred” “Fred” “Peter” “Peter” TreeSet LinkedHashSet HashSet
  • 20. SET IMPLEMENTATIONS HASH SET import java.util.*; public class MyHashSet { public static void main(String args[ ]) { HashSet hash = new HashSet( ); hash.add("a"); hash.add("b"); hash.add("c"); hash.add("d"); Iterator iterator = hash.iterator( ); while(iterator.hasNext( )) { System.out.println(iterator.next( )); } } } d a c b
  • 21. SET IMPLEMENTATIONS LINKED HASH SET import java.util.LinkedHashSet; public class MyLinkedHashSet { public static void main(String args[ ]) { LinkedHashSet lhs = new LinkedHashSet(); lhs.add(new String("One")); lhs.add(new String("Two")); lhs.add(new String("Three")); Object array[] = lhs.toArray( ); for(int x=0; x<3; x++) { System.out.println(array[x]); } } } One Two Three
  • 22. SET IMPLEMENTATIONS TREE SET import java.util.TreeSet; import java.util.Iterator; public class MyTreeSet { public static void main(String args[ ]) { TreeSet tree = new TreeSet(); tree.add("Jody"); tree.add("Remiel"); tree.add("Reggie"); tree.add("Philippe"); Iterator iterator = tree.iterator( ); while(iterator.hasNext( )) { System.out.println(iterator.next( ).toString( )); } } } Jody Philippe Reggie Remiel
  • 23. MAP A Map cares about unique identifiers. “Paul” “Paul” “Mark ” “Mark” “John” “John” “Paul” “Paul” “Luke” “Luke” key value “Pl” “Ma” “Jn” “ul” “Le” LinkedHashMap TreeMap Hashtable HashMap
  • 24. MAP IMPLEMENTATIONS HASH MAP import java.util.HashMap; public class MyHashMap { public static void main(String args[ ]) { HashMap map = new HashMap( ); map.put("name", "Jody"); map.put("id", new Integer(446)); map.put("address", "Manila"); System.out.println("Name: " + map.get("name")); System.out.println("ID: " + map.get("id")); System.out.println("Address: " + map.get("address")); } } Name: Jody ID: 446 Address: Manila
  • 25. MAP IMPLEMENTATIONS HASH TABLE import java.util.Hashtable; public class MyHashtable { public static void main(String args[ ]) { Hashtable table = new Hashtable( ); table.put("name", "Jody"); table.put("id", new Integer(1001)); table.put("address", new String("Manila")); System.out.println("Table of Contents:" + table); } } Table of Contents: {address=Manila, name=Jody, id=1001}
  • 26. MAP IMPLEMENTATIONS LINKED HASH MAP import java.util.*; public class MyLinkedHashMap { public static void main(String args[ ]) { int iNum = 0; LinkedHashMap myMap = new LinkedHashMap( ); myMap.put("name", "Jody"); myMap.put("id", new Integer(446)); myMap.put("address", "Manila"); myMap.put("type", "Savings"); Collection values = myMap.values( ); Iterator iterator = values.iterator( ); while(iterator.hasNext()) { System.out.println(iterator.next( )); } } } Jody 446 Manila Savings
  • 27. MAP IMPLEMENTATIONS TREE MAP public class MyTreeMap { public static void main(String args[]) { TreeMap treeMap = new TreeMap( ); treeMap.put("name", "Jody"); treeMap.put("id", new Integer(446)); treeMap.put("address", "Manila"); Collection values = treeMap.values() Iterator iterator = values.iterator( ); System.out.println("Printing the VALUES...."); while (iterator.hasNext()) { System.out.println(iterator.next( )); } } } import java.util.*; Printing the VALUES.... Manila 446 Jody
  • 28. CO L L ECT IO N C L A S S E S SUMMARY Class Map Set List Ordered Sorted HashMap X No No Hashtable X No No TreeMap X Sorted By natural order or custom comparison rules LinkedHashMap X By insertion order or last access order No HashSet X No No TreeSet X Sorted By natural order or custom comparison rules LinkedHashSet X By insertion order or last access order No ArrayList X By index No Vector X By index No LinkedList X By index No
  • 29. KEY POINTS 𝗈 Collections Framework contains: 1. Interfaces 2. Implementations 3. Operations A list cares about the index. A set cares about uniqueness, it does not allow duplicates. A map cares about unique identifiers. 𝗈 𝗈 𝗈