我在导入Map.Entry时遇到问题。即使我有import import java.util.Map.Entry - 也有一个错误:“导入java.util.Map.Entry无法解析”。并且entrySet()方法不起作用。问题是什么? (我用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() );
}
}
}
答案 0 :(得分:2)
for(Map.Entry<String, Integer> entry : someMap.entrySet())
仅适用于import java.util.Map
导入。
答案 1 :(得分:0)
import java.util.Map
就足够了
试试这段代码,应该可以工作:
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());
}
}
而不是foreach,尝试迭代迭代LinkedHashMap数据结构。
答案 2 :(得分:0)
for (Map.Entry<String, ProteinSequence> entry : a.entrySet();
应该是
for (Map.Entry<String, ProteinSequence> entry : a.entrySet())
(右括号而不是分号)。