List和Map集合的几种遍历方式

List集合的遍历

一,通过Itetator接口遍历:

    Itetator主要用于遍历Collection中的元素,它也被称为迭代器
实例:

public class Exception01 {
    
    
    public static void main(String[] args) {
    
    
        //创建ArrayList集合
        ArrayList<String> arrayList = new ArrayList<String>();
        arrayList.add("张三");
        arrayList.add("李四");
        arrayList.add("王二");
        arrayList.add("麻子");
        Iterator<String> iterator = arrayList.iterator();//获得Iterator对象
        while(iterator.hasNext()){
    
    //判断该集合是否有下一个元素
            String next = iterator.next();//如果有取出来
            System.out.println(next);//打印输出
        }
    }
}

在使用iterator迭代集合的元素是,如果你调用了集合对象的remove()方法删除元素时,会出现异常,如下实例:

public class Exception01 {
    
    
    public static void main(String[] args) {
    
    
        //创建ArrayList集合
        ArrayList<String> arrayList = new ArrayList<String>();
        arrayList.add("张三");
        arrayList.add("李四");
        arrayList.add("王二");
        arrayList.add("麻子");
        Iterator<String> iterator = arrayList.iterator();//获得Iterator对象
        while(iterator.hasNext()){
    
    //判断该集合是否有下一个元素
            String next = iterator.next();//如果有取出来
            if ("张三".equals(next)){
    
    
                arrayList .remove(next);//此处的remove()方法是集合内部的方法
            }
            System.out.println(next);//打印输出
        }

    }
}

运行结果:在这里插入图片描述
该如何解决这个问题,我们只需要调用迭代器的remove()即可。将上面的代码arrayList .remove(next);替换成iterator.remove();即可正常运行。

二,foreach循环遍历

    jdk1.5提供的foreach循环,该循环是一个更加简洁的for循环,也称为增强for循环。

public class Exception01 {
    
    
    public static void main(String[] args) {
    
    
        //创建ArrayList集合
        ArrayList<String> arrayList = new ArrayList<String>();
        arrayList.add("张三");
        arrayList.add("李四");
        arrayList.add("王二");
        arrayList.add("麻子");
        //for(容器元素的类型 临时变量:容器变量){}
        for (String arr : arrayList) {
    
    
            System.out.println(arr);
        }
    }
}

Map集合的遍历

方式一

    先遍历所有key,再根据key取值
实例:

public class Exception05 {
    
    
    public static void main(String[] args) {
    
    
        //创建一个map
        Map hashMap = new HashMap();
        //通过put(key, value)方法存储
        hashMap.put("name1", "张一");
        hashMap.put("name2", "张二");
        hashMap.put("name3", "张三");
        hashMap.put("name4", "张四");
        Set keySet = hashMap.keySet();//获得键的集合
        Iterator iterator = keySet.iterator();//迭代集合
        while (iterator.hasNext()){
    
    //判断是否有下一个key
            Object key = iterator.next();//取出key
            Object value = hashMap.get(key);//根据key取值
            System.out.println(key +":"+ value);
        }
    }
}

方式二

map集合中提供的有一个values()方法,通过这个方法可以直接获得map中存储所有值的    Collertion集合。
实例:

public class Exception05 {
    
    
    public static void main(String[] args) {
    
    
        //创建一个map
        Map hashMap = new HashMap();
        //通过put(key, value)方法存储
        hashMap.put("name1", "张一");
        hashMap.put("name2", "张二");
        hashMap.put("name3", "张三");
        hashMap.put("name4", "张四");
        Collection values = hashMap.values();
        Iterator iterator = values.iterator();
        while (iterator.hasNext()){
    
    
            Object value = iterator.next();
            System.out.println(value);
        }
    }
}

方式三

    通过for循环实现map集合的遍历

public class Exception05 {
    
    
    public static void main(String[] args) {
    
    
        //创建一个map
        Map<String,String> hashMap = new HashMap<String,String>();
        //通过put(key, value)方法存储
        hashMap.put("name1", "张一");
        hashMap.put("name2", "张二");
        hashMap.put("name3", "张三");
        hashMap.put("name4", "张四");
       for (Map.Entry<String,String> map : hashMap.entrySet()){
    
    
           String key = map.getKey();
           String value = map.getValue();
           System.out.println(key + ":" + value);
       }
    }
}

加油吧!!!

猜你喜欢

转载自blog.csdn.net/qq_42494654/article/details/109609787