HashMap效率高的遍历 要用就用这两种了其他的用了浪费时间反正迟早还是要用这两种的

public static void testErgodicMethod(HashMap<String,String> map){
    for (Map.Entry<String, String> entry : map.entrySet()){
        String key = entry.getKey();
        String value = entry.getValue();
        System.out.println("key: "+key+" value: "+value);
    }
}
    public static void testErgodicMethod1(HashMap<String,String> map){
        //结合迭代器
        Iterator it = map.entrySet().iterator();
        while (it.hasNext()){
           Map.Entry<String,String> entry = (Map.Entry<String, String>) it.next();
            String key = entry.getKey();
            String value = entry.getValue();
            System.out.println("key: "+key+" value: "+value);
        }
    }

猜你喜欢

转载自blog.csdn.net/qq_38983577/article/details/81779720