Map Interface in Java

Advertisements

Prev Tutorial Next Tutorial

Map Interface in Java

Maps are defined by the java.util.Map interface in Java. Map is an Interface which store key, value in which both key and value are objects Key must be unique and value can be duplicate. Here value are elements which store in Map. Map Interface is implemented in the following four classes.

  • HashMap
  • HashTable
  • LinkedHashMap
  • TreeMap

Create object for map Implementer classes

Syntax

Map_Implemented_class<generic_class1, generic_class2> obj=new Map_Implemented_class<generic_class1, generic_class2>();

Example to create object of HashMap

Example

HashMap<Integer, Float> obj=new HashMap<Integer, Float>();

Methods of Map Interface

public int size()

This method is used for finding the number of entries in any 2-D Collection framework variable.

public boolean isEmpty()

This method returns true provided 2-D Collection framework is empty (size=0).Returns false in the case of non-empty (size>0).

public void put(Object,Object)

This method is used for adding and modifying the entries of 2-D Collection framework variable. If the (key, value) pair is not existing than such (k,v)pair will be added to the 2-D Collection framework variable and it will be considered as Inserted Entry. In the (k,v) pair ,if the value of key is already present in 2-D collection framework variable than the existing value of value is replaced by new value of value.

Example

m.put(10,7.5f);    //Inserted entery
m.put(20,1.8f);
System.out.println(m);    //(10=7.5)    (20=1.8)
m.put(10,9.5);       //Modified entery
System.out.println(m);   //(10=9.5)   (20=1.8)

public Object get(Object obj)

This method is used for obtaining the value of value by passing value of key

Example

Object vobj1=m.get(10);
Object vobj2=m.get(100);

public set enterySet()

This method is used for extracting or retrieving all the entries of any 2-D Collection framework variable and hold them in the object of java.util.Set interface.

Example

System.out.println(m); //{(10=7.5)(20=1.9)(30=1.5)}
Set s= m.enterySet();   //s={(10=7.5)(20=1.9)(30=1.5)}
Iterator itr=s.iterator();
while(itr.hasNext())
{
Object mobj=itr.next();  //4
Map.Entery me=(Map.Entery)mobj;
Object kobj=me.getKey();
Object vobj=me.getValue();
Integer io=(Integer)kobj;
Float fo=(Float)vobj;
int acno=io.intValue();
float bal=fo.floatValue();
System.out.println(bal+"is the bal of"+acno);
}//while

public set keySet()

This method is used for obtaining the set of keys from any 2-D Collection framework variable and pass those set of keys to the get() for obtaining the values of value.

Example

System.out.println(m); //{(10=7.5)(20=1.9)(30=1.5)}
Set s= m.keySet();   //s={(10=7.5)(20=1.9)(30=1.5)}
Iterator itr=s.iterator();
while(itr.hasNext())
{
Object kobj=itr.next();  
Object vobj=me.get(kobj);
Integer io=(Integer)kobj;
Float fo=(Float)vobj;
int acno=io.intValue();
float bal=fo.floatValue();
System.out.println(bal+"is the bal of"+acno);
}//while

Note: These all above methods are applicable for all Implementer classes of Map Interface.


Prev Tutorial Next Tutorial

Google Advertisment

Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 9999595223

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

For Projects 9999595223

Google Advertisements


Buy Websites 9999595223

Buy College Projects with Documentation Contact on whatsapp 9999595223. Contact on: hitesh.xc@gmail.com or 9999595223 Try this Keyword C++ Programs

Advertisements