繁体   English   中英

哈希表列表上的迭代

[英]iteration over hashmap list

我想遍历HashMap列表并检索键和值(值1和value2)。 这行有一条错误,提示“类型不匹配:无法从元素类型Object转换为Map.Entry>”

for (Map.Entry<String, List<String>> 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();
    }

Map.entrySet()更改为PropertyMap.entrySet()conceptMap.entrySet()

Map接口声明的Map.entrySet()方法返回地图的集合视图(返回Set )。 每个set元素都是一个Map.Entry对象。 获取对地图条目的引用的唯一方法是从此collection-view的迭代器中进行。

如果要返回插入地图的Set ,则必须在放置它的Collection上调用它:

PropertyMap.entrySet()conceptMap.entrySet()将返回Set。

Map.entrySet()不在任何实例化的Maps上调用该方法。

Map.entrySet()返回地图的集合视图。将其更改为conceptMap.entrySet()或propertyMap.entrySet

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM