Several traversal methods of List and Map collections

List collection traversal

One, traverse through the Itetator interface:

    Itetator is mainly used to traverse the elements in the Collection, it is also called iterator
instance:

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);//打印输出
        }
    }
}

When using the iterator to iterate the elements of the collection, if you call the remove() method of the collection object to delete the elements, an exception will occur, as in the following example:

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);//打印输出
        }

    }
}

Operation result: Insert picture description here
How to solve this problem, we only need to call remove() of the iterator. Replace the above code arrayList .remove(next); with iterator.remove(); to run normally.

Two, foreach loop traversal

    The foreach loop provided by jdk1.5 is a more concise for loop, also known as an enhanced for loop.

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);
        }
    }
}

Traversal of Map collection

method one

    First traverse all the keys, and then get the value according to the key.
Example:

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);
        }
    }
}

Way two

There is a values() method provided in the map collection, through which the Collertion collection of all the values ​​stored in the map can be directly obtained.
Examples:

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);
        }
    }
}

Way three

    Realize the traversal of the map collection through the for loop

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);
       }
    }
}

Come on! ! !

Guess you like

Origin blog.csdn.net/qq_42494654/article/details/109609787