java - map nasted 在集合中。获取 2D map 的 value() 集

标签 java collections dictionary hashmap

(方法的重新定义

public Collection<String> values();

)

我有一个 map 包含在另一个 map 中,所以它是这样的:

Map<String,Map<String,String>> centralMap

(实习生 map 为subjectGradeMap) 我现在想使用这个方法:

public Collection<String> values()

获取包含 map 所有值的集合。我尝试过:

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 对象的接口(interface)中实现:

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());
            }

    }

感谢您的帮助!

最佳答案

centralMap.values() 返回一个 Collection,Collection 没有 values()centralMap.values() 本质上返回一个 map 列表。因此,为了评估每个 map ,您需要迭代:

for (Map map : cetralMap.values()) {
    Collection values = map.values();
    // do something with your values here
}

要构建 centralMap 中包含的所有 map 的所有值的集合:

List myGrandList = new ArrayList();
for (Map map : centralMap.values()) {
    myGrandList.addAll(map.values());
}
return myGrandList;

关于java - map nasted 在集合中。获取 2D map 的 value() 集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10430185/

相关文章:

java - 关于Java中初始化 block 的一些问题

Java int[] 数组到 HashSet<Integer>

java - 在 Java 中对数组列表进行排序时无法转换为类错误

.net - 为什么 .Net 词典会调整为质数?

python - 遵循层次结构的多个嵌套字典

Python合并两个字典而不丢失顺序

java - 捕获并修改从外部 JAR 发送的 stderr

java - 如何估计 JVM 是否有足够的空闲内存用于特定的数据结构?

java - 将 Room 持久性库与 sqlite 一起使用

java - 从java 8流中获取N(3)个列表(基于ENUM性别(男,女,其他))并基于ENUM过滤条件