我正在尝试为我作为输入的每个文档创建一个新的 HashMap
。在伪代码中,我可以想到类似的东西:
For(eachInputDoc)
{
Map<String, String> mapInputNumber = new HashMap<String, String>;
}
因此对于 4 个文档,您将拥有:
mapInput1
mapInput2
mapInput3
mapInput4
我怎样才能做到这一点?
最佳答案
看起来您正在尝试动态声明变量。你不能在 Java 中这样做——变量本身是在编译时确定的。但是,您可以创建一个列表:
List<Map<String, String>> maps = new ArrayList<Map<String, String>>();
for (Document doc : docs)
{
Map<String, String> map = new HashMap<String, String>();
// Populate map from doc
maps.add(map);
}
关于java - 在 Java 中递归创建 Hashmap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7287949/