Java 遍历利用entry遍历Map时类型转换的问题

来源:互联网 发布:手机淘宝卖家登录首页 编辑:程序博客网 时间:2024/05/18 03:09
public class test {    public static void main(String[] args) {        Map<Integer,String> map = new HashMap<>();        for (int i=0;i<5;i++) {            map.put(i,Integer.toString(i));        }        for (Map.Entry entry:map.entrySet()) {        //      在java8中,必须将entry中得到的key和value进行转换后才能赋值给相应的类型;            int key = (int) entry.getKey();            String val = (String) entry.getValue();            dsp(key);            dsp(val);        }    }    public static void dsp(Object o) {        System.out.println(o);    }}
原创粉丝点击