作者:那一年2502931247 | 来源:互联网 | 2023-05-17 01:30
CanIaccessmynestedMapinmyiteratorwhenthenestedMapiscreatedintheput()method,likethi
Can I access my nestedMap
in my iterator when the nestedMap
is created in the put()
method, like this:
当在put()方法中创建嵌套映射时,我可以在迭代器中访问我的嵌套映射,如下所示:
@Override
public String put(final String row, final String column, final String value) {
/**
* Second map which is contained by centralMap, that contain Strings as
* Keys and Values.
*/
Map nestedMap;
if (centralMap.containsKey(row))
nestedMap = centralMap.get(row);
else
nestedMap = new HashMap();
if (!nestedMap.containsKey(column))
counter++;
centralMap.put(row, nestedMap);
return nestedMap.put(column, value);
}
and the centralMap is declared as an Object-Variable,
并且centralMap被声明为对象变量,
private final Map> centralMap;
but instantiated just in the constructor, like this:
但只是在构造函数中实例化,如下所示:
centralMap = new HashMap>();
the method i'm trying to implement is the remove method:
我试图实现的方法是remove方法:
@Override
public void remove() {
for (Map map : centralMap.values()) {
map = centralMap.get(keyName);
iteratorNested.remove();
if (map.size() <= 0)
iteratorCentral.remove();
}
}
Thanks a lot!
非常感谢!
1 个解决方案
0
Not sure what you're asking exactly, but I think this is a little easier to read:
不确定你究竟在问什么,但我认为这更容易阅读:
@Override
public String put(final String row, final String column, final String value) {
/**
* Second map which is contained by centralMap, that contain Strings as
* Keys and Values.
*/
Map nestedMap = centralMap.get(row);
if (nestedMap == null) {
nestedMap = new HashMap();
centralMap.put(row,nestedMap);
}
if (!nestedMap.containsKey(column))
counter++;
centralMap.put(row, nestedMap);
return nestedMap.put(column, value);
}
I can't quite understand what you're doing in the second stanza, so can't help you improve that. And I don't see an iterator as referred to in your question.
我不太明白你在第二节中做了什么,所以无法帮助你改进。而且我没有看到你问题中提到的迭代器。
You're making me guess, but maybe ELSEWHERE in your program (it would really help to see more code, and a specific function prototype or statement of behavior you're seeking) you want to be able to iterate through the contents of the centralMap
instance, and nested instances of nestedMap
. Yes you can.
你猜对了,但是你的程序中可能是ELSEWHERE(它确实有助于查看更多代码,以及您正在寻找的特定函数原型或行为声明)您希望能够遍历centralMap的内容实例和嵌套的嵌套实例。是的你可以。
public void iterateOverAllNested()
{
for (Map.Entry> nested : centralMap) {
final String centralKey = nested.key();
final Map nestedMap = nested.value();
System.out.println("Central map row/column: "+centralKey);
for (Map.Entry entry : nestedMap) {
System.out.println(" key="+entry.key()+", value="+entry.value());
}
}
}
Note that this smells. Nested maps of untyped String
s are probably wrong. Any chance you've been writing Perl recently? I suggest you write a second SO question asking about a good data structure for your specific problem. You can include this code as your starting place, and folks will likely offer a cleaner solution.
请注意,这闻起来。无类型字符串的嵌套映射可能是错误的。你最近有没有机会写Perl?我建议你写第二个问题,询问有关特定问题的良好数据结构。您可以将此代码作为起始位置,人们可能会提供更清晰的解决方案。