You are on page 1of 14

Object Oriented Programming

Instructor Name:
Lecture-11
Today’s Lecture

 The Concept of Map

 HashMap

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
Collections

13
14

You might also like