HashMap的四种遍历方式
package com.xt.map; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import java.util.Set; public class HashMapTest { public static void main(String[] args) { Map<Integer,Student> students=new HashMap<>(); students.put(1, new Student("111")); Set<Integer> Keys=students.keySet(); //1 for (Integer key : Keys) { System.out.println(students.get(key)); } //2 Iterator<Integer> iterator=Keys.iterator(); while(iterator.hasNext()) { System.out.println(students.get(iterator.next())); } Set<Entry<Integer,Student>> entrys = students.entrySet(); //3 for (Entry<Integer, Student> entry : entrys) { System.out.println(entry.getValue()); } //4 Iterator<Entry<Integer, Student>> iterator1=entrys.iterator(); while(iterator1.hasNext()) { System.out.println(iterator1.next().getValue()); } } }