Map Interface
Map Interface
__________________________________________________________________
Map interface is same like as collection but not part of Collection and which is used for store data in the
form of key and value pair key cannot be duplicated but value may be duplicated using key we can store
data in map , retrieve data from map as well as delete data from map etc
Map is combination of list and set collection set maintain the key data and list maintain value and
normally map recommended when we want to maintain the uniqueness in duplicated data.
Example of Map
If we think about above hierarchy we have Map interface and Map interface provide some inbuilt
method to us for store data as well as retrieve data as well as perform different operation on Map
interface.
V remove(java.lang.Object key): this method can remove data from map using its key and if key not
found return null
Note:Normally when we want to remove data from map first ensure data present in map by using get()
methods
boolean containsKey(Object key): this method is used for search key in map if key found return true
otherwise return false.
boolean containsValue(Object value): this method check value present in map or not if present return
true otherwise return false.
Set keySet(): this method can fetch all keys from map and store in Set collection.
Note: we can fetch this method for fetch all data from Map Collection
Collection values(): this method is used for fetch all values from map Collection.
public Set<Map.Entry> entrySet(): this method can fetch all keys and value from map by using
Map.Entry interface.
So Map.Entry interface provide some inbuilt method to us
Object getKey(): this method is used for return key from map.
Object getValue(): this method is used for return value from map.
Constructor of LinkedHashMap
_______________________________________________________________________________
LinkedHashMap(): create LinkedHashMap with initial capacity 16 and load factory 0.75
LinkedHashMap(int intialCapacity): this constructor create LinkedHashMap with initial capacity 16.
LinkedHashMap(int initialCapacity,float loadfactor): this constructor create LinkedHashMap with
initialcapacity as per user choid and loadfactor as per user choice.
Note: if we think about above code we get key sequence as per the user sequence.
TreeMap(Comparator): this consturctor can arrange key in asending order when we have user defined
class object as key
Suppose consider we have Product list with its Feature list and we want to store product info in
TreeMap and some time we required to sort feature of product by product id or some time want to sort
feature of by product name.
So we want to perform this task by using TreeMap
package org.techhub.map;
}
package org.techhub.map;
import java.util.Comparator;
@Override
public int compare(Product o1, Product o2) {
if(o1.getId()>o2.getId()) {
return 1;
}
else if(o1.getId()<o2.getId()) {
return -1;
}
else {
return 0;
}
}
package org.techhub.map;
import java.util.*;
public class TreeMapApplication {
}
}
Example: Write a Program to Create Student Record using map collection so here rollno consider as key
and name consider value and perform following task on map.
import java.util.*;
public class MapOperationApp
{
public static void main(String x[])
{
LinkedHashMap<Integer,String> map = new LinkedHashMap<Integer,String>();
do{
Scanner xyz = new Scanner(System.in);
System.out.println("1:Insert Record in Map");
System.out.println("2:View All Record");
System.out.println("3:Delete record by using key");
System.out.println("4:Search Record by using map");
int choice;
System.out.println("Enter your choice");
choice=xyz.nextInt();
switch(choice)
{
case 1:
System.out.println("Enter roll number of student");
int roll=xyz.nextInt();
xyz.nextLine();
boolean b=map.containsKey(roll);
if(b){
System.out.println("Roll no already present");
}
else{
System.out.println("Enter name of student");
String name=xyz.nextLine();
map.put(roll,name);
System.out.println("New Student Record Added Successfully....");
}
break;
case 2:
Set<Map.Entry<Integer,String>> set=map.entrySet();
System.out.println("Student Id \t Student Name");
for(Map.Entry m:set)
{
System.out.println(m.getKey()+"\t\t"+m.getValue());
}
break;
case 3:
System.out.println("enter roll no for delete");
int rollno=xyz.nextInt();
b=map.containsKey(rollno);
if(b)
{ String s=map.remove(rollno);
System.out.println("Deleted record is "+s);
}
else{
System.out.println("Key not found");
}
break;
case 4:
xyz.nextLine();
System.out.println("Search student record by name");
String sname=xyz.nextLine();
b=map.containsValue(sname);
if(b)
{ System.out.println("Student record found");
}
else{
System.out.println("Student record not found");
}
break;
default:
System.out.println("Wrong choice");
}
}while(true);
}
}
Example: WAP to create array of size 10 and find the occurrence of every number
package org.techhub.map;
import java.util.*;
public class OccurenceOfNumberByMap {
public static void main(String[] args) {
int a[]=new int[10];
Scanner xyz = new Scanner(System.in);
System.out.println("Enter number ");
for(int i=0;i<a.length;i++)
{ a[i]=xyz.nextInt();
}
System.out.println("display array");
for(int i=0;i<a.length;i++) {
System.out.print(a[i]+"\t");
}
System.out.println();
LinkedHashMap<Integer,Integer> map = new LinkedHashMap<Integer,Integer>();
for(int i=0;i<a.length;i++) {
Integer count=map.get(a[i]);
if(count==null) {
count=new Integer(0);
}
++count;
map.put(a[i], count);
}
System.out.println("Display all values from map");
Set<Map.Entry<Integer, Integer>> set=map.entrySet();
for(Map.Entry<Integer, Integer> m:set) {
System.out.println(m.getKey()+"\t"+m.getValue());
}
}
}
Example: WAP to input the string from keyboard find the repeat word from string.
package org.techhub.map;
import java.util.*;
public class FindDuplicateWordFromString {
map.put("India", indPList);
map.put("Aus", ausPList);
Set<Map.Entry<String, ArrayList<String>>> set=map.entrySet();
for(Map.Entry<String,ArrayList<String>> m:set) {
String teamName=m.getKey();
ArrayList<String> plist=m.getValue();
System.out.println(teamName);
System.out.println("===============================");
for(String pname:plist) {
System.out.println(pname);
}
System.out.println();
System.out.println();
}
}