java.util.Map.Entry cannot be resolved error

2.4k Views Asked by At

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

  • 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!!

1

There are 1 best solutions below

3
Nitin Bisht On

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. So we will be accessed it by Map.Entry name. It provides methods to get key and value.