简体   繁体   English

java.util.Map.Entry无法解决错误

[英]java.util.Map.Entry cannot be resolved error

I tried the following code as 我尝试了以下代码

package collectconstructor;
import java.util.Iterator;  
import java.util.Map;  
import java.util.Set;  

public class QuestionMap {

private int id;  
private String name;  
private Map<String,String> answers;  


public QuestionMap(int id, String name, Map<String, String> answers) {  
    super();  
    this.id = id;  
    this.name = name;  
    this.answers = answers;  
}  

public void displayInfo(){  
    System.out.println("question id:"+id);  
    System.out.println("question name:"+name);  
    System.out.println("Answers....");  
    Set<Entry<String, String>> set=answers.entrySet();  
    Iterator<Entry<String, String>> itr=set.iterator();  
    while(itr.hasNext()){  
        Entry entry= itr.next();  
        System.out.println("Answer:"+entry.getKey()+" Posted By:"+entry.getValue());  
    }  
}  

}

Here in the following code snippet 在下面的代码片段中

 Set<Entry<String, String>> set=answers.entrySet();  
 Iterator<Entry<String, String>> itr=set.iterator(); 

I got the error as the 我得到了错误

Multiple markers at this line 这行有多个标记

  • The method entrySet() from the type Map refers to the missing type Map$Entry Map类型的方法entrySet()引用缺少的Map $ Entry类型

  • Entry cannot be resolved to a type 条目无法解析为类型

Can anybody explain as Why I am getting this error? 有人可以解释为为什么我会收到此错误吗?

Also I tried importing 我也尝试导入

import java.util.Map.Entry;

but got same error.. please help!! 但是有同样的错误..请帮助!!

Try this: 尝试这个:

Set<Map.Entry<String, String>> set = answers.entrySet();  
Iterator<Map.Entry<String, String>> itr = set.iterator();  

Here Map.Entry is the sub interface of Map. 这里的Map.Entry是Map的子接口。 So we will be accessed it by Map.Entry name. 因此,将通过Map.Entry名称对其进行访问。 It provides methods to get key and value. 它提供了获取关键和价值的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 出现错误:无法解析类型java.util.Map $ Entry。 从要求中间接引用 - Getting error : The type java.util.Map$Entry cannot be resolved. It is indirectly referenced from required spring batch exception无法构造java.util.Map $ Entry - spring batch exception Cannot construct java.util.Map$Entry “导入java.util.function无法解析”错误 - The “import java.util.function cannot be resolved” error “ Map”无法注入“ java.util.NavigableMap”类型的属性 - Property of 'java.util.NavigableMap' type cannot be injected by 'Map' 类型java.util.optional无法解析。 它是从要求中间接引用的-代码日志 - the type java.util.optional cannot be resolved. it is indirectly referenced from required - stack overflow 在Spring中动态改变util:map的入口值 - dynamically changing entry value of util:map in Spring Spring,Java:错误,无法将java.lang.Long强制转换为java.util.Date - Spring, Java : Error, cannot cast java.lang.Long to java.util.Date Spring插入错误无法实例化[java.util.Map]:指定的类是一个接口 - Spring insert error Failed to instantiate [java.util.Map]: Specified class is an interface Resttemplate Oauth2 Spring Userinfo无法将xml转换为java.util.map - Resttemplate Oauth2 Spring Userinfo cannot convert xml to java.util.map Maven错误ContextConfigApplicationContext无法解决 - Maven error ContextConfigApplicationContext cannot be resolved
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM