package collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class AddRetUsingMap {
Map<Integer,String> m=new HashMap<Integer,String>();
public void addData()
{
m.put(1,"Abhishek");
m.put(2,"Abhinav");
m.put(3,"Alok");
m.put(4,"Abhijeet");
}
public void retData()
{
//retrieve data using for-each loop
/*for(Map.Entry<Integer,String> entry:m.entrySet() )
{
System.out.println("RollNo["+entry.getKey()+"],Name["+entry.getValue()+"]");
}*/
//retrieve data using Iterator
Iterator it=m.entrySet().iterator();
while(it.hasNext())
{
Map.Entry<Integer, String> me=(Map.Entry)it.next();
System.out.println(me.getKey()+ " "+me.getValue());
}
}
public static void main(String[] args) {
AddRetUsingMap am=new AddRetUsingMap();
//call method to add the data into hashmap
am.addData();
//call method to display the stored data
System.out.println("List of Roll Numbers and Students: ");
am.retData();
}
}
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class AddRetUsingMap {
Map<Integer,String> m=new HashMap<Integer,String>();
public void addData()
{
m.put(1,"Abhishek");
m.put(2,"Abhinav");
m.put(3,"Alok");
m.put(4,"Abhijeet");
}
public void retData()
{
//retrieve data using for-each loop
/*for(Map.Entry<Integer,String> entry:m.entrySet() )
{
System.out.println("RollNo["+entry.getKey()+"],Name["+entry.getValue()+"]");
}*/
//retrieve data using Iterator
Iterator it=m.entrySet().iterator();
while(it.hasNext())
{
Map.Entry<Integer, String> me=(Map.Entry)it.next();
System.out.println(me.getKey()+ " "+me.getValue());
}
}
public static void main(String[] args) {
AddRetUsingMap am=new AddRetUsingMap();
//call method to add the data into hashmap
am.addData();
//call method to display the stored data
System.out.println("List of Roll Numbers and Students: ");
am.retData();
}
}
0 comments:
Post a Comment