java.util.NoSuchElementException异常原因

java.util.NoSuchElementException异常原因

public class TestDemo {
    public static void main(String[] args) {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("a", "工人");
        hashMap.put("b", "老师");
        hashMap.put("c", "公务员");
        Iterator iterator = hashMap.keySet().iterator();
        while (iterator.hasNext()) {
            String key = (String) iterator.next();
            String values = (String) iterator.next();
            System.out.println("初始化后的map:");
            System.out.println(key);
            System.out.println(values);
        }
    }
}

出现异常:

Exception in thread "main" java.util.NoSuchElementException
	at java.util.HashMap$HashIterator.nextNode(HashMap.java:1431)
	at java.util.HashMap$KeyIterator.next(HashMap.java:1453)
	at com.chenheng.TestDemo.main(TestDemo.java:19)

Iterator类的next( )方法在同一循环中不能出现两次。会导致最后一次的游标指向空值

更改为下面代码:

public static void main(String[] args) {

        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("a", "工人");
        hashMap.put("b", "老师");
        hashMap.put("c", "公务员");
        Iterator<Map.Entry<String, String>> iterator = hashMap.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry entry = iterator.next();
            String key = (String) entry.getKey();
            String values = (String) entry.getValue();
            System.out.print("初始化后的map:");
            System.out.println(key + "=" + values);
        }
    }

结果为:

初始化后的map:a=工人
初始化后的map:b=老师
初始化后的map:c=公务员

另外添加遍历map集合的四种方式

package com.example.demo;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

@SpringBootTest
class DemoApplicationTests {
    public Map<String, String> getmap(){
        Map<String, String> hashMap = new HashMap<>();
        hashMap.put("a", "工人");
        hashMap.put("b", "老师");
        hashMap.put("c", "公务员");
        return hashMap;
    }
    /*
    * 第一种使用迭代器,效率高
    * */
    @Test
    void test01(){
        Map<String, String> map = getmap();
        Iterator iterator = map.entrySet().iterator();
        while(iterator.hasNext()){
            Map.Entry entry = (Map.Entry)iterator.next();
            Object key = entry.getKey();
            Object value = entry.getValue();
            System.out.println("key:"+key+",value:"+value);
        }
    }
    /*
    * 使用迭代器,效率低
    * */
    @Test
    void test02(){
        Map<String, String> map = getmap();
        Iterator<String> iterator = map.keySet().iterator();
        while (iterator.hasNext()){
            String key = iterator.next();
            String value = map.get(key);
            System.out.println("key:"+key+",value:"+value);
        }
    }
    /*
    * for each 遍历 第一种
    * */
    @Test
    void test03(){
        Map<String, String> map = getmap();
        for (String key:map.keySet()){
            System.out.println("key:"+key+",value:"+map.get(key));
        }
    }
    /*
    * for each 遍历 第二种
    * */
    @Test
    void test04(){
        Map<String, String> map = getmap();
        for (Map.Entry<String,String> entry:map.entrySet()){
            String key = entry.getKey();
            String value = entry.getValue();
            System.out.println("key:"+key+",value:"+value);
        }
    }
    @Test
    void contextLoads() {
    }

}

你可能感兴趣的:(那些年开发过程中踩过的坑,java,hashmap)