public class MapTest {
public static void main(String[] args) {
Map<String, String> map = new HashMap<>();
map.put("A", "say one");
map.put("B", "say two");
map.put("C", "say three");
//通过得到key的值
for (String key : map.keySet()) {
System.out.println("方式一:key的值为:" + key + ",value的值为:" + map.get(key));
}
//通过迭代器
Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, String> entry = iterator.next();
System.out.println("方式二:key的值为:" + entry.getKey() + ",value的值为:" + entry.getValue());
}
//推荐,尤其是当数据量很大的时候
for (Map.Entry<String, String> entry : map.entrySet()) {
System.out.println("方式三:key的值为:" + entry.getKey() + ",value的值为:" + entry.getValue());
}
for (String value : map.values()) {
System.out.println("方式四(只能去value),value的值为:" + value);
}
}
}