The Map interface in java

Map interface

Maps store elements in the form of key and value pairs. If the key is provided then its corresponding value can be obtained. Of course, the Map interface maps unique keys to values. It has some classes.

1. HashMap class

HashMap is a collection that stores elements in the form of key-value pairs. Keys should be unique. This means we cannot use duplicate data for keys in the HashMap. The HashMap class uses a hashtable to implement the Map interface.

We can write HashMap class as:

class HashMap<k,v>

We can create object of the HashMap as:

HashMap<String, Integer> hm = new HashMap<String, Integer>();

HashMap class methods

value put(key, value)
This method stores the key-value pair into it.

value get(Object key)
It returns corresponding value when key is given. If the key does not have a value associate with it, then it returns null.

Set keySet()
It returns a set view of the keys contained in the map.

Collection values()
It returns a collection view of the values contained in the map.

Object remove(Object key)
It removes the mapping for this key from this map if present.

int size()
It returns the number of key-value mappings in the map.

void clear()
It removes all mappings from the map.

boolean isEmpty()
It returns true if this map contains no key-value mappings.

Write a program that shows the use of HashMap class
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class test
{
public static void main(String args[]) throws IOException
{
HashMap<String, Long>hm = new HashMap<String, Long>();
String name, str;
Long phno;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while(true)
{
System.out.println(“1.Enter phone no entries”);
System.out.println(“2.Lookup in the book”);
System.out.println(“3.Display name in the book”);
System.out.println(“4.Exit”);
System.out.println(“Enter your choice”);
int n=Integer.parseInt(br.readLine());
switch(n)
{
case 1:
System.out.println(“Enter name “);
name=br.readLine();
System.out.println(“Enter phone no”);
str=br.readLine();
hm.put(name, phno);
break;
case 2:
System.out.println(“Enter name “);
name=br.readLine();
name=name.trim();
phno=hm.get(name);
System.out.println(“Phno “+phno);
break;
case 3:
Set set=new HashSet();
set=hm.keySet();
System.out.println(set);
break;
case 4:
return;
}
}
}
}

Output:
1.Enter phone no entries
2.Lookup in the book
3.Display name in the book
4.Exit
Enter your choice
1
Enter name
DAVID
Enter phone no
9999999999
1.Enter phone no entries
2.Lookup in the book
3.Display name in the book
4.Exit
Enter your choice
1
Enter name
MAT
Enter phone no
8888888888
1.Enter phone no entries
2.Lookup in the book
3.Display name in the book
4.Exit
Enter your choice
3
[DAVID, MAT] 1.Enter phone no entries
2.Lookup in the book
3.Display name in the book
4.Exit
Enter your choice
2
Enter name
MAT
Phno
8888888888
1.Enter phone no entries
2.Lookup in the book
3.Display name in the book
4.Exit
Enter your choice
4

2.Hashtable class

It is similat to HashMap which can store elements in the form of key-value pairs. But Hashtable is synchronized assuring proper results even if multiple threads act on it simultaneously.

We can write Hashtable class as:

class Hashtable<k,v>

We can create object of Hashtable as:

Hashtable<String, Integer> hm = new Hashtable<String, Integer>();

Hashtable class methods

void clear()
It resets and empties the hash table.

value put(key, value)
It inserts a key and a value into the hash table. It returns null if the key isn’t already in the hash table and returns the previous value associated with the key if the key is already in the hash table.

Object get(Object key)
It returns the object that contains the value associated with the key. If the key is not in the hash table, a null object is returned.

Set keySet()
It returns a set view of the keys contained in the Hashtable.

Collection<v> values()
This method, when applied on a Hashtable object returns all the values of the Hashtable into a Collection object.

value remove(Object key)
It removes the key and its value and returns the value associated with the key. If the key is not in the hash table, a null object is returned.

boolean isEmpty( )
It returns true if the hash table is empty; It returns false if it contains at least one key.

int size( )
It returns the number of entries in the hash table.

Write a program that shows the use of Hashtable class
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
class test
{
public static void main(String args[]) throws IOException
{
Hashtable<String, Integer> ht=new Hashtable<String, Integer>();
ht.put(“David”,1);
ht.put(“Mat”,2);
ht.put(“Steve”,3);
ht.put(“Chris”,4);
System.out.println(“The name is “);
Enumeration e=ht.keys();
while(e.hasMoreElements())
System.out.println(e.nextElement());
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println(“Enter name “);
String name=br.readLine();
name=name.trim();
Integer number=ht.get(name);
if(number!=null)
{
int num=number.intValue();
System.out.println(name+” Number:”+num);
}
else
System.out.println(“Name not found”);
}
}

Output:
The name is
Mat
Steve
Chris
David
Enter name
David
David Number:1

3.TreeMap class

It is an implementation of SortedMap. It is using a balanced binary tree that keeps its elements ordered by key. It is very useful for ordered data set. Searching is done through key.

We can write TreeMap class as:

class TreeMap<k,v>

We can create object of TreeMap as:

TreeMap<Integer,String> hm=new TreeMap<Integer,String>();

TreeMap class method

void clear()
It removes all mappings from this TreeMap.

boolean containsKey(Object key)
It returns true if this map contains a mapping for the specified key.

Set entrySet()
It returns a set view of the mappings contained in the map.

Object get(Object key)
It returns the value to which this map maps the specified key.

Set keySet()
It returns a Set view of the keys contained in this map.

Object put(key, value)
It associates the specified value with the specified key in the map.

Object remove(Object key)
It removes the mapping for this key from this TreeMap if present.

int size()
It returns the number of key-value mappings in this map.

Write a program that shows the use of TreeMap class
import java.util.*;
class test
{
public static void main(String args[])
{
TreeMap<String,Integer> tm=new TreeMap<String, Integer>();
tm.put(“Adam”,1);
tm.put(“Raman”,2);
tm.put(“Vics”,3);
tm.put(“Rayan”,4);
for(Map.Entry m:tm.entrySet())
{
System.out.println(m.getKey()+” “+m.getValue());
}
}
}

Output:
Adam 1
Raman 2
Rayan 4
Vics 3

Leave a Reply

Your email address will not be published. Required fields are marked *