HashMap的两种遍历方式

xiaoxiao2021-02-28  7

package hashmap遍历; import java.util.HashMap; import java.util.Map.Entry; import java.util.Set; public class Demo { public static void main(String[] args) { HashMap<String, String> hashMap = new HashMap<String, String>(); hashMap.put("a", "1"); hashMap.put("b", "2"); hashMap.put("c", "3"); hashMap.put("d", "4"); System.out.println("第一种遍历"); Set<String> keySet = hashMap.keySet(); for (String key : keySet) { String value = hashMap.get(key); System.out.println("key是" + key + "---value是" + value); } System.out.println("第二种遍历"); Set<Entry<String,String>> entrySet = hashMap.entrySet(); for (Entry<String, String> entry : entrySet) { String value = entry.getValue(); String key = entry.getKey(); System.out.println("key是" + key + "---value是" + value); } } }
转载请注明原文地址: https://www.6miu.com/read-2400034.html

最新回复(0)