//在一个循环,同时输出key和value
public static void main(String[] args) {
Map<String, String> hashMap = new HashMap<String, String>();
hashMap.put("a", "1234");
hashMap.put("m", "oi");
hashMap.put("p", "看");
hashMap.put("d", "fuck");
hashMap.put("e", "2");
hashMap.put("v", "-_-");//以上,添加元素
//将Map接口,变为Set接口
Set<Entry<String, String>> entrySet = hashMap.entrySet();
//得到Iterator
Iterator<Entry<String, String>> iterator = entrySet.iterator();
//同时输出key和value
while (iterator.hasNext()) {
//通过iterator.next(),得到key和value
Entry<String, String> next = iterator.next();
System.out.println(next.getKey() + " -> " + next.getValue());
}
Java.在一个循环,同时输出Map的key和value
最新推荐文章于 2023-11-19 19:21:53 发布