package collection_Codes;
import java.util.HashMap;
public class Hashmap_Iterate2 {
public static void main(String[] args)
{
HashMap<String,String> m = new HashMap<String,String>();
m.put("name", "Amit");
m.put("college","NIT");
m.put("profession","SE");
for(String s : m.keySet())
{
String val = m.get(s);
System.out.println(s +" "+val);
}
}
}
import java.util.HashMap;
public class Hashmap_Iterate2 {
public static void main(String[] args)
{
HashMap<String,String> m = new HashMap<String,String>();
m.put("name", "Amit");
m.put("college","NIT");
m.put("profession","SE");
for(String s : m.keySet())
{
String val = m.get(s);
System.out.println(s +" "+val);
}
}
}
========================================================================
package collection_Codes;
//Interface Entry is present inside MAP Interface. Entry has 3 methods:
// 1) Object getKey();
// 2) Object getValue();
// 3) Object setValue(Object );
//**********************************
import java.util.HashMap;
import java.util.Map;
public class HashMap_Iterate1 {
public static void main(String[] args)
{
HashMap<String,String> m = new HashMap<String,String>();
m.put("name", "Amit");
m.put("college","NIT");
m.put("profession","SE");
for(Map.Entry<String,String> entry : m.entrySet())
{
System.out.println(entry.getKey()+" = "+entry.getValue());
}
}
}
===================================================================
package ERRORS;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class Test {
public static void main(String[] args)
{
Map<String,String> m = new HashMap<>();
m.put("game", "FOOTBALL");
m.put("food", "Haldiram Full Thali");
m.put("hobby", "WINNING");
m.put("place", "America");
m.put("bd", "7 AUGUST");
// Now Iterate
//********************************* Method 1
for(String s1 : m.keySet())
{
System.out.println(s1 +" "+m.get(s1));
}
//********************************* Method 2
Set <Entry<String , String>> entry = m.entrySet();
for(Entry<String, String> s1 : entry)
{
System.out.println(s1.getKey()+" ~ "+s1.getValue());
}
}
}
package ERRORS;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class Test {
public static void main(String[] args)
{
Map<String,String> m = new HashMap<>();
m.put("game", "FOOTBALL");
m.put("food", "Haldiram Full Thali");
m.put("hobby", "WINNING");
m.put("place", "America");
m.put("bd", "7 AUGUST");
// Now Iterate
//********************************* Method 1
for(String s1 : m.keySet())
{
System.out.println(s1 +" "+m.get(s1));
}
//********************************* Method 2
Set <Entry<String , String>> entry = m.entrySet();
for(Entry<String, String> s1 : entry)
{
System.out.println(s1.getKey()+" ~ "+s1.getValue());
}
}
}
========================================================================
No comments:
Post a Comment