在类型为<string,integer>的java中使用getValue方法进行映射

时间:2017-01-24 19:23:33

标签: java maps typecasting-operator getvalue

执行以下部分时,getValue()方法中会出现错误。我尝试在getValue()内传递s作为参数。在那里也不起作用。

//Mymap<String,Integer>()       
while(in.hasNext()){
    //in is a scanner object
    String s = in.next();
    // Write code here
    //s is a string to be searched
    if (Mymap.containsKey(s)) {
        //the value corresponding to s is to be retrieved
        Integer i= (Integer)Mymap.getValue();
        System.out.println(i);
        System.out.println(s+"="+Mymap.get(s));
    } else {
        System.out.println("Not found");
    }
}

1 个答案:

答案 0 :(得分:0)

好的,正如其他人所指出的,方法getValue() 不存在,因此编译错误。

Here is the list HashMap类的所有方法。

如果您想要与密钥s对应的整数值并将其存储在i中,则应调用Mymap.get(s) ...就像您在行中所做的那样:

System.out.println(s+"="+Mymap.get(s));