Map

/*
* 遍历Map
*/
Map<String, Person> rootMap = new LinkedHashMap<String, Person>();
way 1:
for(Iterator it = rootMap.entrySet().iterator(); it.hasNext();){
    Map.Entry e = (Map.Entry) it.next();
    str = e.getKey();
    person = e.getValue();
}
//与上面是等价的,只不过一个用for一个用while
Iterator it = rootMap.entrySet().iterator(); 
while(it.hasNext()){
    Map.Entry e = (Map.Entry) it.next();
    str = e.getKey();
    person = e.getValue();
}

way 2:
for(Map.Entry<String, Person> e : rootMap.entrySet()){
    str = e.getKey();
    person = e.getValue();
}

//---------------------------------
// 获取map中所有key
Set<String> set = map.keySet();

// 获取map中所有value
Collection<Person> c = map.values();

// 获取map中包含的映射关系的Set视图
Set<Map.Entry<String, Person>> entrySet = map.entrySet();

你可能感兴趣的:(map)