Java 无法使用Map调用方法<;字符串,字符串>;哪个映射作为输入参数?
我在一个类中有两个HashMap,称为pathMap和fileMap,我需要从中提取某些值。这很容易 如果我有两种不同的方法,但我希望可以将它们结合起来,但以下方法不起作用:Java 无法使用Map调用方法<;字符串,字符串>;哪个映射作为输入参数?,java,android,Java,Android,我在一个类中有两个HashMap,称为pathMap和fileMap,我需要从中提取某些值。这很容易 如果我有两种不同的方法,但我希望可以将它们结合起来,但以下方法不起作用: Map<String, String> pathMap = new LinkedHashMap<>(); thisPath = thisMap.getSavedValues(item, pathMap); Map<String, String> fileMap = new Li
Map<String, String> pathMap = new LinkedHashMap<>();
thisPath = thisMap.getSavedValues(item, pathMap);
Map<String, String> fileMap = new LinkedHashMap<>();
thisFile = thisMap.getSavedValues(item, fileMap);
....
public String getSavedValues(String key, Map<String, String> whichMap){
Set<Map.Entry<String, String>> set = whichMap.entrySet();
String hashKey = "", hashValue = "";
for (Map.Entry<String, String> check : set) { //--- Iterate complete HashMap here --
hashKey = check.getKey();
hashValue = check.getValue();
if (hashKey.equals(key)) {
break;
}
}
return hashValue;
}
Map pathMap=newlinkedhashmap();
thisPath=thisMap.GetSavedValue(项,路径映射);
Map fileMap=newlinkedhashmap();
thisFile=thisMap.GetSavedValue(项,文件映射);
....
公共字符串getSavedValues(字符串键、映射映射){
Set Set=whichMap.entrySet();
字符串hashKey=“”,hashValue=“”;
对于(Map.Entry check:set){/--在此处迭代完整的HashMap--
hashKey=check.getKey();
hashValue=check.getValue();
if(hashKey.equals(key)){
打破
}
}
返回哈希值;
}
有人能帮忙吗?更具体地说,这是可行的:
thisPath = thisMap.getPath(item);
....
public String getPath(String key){
Set<Map.Entry<String, String>> set = pathMap.entrySet();
String hashKey = "", hashValue = "";
for (Map.Entry<String, String> check : set) { //--- Iterate complete HashMap here --
hashKey = check.getKey();
hashValue = check.getValue();
if (hashKey.equals(key)) {
break;
}
}
return hashValue;
}
thisFile = thisMap.getFile(item);
....
public String getFile(String key){
Set<Map.Entry<String, String>> set = fileMap.entrySet();
String hashKey = "", hashValue = "";
for (Map.Entry<String, String> check : set) { //--- Iterate complete HashMap here --
hashKey = check.getKey();
hashValue = check.getValue();
if (hashKey.equals(key)) {
break;
}
}
return hashValue;
}
thisPath=thisMap.getPath(项目);
....
公共字符串getPath(字符串键){
Set=pathMap.entrySet();
字符串hashKey=“”,hashValue=“”;
对于(Map.Entry check:set){/--在此处迭代完整的HashMap--
hashKey=check.getKey();
hashValue=check.getValue();
if(hashKey.equals(key)){
打破
}
}
返回哈希值;
}
thisFile=thisMap.getFile(项目);
....
公共字符串getFile(字符串键){
Set=fileMap.entrySet();
字符串hashKey=“”,hashValue=“”;
对于(Map.Entry check:set){/--在此处迭代完整的HashMap--
hashKey=check.getKey();
hashValue=check.getValue();
if(hashKey.equals(key)){
打破
}
}
返回哈希值;
}
我只是想把这两种方法结合在一起。什么是
thisMap
?它是实现getSavedValues
的类的对象吗?是thisFile
和thisPath
字符串吗?定义“不工作”确定此映射是类引用和GetSavedValue在其中实现;是的,它们是字符串返回。我的意思是“不起作用”,即不返回任何内容,而使用两个单独的方法,我会收到正确的字符串结果。但是在您发布的代码片段中,您正在迭代两个空LinkedHashMapNo,这两个HashMap都是在类的前面声明和填充的。现在,我已经添加了返回上述正确结果的代码。