1

I have problems with import Map.Entry. Even though I have import import java.util.Map.Entry - there is an error : "The import java.util.Map.Entry cannot be resolved". And entrySet() method doesnot work. what is the problem? (I use jre8)

import java.io.File;
import java.util.LinkedHashMap;
import java.util.Map;

import java.util.Set;

import org.biojava3.core.sequence.ProteinSequence;
import org.biojava3.core.sequence.io.FastaReaderHelper;




public class Main {

/**
 * @param args
 */
public static void main(String[] args) throws Exception {
    LinkedHashMap<String, ProteinSequence> a = FastaReaderHelper.readFastaProteinSequence(new File("A2RTH4.fasta"));

    for (Map.Entry<String, ProteinSequence> entry : a.entrySet(); //entrySet A map entry (key-value pair). 
    {
        System.out.println( entry.getValue().getOriginalHeader() + "=" + entry.getValue().getSequenceAsString() );
    }
    }

}
Ewa Kania
  • 41
  • 1
  • 2
  • 8

3 Answers3

5
for(Map.Entry<String, Integer> entry : someMap.entrySet())

works with import java.util.Map import alone.

Nikhil Talreja
  • 2,754
  • 1
  • 14
  • 20
0

import java.util.Map is enough

Try this code, should be work:

public static void main(String[] args) throws Exception {
LinkedHashMap<String, ProteinSequence> a = FastaReaderHelper.readFastaProteinSequence(new File("A2RTH4.fasta"));
Set entrySet = a.entrySet();
Iterator it = entrySet.iterator();
// Iterate through LinkedHashMap entries
System.out.println("LinkedHashMap entries : ");
while(it.hasNext())
  System.out.println(it.next());
  }
}

Instead of foreach, try an Iterator to iterate over a LinkedHashMap data structure.

user840718
  • 1,563
  • 6
  • 29
  • 54
  • 1
    even if i have import java.util.Map i have problem with my code line for (Map.Entry entry : a.entrySet(); eclipse says : reference to the missing element 'Map.Entry' – Ewa Kania Jun 24 '14 at 11:13
  • Maybe you have to set Eclipse correctly, but posting code will be the better solution for help. – user840718 Jun 24 '14 at 11:16
  • There is a syntax error, close parenthesis out the for cycle. – user840718 Jun 24 '14 at 11:34
  • thats true, but it s not the main problem. still it s wrong something with the reference to Map.Entry – Ewa Kania Jun 24 '14 at 11:38
0
for (Map.Entry<String, ProteinSequence> entry : a.entrySet();

should be

for (Map.Entry<String, ProteinSequence> entry : a.entrySet())

(closing parenthesis instead of semicolon).

Ian Roberts
  • 120,891
  • 16
  • 170
  • 183