package Collection; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; public class MapTest { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub entrySet(); } //第一种取出方式keySet(); public static void keySet(){ HashMap<Students,String> hm = new HashMap<Students,String>(); hm.put(new Students("lisi1",21), "beijing"); hm.put(new Students("lisi1",21), "tianjing"); hm.put(new Students("lisi4",33), "nanjing"); hm.put(new Students("lisi8",44), "ganzhou"); hm.put(new Students("lisi5",80), "hengyang"); Set<Students> keyset =hm.keySet(); for(Iterator<Students> it = keyset.iterator();it.hasNext();){ Students key =it.next(); String addrv =hm.get(key); System.out.println(key+":::"+addrv); } } public static void entrySet(){ HashMap<Students,String> hm = new HashMap<Students,String>(); hm.put(new Students("lisi1",21), "beijing"); hm.put(new Students("lisi1",21), "tianjing"); hm.put(new Students("lisi4",33), "nanjing"); hm.put(new Students("lisi8",44), "ganzhou"); hm.put(new Students("lisi5",80), "hengyang"); Set<Map.Entry<Students, String>> entryset = hm.entrySet(); for(Iterator<Map.Entry<Students, String>> it =entryset.iterator();it.hasNext();){ Map.Entry<Students, String> entry = it.next(); Students s = entry.getKey(); String addr = entry.getValue(); System.out.println(s+":::"+addr); } } } |