SlideShare a Scribd company logo
Lecture-11
Instructor Name:
Object Oriented Programming
Today’s Lecture
 The Concept of Map
 HashMap
 HashSet
2
MAP
The Concept of Map
 A map is a collection of key/value pairs of objects.
 As with the ArrayList, a map can store a flexible number of entries.
 One difference between the ArrayList and a Map is that with a Map
 each entry is not an object, but a pair of objects.
 This pair consists of a key object and a value object.
 Instead of looking up entries in this collection using an integer index
(as we did with the ArrayList), we use the key object to look up the
value object.
3
MAP
Everyday Example of Map
 An everyday example of a map is a telephone directory.
 A telephone directory contains entries, and each entry is a pair: a name and a
phone number.
 You use a phone book by looking up a name and getting a phone number.
 We do not use an index—the position of the entry in the phone book—to find
it.
 A map can be organized in such a way that looking up a value for a key is
easy. In the case of a phone book, this is done using alphabetical sorting.
 By storing the entries in the alphabetical order of their keys, finding the key
and looking up the value is easy
4
HashMap
Using HashMap
 HashMap is a particular implementation of Map.
 The HashMap class uses a hashtable to implement the Map interface.
 The most important methods of the HashMap class are put and get.
 The put method inserts an entry into the map
 The get method retrieves the value for a given key.
 Important thing is that NULL is not allowed in the map.
 Object must be compatible with the elements in the map.
5
HashMap
Using HashMap
 The following code fragment creates a HashMap and inserts three entries into
it. Each entry is a key/value pair consisting of a name and a telephone
number.
HashMap<String, String> phoneBook = new HashMap<String, String>();
phoneBook.put(“Muhammad Safyan", “(300) 9999 999");
phoneBook.put(“Atif Ishaq", "(333) 8888 888");
phoneBook.put(“Muhammad Asif", "(321) 7777 777");
 The following code will find the phone number for Atif Ishaq and print it.
String number = phoneBook.get(“Atif Ishaq");
System.out.println(number); 6
HashMap
Address Book Example
import java.util.HashMap;
public class PhoneBook
{
HashMap<String,String> phonebook=new HashMap<String,String>();
public void enterNumber(String name, String number){
phonebook.put(name,number);
}
public String lookUpNumber(String name){
String number = phonebook.get(name);
return number;
}
}
7
HashMap
HashMapTester Class
import java.util.Scanner;
public class HashMapTester
{
public static void main(String[] args){
PhoneBook phonebook=new PhoneBook();
Scanner sc=new Scanner(System.in);
String opt="Y";
do{
System.out.print("Enter Name : ");
String name=sc.next();
System.out.print("Enter Number : ");
String number=sc.next();
phonebook.enterNumber(name,number);
System.out.print ("Do you want to add more Numbers : ");
opt=sc.next();
}while(opt.equals("Y"));
}
} 8
HashMap
What Happens if you add an entry to map
with a key that already exists in the map?
 Duplicate Key values are not allowed in the HashMap.
 If a Key already exists, it will replace its value with new value.
phoneBook.put(“Muhammad Asif", "(321) 7777 777");
phoneBook.put(“Muhammad Asif", "(332) 6666 777");
 It will replace the phone number 7777 777 with 6666 777 against the key
Muhammad Asif. No New will be created in the HashMap.
9
HashMap
How a value can be removed from HashMap?
public void removeEntry(String name){
if(phonebook.containsKey(name))
phonebook.remove(name);
else
System.out.println("Invalid Entry");
}
10
HashMap
How you can check the total number of
entries in the HashMap?
public int totalNumber(){
return phonebook.size();
}
11
HashSet
12
Map
13
14

More Related Content

PPTX
Compiler design
ZIP
Hashing
PPTX
Hashing 1
PDF
Hashing notes data structures (HASHING AND HASH FUNCTIONS)
PPT
358 33 powerpoint-slides_15-hashing-collision_chapter-15
PPTX
linear probing
PPTX
Hashing
PPT
Ch17 Hashing
Compiler design
Hashing
Hashing 1
Hashing notes data structures (HASHING AND HASH FUNCTIONS)
358 33 powerpoint-slides_15-hashing-collision_chapter-15
linear probing
Hashing
Ch17 Hashing

What's hot (20)

PDF
Hashing and Hash Tables
PDF
08 Hash Tables
PPS
Ds 8
PPTX
Quadratic probing
PPT
Hashing PPT
PPTX
Hashing In Data Structure
PPTX
Hashing Technique In Data Structures
PPT
Concept of hashing
PPTX
Hashing
PPT
Hashing
PDF
Application of hashing in better alg design tanmay
PPTX
Hashing in datastructure
PPTX
Hash tables
PPTX
Hashing
PPT
Data Structure and Algorithms Hashing
PPT
18 hashing
PPTX
Hashing data
PPTX
Rehashing
PDF
Bloom Filters: An Introduction
PPT
Chapter 12 ds
Hashing and Hash Tables
08 Hash Tables
Ds 8
Quadratic probing
Hashing PPT
Hashing In Data Structure
Hashing Technique In Data Structures
Concept of hashing
Hashing
Hashing
Application of hashing in better alg design tanmay
Hashing in datastructure
Hash tables
Hashing
Data Structure and Algorithms Hashing
18 hashing
Hashing data
Rehashing
Bloom Filters: An Introduction
Chapter 12 ds
Ad

Viewers also liked (11)

PDF
Prp_Portfolio
PDF
Ost 1 10916 73
PDF
DiceSimulatorProgram
DOCX
Portad amichi^^
PDF
Stb 14969
DOCX
Word seminar rs. muhammadiyah palembang (repaired)
PPSX
инђија
PPTX
Album
PPTX
Cubismo
Prp_Portfolio
Ost 1 10916 73
DiceSimulatorProgram
Portad amichi^^
Stb 14969
Word seminar rs. muhammadiyah palembang (repaired)
инђија
Album
Cubismo
Ad

Similar to Lecture 11 (20)

PDF
Lecture notesmap
PPT
WDD_lec_06.ppt
PDF
Hash map (java platform se 8 )
PPTX
Java Collections.pptx
PPTX
Map-Interface-and-HashMap-in-Java-A-Deep-Dive.pptx.pptx
PPT
description of Collections, seaching & Sorting
PPTX
Basic_Understanding_of_Java_HashMap_Quipoin.pptx
PPTX
collection framework in java
PPTX
Collection and framework
PPTX
javaprograming-COLLECTION FRAMEWORK-171012084019.pptx
PPT
hashcode hash map hashing hashset hashing
PPT
sets and maps
PPTX
Session 20 - Collections - Maps
PPT
Java Presentation
PPTX
Java Hands-On Workshop
PPTX
LJ_JAVA_FS_Collection.pptx
PPTX
Java util
DOC
24 collections framework interview questions
PDF
Collections in Java Interview Questions PDF By ScholarHat
Lecture notesmap
WDD_lec_06.ppt
Hash map (java platform se 8 )
Java Collections.pptx
Map-Interface-and-HashMap-in-Java-A-Deep-Dive.pptx.pptx
description of Collections, seaching & Sorting
Basic_Understanding_of_Java_HashMap_Quipoin.pptx
collection framework in java
Collection and framework
javaprograming-COLLECTION FRAMEWORK-171012084019.pptx
hashcode hash map hashing hashset hashing
sets and maps
Session 20 - Collections - Maps
Java Presentation
Java Hands-On Workshop
LJ_JAVA_FS_Collection.pptx
Java util
24 collections framework interview questions
Collections in Java Interview Questions PDF By ScholarHat

More from talha ijaz (16)

PPTX
Lecture 23-24.pptx
PPTX
Lecture 22
PPTX
Lecture 20-21
PPTX
Lecture 19
PPTX
Lecture 18
PPTX
Lecture 17
PPTX
Lecture 12
PPTX
Lecture 9
PPTX
Lecture 7
PPTX
Lecture 5
PPT
Visual perception
PPT
Introduction
PPTX
Lecture 4
PPTX
Lecture 3
PPTX
Lecture 2
PPTX
Lecture 1
Lecture 23-24.pptx
Lecture 22
Lecture 20-21
Lecture 19
Lecture 18
Lecture 17
Lecture 12
Lecture 9
Lecture 7
Lecture 5
Visual perception
Introduction
Lecture 4
Lecture 3
Lecture 2
Lecture 1

Recently uploaded (20)

PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPT
Teaching material agriculture food technology
PDF
Machine learning based COVID-19 study performance prediction
PPTX
Cloud computing and distributed systems.
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
cuic standard and advanced reporting.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Review of recent advances in non-invasive hemoglobin estimation
MIND Revenue Release Quarter 2 2025 Press Release
Mobile App Security Testing_ A Comprehensive Guide.pdf
Building Integrated photovoltaic BIPV_UPV.pdf
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Teaching material agriculture food technology
Machine learning based COVID-19 study performance prediction
Cloud computing and distributed systems.
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
gpt5_lecture_notes_comprehensive_20250812015547.pdf
“AI and Expert System Decision Support & Business Intelligence Systems”
Reach Out and Touch Someone: Haptics and Empathic Computing
A comparative analysis of optical character recognition models for extracting...
cuic standard and advanced reporting.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
Programs and apps: productivity, graphics, security and other tools
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
The Rise and Fall of 3GPP – Time for a Sabbatical?
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Review of recent advances in non-invasive hemoglobin estimation

Lecture 11

  • 2. Today’s Lecture  The Concept of Map  HashMap  HashSet 2
  • 3. MAP The Concept of Map  A map is a collection of key/value pairs of objects.  As with the ArrayList, a map can store a flexible number of entries.  One difference between the ArrayList and a Map is that with a Map  each entry is not an object, but a pair of objects.  This pair consists of a key object and a value object.  Instead of looking up entries in this collection using an integer index (as we did with the ArrayList), we use the key object to look up the value object. 3
  • 4. MAP Everyday Example of Map  An everyday example of a map is a telephone directory.  A telephone directory contains entries, and each entry is a pair: a name and a phone number.  You use a phone book by looking up a name and getting a phone number.  We do not use an index—the position of the entry in the phone book—to find it.  A map can be organized in such a way that looking up a value for a key is easy. In the case of a phone book, this is done using alphabetical sorting.  By storing the entries in the alphabetical order of their keys, finding the key and looking up the value is easy 4
  • 5. HashMap Using HashMap  HashMap is a particular implementation of Map.  The HashMap class uses a hashtable to implement the Map interface.  The most important methods of the HashMap class are put and get.  The put method inserts an entry into the map  The get method retrieves the value for a given key.  Important thing is that NULL is not allowed in the map.  Object must be compatible with the elements in the map. 5
  • 6. HashMap Using HashMap  The following code fragment creates a HashMap and inserts three entries into it. Each entry is a key/value pair consisting of a name and a telephone number. HashMap<String, String> phoneBook = new HashMap<String, String>(); phoneBook.put(“Muhammad Safyan", “(300) 9999 999"); phoneBook.put(“Atif Ishaq", "(333) 8888 888"); phoneBook.put(“Muhammad Asif", "(321) 7777 777");  The following code will find the phone number for Atif Ishaq and print it. String number = phoneBook.get(“Atif Ishaq"); System.out.println(number); 6
  • 7. HashMap Address Book Example import java.util.HashMap; public class PhoneBook { HashMap<String,String> phonebook=new HashMap<String,String>(); public void enterNumber(String name, String number){ phonebook.put(name,number); } public String lookUpNumber(String name){ String number = phonebook.get(name); return number; } } 7
  • 8. HashMap HashMapTester Class import java.util.Scanner; public class HashMapTester { public static void main(String[] args){ PhoneBook phonebook=new PhoneBook(); Scanner sc=new Scanner(System.in); String opt="Y"; do{ System.out.print("Enter Name : "); String name=sc.next(); System.out.print("Enter Number : "); String number=sc.next(); phonebook.enterNumber(name,number); System.out.print ("Do you want to add more Numbers : "); opt=sc.next(); }while(opt.equals("Y")); } } 8
  • 9. HashMap What Happens if you add an entry to map with a key that already exists in the map?  Duplicate Key values are not allowed in the HashMap.  If a Key already exists, it will replace its value with new value. phoneBook.put(“Muhammad Asif", "(321) 7777 777"); phoneBook.put(“Muhammad Asif", "(332) 6666 777");  It will replace the phone number 7777 777 with 6666 777 against the key Muhammad Asif. No New will be created in the HashMap. 9
  • 10. HashMap How a value can be removed from HashMap? public void removeEntry(String name){ if(phonebook.containsKey(name)) phonebook.remove(name); else System.out.println("Invalid Entry"); } 10
  • 11. HashMap How you can check the total number of entries in the HashMap? public int totalNumber(){ return phonebook.size(); } 11
  • 14. 14