public static void main(String[] args) {
// Map<String, Object> map = new HashMap<String, Object>();
Map<String, Object> map = new LinkedHashMap<String, Object>();
map.put("username","zhaokuo");
map.put("password", "123456");
map.put("email", "zhaokuo719@163.com");
map.put("sex", "男");
//第一种 用for循环的方式
for (Map.Entry<String, Object> m :map.entrySet()) {
System.out.println(m.getKey()+"\t"+m.getValue());
}
//利用迭代 (Iterator)
Set set=map.entrySet();
Iterator iterator=set.iterator();
while(iterator.hasNext()){
Map.Entry<String, Object> enter=(Entry<String, Object>) iterator.next();
System.out.println(enter.getKey()+"\t"+enter.getValue());
}
//利用KeySet 迭代
Iterator it = map.keySet().iterator();
while(it.hasNext()){
String key;
String value;
key=it.next().toString();
value=(String) map.get(key);
System.out.println(key+"--"+value);
}
//利用EnterySet迭代
Iterator i=map.entrySet().iterator();
System.out.println( map.entrySet().size());
String key;
String value;
while(i.hasNext()){
Map.Entry entry = (Map.Entry)i.next();
key=entry.getKey().toString();
value=entry.getValue().toString();
System.out.println(key+"===="+value);
}
System.out.println(getKeyByValue(map, "zhaokuo"));
}
public static String getKeyByValue(Map map, Object value) {
String keys="";
Iterator it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Entry) it.next();
Object obj = entry.getValue();
if (obj != null && obj.equals(value)) {
keys=(String) entry.getKey();
}
}
return keys;
}