Java hashmap列表上的迭代
我想迭代HashMap列表并检索键和值(值1和值2)。这行有个错误,上面写着 “类型不匹配:无法从元素类型对象转换为映射。条目>”Java hashmap列表上的迭代,java,hashmap,Java,Hashmap,我想迭代HashMap列表并检索键和值(值1和值2)。这行有个错误,上面写着 “类型不匹配:无法从元素类型对象转换为映射。条目>” for(Map.Entry:Map.entrySet()) 我做错什么了吗。请帮帮我。这是完整的代码 public static void main(String[] args) { Map<String, List<String>> conceptMap = new HashMap<String, List<Stri
for(Map.Entry:Map.entrySet())
我做错什么了吗。请帮帮我。这是完整的代码
public static void main(String[] args) {
Map<String, List<String>> conceptMap = new HashMap<String, List<String>>();
Map<String, List<String>> PropertyMap = new HashMap<String, List<String>>();
try{
Scanner scanner = new Scanner(new FileReader("C:/"));
while (scanner.hasNextLine()){
String nextLine = scanner.nextLine();
String [] column = nextLine.split(":");
if (column[0].equals ("Property")){
if (column.length == 4) {
PropertyMap.put(column [1], Arrays.asList(column[2], column[3]));
}
else {
conceptMap.put (column [1], Arrays.asList (column[2], column[3]));
}
}
for (Map.Entry<String, List<String>> entry : Map.entrySet()) {
String key = entry.getKey();
List<String> valueList = entry.getValue();
System.out.println("Key: " + key);
System.out.print("Values: ");
for (String s : valueList) {
System.out.print(s + " ");
}
}
}
scanner.close();
}
catch (Exception e) {
e.printStackTrace();
}
publicstaticvoidmain(字符串[]args){
Map conceptMap=newhashmap();
Map PropertyMap=新HashMap();
试一试{
Scanner Scanner=新扫描仪(新文件读取器(“C:/”));
while(scanner.hasNextLine()){
字符串nextLine=scanner.nextLine();
String[]column=nextLine.split(“:”);
if(列[0]。等于(“属性”)){
if(column.length==4){
PropertyMap.put(列[1],数组.asList(列[2],列[3]);
}
否则{
conceptMap.put(列[1],Arrays.asList(列[2],列[3]);
}
}
对于(Map.Entry:Map.entrySet()){
String key=entry.getKey();
List valueList=entry.getValue();
System.out.println(“Key:+Key”);
系统输出打印(“值:”);
用于(字符串s:值列表){
系统输出打印(s+“”);
}
}
}
scanner.close();
}
捕获(例外e){
e、 printStackTrace();
}
将映射.entrySet()
更改为属性映射.entrySet()
或概念映射.entrySet()
将映射.entrySet()
更改为属性映射.entrySet()
或概念映射.entrySet()
映射.entrySet()由映射接口声明的方法返回映射的集合视图(返回一个集合
)。这些集合元素中的每个元素都是一个Map.Entry
对象。获取映射项引用的唯一方法是从该集合视图的迭代器获取
如果要返回插入到地图中的集
,必须在放置该集的集合中调用它:
PropertyMap.entrySet()
和conceptMap.entrySet()
将返回集合
Map.entrySet()
没有对实例化的Map
调用方法。Map接口声明的Map.entrySet()
方法返回映射的集合视图(返回Set
)。这些集合元素中的每一个都是一个Map.Entry
对象。获取映射项引用的唯一方法是从此集合视图的迭代器
如果要返回插入到地图中的集
,必须在放置该集的集合中调用它:
PropertyMap.entrySet()
和conceptMap.entrySet()
将返回集合
Map.entrySet()
未对实例化的Map
Map.entrySet()调用方法。entrySet()返回映射的集合视图。将其更改为conceptMap.entrySet()或
propertyMap.entrySetMap.entrySet()返回映射的集合视图。将其更改为conceptMap.entrySet()或
propertyMap.entrySet有任何特定的语言吗?有任何特定的语言吗?
public static void main(String[] args) {
Map<String, List<String>> conceptMap = new HashMap<String, List<String>>();
Map<String, List<String>> PropertyMap = new HashMap<String, List<String>>();
try{
Scanner scanner = new Scanner(new FileReader("C:/"));
while (scanner.hasNextLine()){
String nextLine = scanner.nextLine();
String [] column = nextLine.split(":");
if (column[0].equals ("Property")){
if (column.length == 4) {
PropertyMap.put(column [1], Arrays.asList(column[2], column[3]));
}
else {
conceptMap.put (column [1], Arrays.asList (column[2], column[3]));
}
}
for (Map.Entry<String, List<String>> entry : Map.entrySet()) {
String key = entry.getKey();
List<String> valueList = entry.getValue();
System.out.println("Key: " + key);
System.out.print("Values: ");
for (String s : valueList) {
System.out.print(s + " ");
}
}
}
scanner.close();
}
catch (Exception e) {
e.printStackTrace();
}