package com.uns.mpos.account.test;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class MapTest {
public static void main(String[] args) {
Map<String,String> params=new HashMap<String,String>();
params.put("1", "tom");
params.put("2", "john");
params.put("3", null);
params.put("4", "lily");
int flag=3;
switch (flag) {
case 1:
//利用map.keySet()遍历
for (String key : params.keySet()) {
System.out.println("key值="+key+";value值="+params.get(key));
}
break;
case 2:
//利用map.entrySet(),加迭代器进行遍历
Iterator<Map.Entry<String,String>> it=params.entrySet().iterator();
while(it.hasNext()){
Map.Entry<String,String> entry=it.next();
System.out.println("key值="+entry.getKey()+";value值="+entry.getValue());
}
break;
case 3:
//利用map.entrySet()遍历
for (Map.Entry<String, String> entry : params.entrySet()) {
System.out.println("key值="+entry.getKey()+";value值="+entry.getValue());
}
break;
case 4:
for (String value : params.values()) {
System.out.println("value值="+value);
}
break;
default:
System.err.println("NULL");
break;
}
}
}
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class MapTest {
public static void main(String[] args) {
Map<String,String> params=new HashMap<String,String>();
params.put("1", "tom");
params.put("2", "john");
params.put("3", null);
params.put("4", "lily");
int flag=3;
switch (flag) {
case 1:
//利用map.keySet()遍历
for (String key : params.keySet()) {
System.out.println("key值="+key+";value值="+params.get(key));
}
break;
case 2:
//利用map.entrySet(),加迭代器进行遍历
Iterator<Map.Entry<String,String>> it=params.entrySet().iterator();
while(it.hasNext()){
Map.Entry<String,String> entry=it.next();
System.out.println("key值="+entry.getKey()+";value值="+entry.getValue());
}
break;
case 3:
//利用map.entrySet()遍历
for (Map.Entry<String, String> entry : params.entrySet()) {
System.out.println("key值="+entry.getKey()+";value值="+entry.getValue());
}
break;
case 4:
for (String value : params.values()) {
System.out.println("value值="+value);
}
break;
default:
System.err.println("NULL");
break;
}
}
}