0

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

NDT
  • 13
  • 5
  • `Set> set=answers.entrySet();` should work.. – Romeo Sierra May 18 '18 at 06:43
  • I know it is a while since you asked this, but this answer might be of help https://stackoverflow.com/questions/24110620/the-type-java-util-mapentry-cannot-be-resolved-it-is-indirectly-referenced-fro – Shashank Garg Apr 11 '19 at 02:35

1 Answers1

1

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.

Nitin Bisht
  • 5,053
  • 4
  • 14
  • 26
  • It is showing the error that Map.Entry cannot be resolved to a type. – NDT May 21 '18 at 07:01
  • @NDT I have tested it in eclipse editor, It is not showing any error. Did you also change in `Map.Entry entry= itr.next();` in while loop? Let me know if it is still showing same error after all these 3 changes. – Nitin Bisht May 21 '18 at 07:11
  • Then I think problem is somewhere else, You better edit you question with complete code, then only I will be able to help you. – Nitin Bisht May 21 '18 at 09:44