Java 无法导入 Map.Entry

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24384948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 11:53:29  来源:igfitidea点击:

Cannot import Map.Entry

javahashmap

提问by Ewa Kania

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)

我在导入 Map.Entry 时遇到问题。即使我导入了 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() );
    }
    }

}

回答by user840718

import java.util.Mapis enough

import java.util.Map足够

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.

尝试使用 Iterator 来迭代 LinkedHashMap 数据结构,而不是 foreach。

回答by Nikhil Talreja

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

works with import java.util.Mapimport alone.

import java.util.Map单独使用导入。

回答by Ian Roberts

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

should be

应该

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

(closing parenthesis instead of semicolon).

(右括号而不是分号)。