我有一个Map,该地图将registrationID作为键,并将整个filePath用作该ID的值存储的信息。 我想基于一些条件检查来动态更改该路径的值。
void getDetails(final Map<String, String> detailsMap){
String filePath = new File(details.entrySet().iterator().next().getValue()).getParent();
// key: 101A90Q value : C:\Users\xyy\registeredDetails\101A90QInfo\101A90QInfo.xlsx
//sometimes the value of the detailsMap is dynamically changed (only the path, filename remains same)
//logic to get the dynamic path
if(someCondCheck)
filePath = "c:\users\xyy\registeredDetails\conference"; //new path, but the filename i need to take from the old filePath value mentioned above. (101A90QInfo.xlsx)
}
//i want to update the map (detailsMap) with the above mentioned filePath along with the filename
showRegisteredCompleteInfo(detailsMap);
}
我想使用已更新的文件路径以及文件名来更新值为C:\Users\xyy\registeredDetails\101A90QInfo\101A90QInfo.xlsx
的detailsMap
最初提到为c:\users\xyy\registeredDetails\conference\101A90QInfo.xlsx
。
我可以在不重复进行的情况下更新Map的值吗?请指教。
答案 0 :(得分:0)
您将使用
获得集合中的第一个Entry
String filePath = new File(details.entrySet().iterator().next().getValue()).getParent();
您可以将Entry
保留在内存中,以便以后可以获取key
。
Entry<String, String> e = details.entrySet().iterator().next();
String filePath = new File(e.getValue()).getParent();
...
e.setValue(filePath);
如Entry javadoc所述:
用指定的值替换与此条目对应的值(可选操作)。 (写到地图。)如果已经从映射中删除了映射(通过迭代器的remove操作),则此调用的行为是不确定的。
或者直接在地图上直接使用按键:
details.put(e.getKey(), filePath);
您将获得entrySet中的第一项,因此无法确定它的确切位置。如果要迭代该集合,则需要更新代码以使用循环读取它:
for(Entry<String, String> e : details.entrySet()){
...
}