import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class MapImplementation {
public MapImplementation() {
super();
}
public static void main(String args[]){
Map<Integer,String> map = new HashMap<Integer,String>();
// Addition of element in map
map.put(1,"Shekhar");
map.put(2, "Gaurva");
// One way to iterate map
Set<Integer> keySet = map.keySet();
Iterator<Integer> iterator = keySet.iterator();
while(iterator.hasNext()){
Integer key = iterator.next();
String value = map.get(key);
System.out.println("key "+key +" value "+value);
}
// Another way to iterate map
Set<Map.Entry<Integer, String>> entrySet = map.entrySet();
Iterator<Map.Entry<Integer, String>> iterator0 = entrySet.iterator();
while(iterator0.hasNext()){
Map.Entry<Integer, String> entry = iterator0.next();
Integer key = entry.getKey();
String value = entry.getValue();
System.out.println("key "+key +" value "+value);
}
}
}
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class MapImplementation {
public MapImplementation() {
super();
}
public static void main(String args[]){
Map<Integer,String> map = new HashMap<Integer,String>();
// Addition of element in map
map.put(1,"Shekhar");
map.put(2, "Gaurva");
// One way to iterate map
Set<Integer> keySet = map.keySet();
Iterator<Integer> iterator = keySet.iterator();
while(iterator.hasNext()){
Integer key = iterator.next();
String value = map.get(key);
System.out.println("key "+key +" value "+value);
}
// Another way to iterate map
Set<Map.Entry<Integer, String>> entrySet = map.entrySet();
Iterator<Map.Entry<Integer, String>> iterator0 = entrySet.iterator();
while(iterator0.hasNext()){
Map.Entry<Integer, String> entry = iterator0.next();
Integer key = entry.getKey();
String value = entry.getValue();
System.out.println("key "+key +" value "+value);
}
}
}
No comments:
Post a Comment