(重新定义方法
public Collection<String> values();
)
我有一张地图包含在另一张地图中,所以就像这样:
Map<String,Map<String,String>> centralMap
(实习生地图是subjectGradeMap) 我现在想要使用这种方法:
public Collection<String> values()
获取包含地图所有值的集合。我试过了:
Collection<String> returncoll = centralMap.values().values();
但它不起作用。试过这个:
Collection<Map<String,String>> collec = centralMap.values();
Collection<String> returncollection = collec.values();
但徒劳无功:-S
这个问题解决了谢谢! 现在我想问你是否有想法,我应该如何实现Iterator方法?
/**
* Returns an Iterator, that pass through all the entries of the map. If
* something changes in the map, when it is being passed through all its'
* entries by the Iterator, we can't determinate the behaviour that will
* happen..
*
* @return An Iterator that pass through all entries. Every entry will be
* returned as String-Tripel with its' three Elements row, column
* and value.
*/
@Override
public Iterator<Entry> iterator() {
return null;
}
你有什么想法吗?
Entry类是以下类,(在我用来创建TrueStringMap2D对象的接口中实现:
final class Entry
{
/** First Key. */
private final String key1;
/** Second Key. */
private final String key2;
/** Value. */
private final String value;
/** Ctor for a tripel.
* @param key1 1st key.
* @param key2 2nd key.
* @param value Value.
*/
public Entry(final String key1, final String key2, final String value)
{
this.key1 = key1;
this.key2 = key2;
this.value = value;
}
public String getFirstKey()
{
return key1;
}
public String getSecondKey()
{
return key2;
}
public String getValue()
{
return value;
}
@Override public boolean equals(final Object anything)
{
if(anything == null)
return false;
if(getClass() != anything.getClass())
return false;
final Entry that = (Entry)anything;
return Objects.equals(getFirstKey(), that.getFirstKey())
&& Objects.equals(getSecondKey(), that.getSecondKey())
&& Objects.equals(getValue(), that.getValue());
}
// CHECKSTYLE- Magic Number
@Override public int hashCode()
{
int hash = 7;
hash = 17 * hash + Objects.hashCode(getFirstKey());
hash = 17 * hash + Objects.hashCode(getSecondKey());
hash = 17 * hash + Objects.hashCode(getValue());
return hash;
}
// CHECKSTYLE+ Magic Number
@Override public String toString()
{
return String.format("(%s, %s, %s)", getFirstKey(), getSecondKey(), getValue());
}
}
谢谢你的帮助!
答案 0 :(得分:1)
centralMap.values()
会返回一个Collection,Collection没有values()
。 centralMap.values()
基本上会返回地图列表。因此,为了评估每个地图,您需要迭代:
for (Map map : cetralMap.values()) {
Collection values = map.values();
// do something with your values here
}
构建centralMap
中包含的所有地图中所有值的集合:
List myGrandList = new ArrayList();
for (Map map : centralMap.values()) {
myGrandList.addAll(map.values());
}
return myGrandList;
答案 1 :(得分:0)
您可以使用MultiKeyMap
(来自Apache的Commons-Collections):
MultiKeyMap<String, String> map;
map.put("Key1", "Key2", "Value");
map.put("Key1", "Key3", "Value2");
for (Entry<MultiKey<String>, String> entry : map.entrySet()) {
// TODO something
}
它有两个版本: