Map集合的遍历

来源:互联网 发布:诺瓦刷屏软件 编辑:程序博客网 时间:2024/05/20 03:41

集合的遍历方法有三种:普通for循环方法,加强for循环方法,迭代器方法

我使用了迭代器遍历的方法:

代码举例:

import java.util.*;public class Demo2{public static void main(String[] args){Map<Integer,String> map = new HashMap<Integer,String>();map.put(1123,"胡瑶");map.put(1124,"李四");map.put(1125,"王五");map.put(1126,"文娜");//先获得所有的Key,Key是一个Set集合Set<Integer> ks = map.KeySet();//迭代Setfor(int k:ks){String v = map.get(k);System.out.println(k+" "+v);}}}


原创粉丝点击