Iterating over Map objects in java

public void mymap(){
Map<Integer,String> map = new HashMap<Integer,String>();
map.put(1, "rose");
map.put(2, "lily");
map.put( 3, "Peony");
map.put(4, "Chinese rose");
//The entrySet method of map traverses, the returned Set
Set<Entry<Integer,String>> set = map.entrySet();
for(Entry< Integer,String> en:set){
Integer in = en.getKey();
String st = en.getValue();
System.out.println(in+"====="+st);
}
//Using map .entrySet().iterator() traverses and returns an Iterator object
Iterator<Entry<Integer, String>> iter = map.entrySet().iterator();
while(iter.hasNext()){
Entry<Integer,String > ent = iter.next();
Integer key = ent.getKey();
String value = ent.getValue();
System.out.println(key+"=========="+value);
}
//Only get the key of the map and call the keySet() of the map
for(Integer in:map.keySet()){
System.out.println(in);
}
//Get the value of the map and call the values() of the map
for(String st:map.values()){
System.out.println(st);
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325157799&siteId=291194637