【问题标题】:Java: convert HashMap values to Set<Integer>Java:将 HashMap 值转换为 Set<Integer>
【发布时间】:2013-11-12 06:12:43
【问题描述】:

关于Java中HashMap的另一个问题:

我有以下

Map<String, Set<Integer>> myWordDict = new HashMap<String, Set<Integer>>();

将数据存入变量myWordDict后,我想遍历HashMapValues,并将每个值添加到一个新的Set变量中?

当我尝试做Set&lt;Integer&gt; newVariable = myWordDict.entrySet() 时,似乎数据类型不兼容。

所以我的问题本质上是:

如何将 HashMap 值或 entrySet() 转换为 Set ?

谢谢

【问题讨论】:

  • 地图值类型是什么?
  • 你想要所有的键值组合作为一个集合吗?还是您只想要键或值?
  • 我希望只有 Set 中的值。值类型为整数。
  • @TonyGW 你说值类型是Integer,但在上面的例子中值类型实际上是Set。
  • 是的,值为 Set。很抱歉造成混乱

标签: java hashmap set


【解决方案1】:

试试:

Set<Integer> newVariable = mywordDict.keySet();

或

Set<Integer> newVariable = new HashSet<Integer>(myWordDict.values());

【讨论】:

    【解决方案2】:

    您的声明应如下所示。它会将您的地图值转换为Collection

      Collection<Set<Integer>> newVariable = myWordDict.values();
    

    【讨论】:

    • 你确定这行得通吗?!首先,你必须投射它,即使你这样做,它也会抛出一个ClassCastException。
    【解决方案3】:

    What does entrySet returns?

    public Set<Map.Entry<K,V>> entrySet()
    

    返回此映射中包含的映射的 Set 视图。集合由地图支持,因此对地图的更改会反映在集合中,反之亦然。

    如果您想遍历 myWordDict 哈希映射,那么只需

    for(Map.Entry<String, Set<Integer>> entry : myWordDict.entrySet())
    {
       Set<Integer> newVariable = entry.getValue();
    }
    

    相关链接

    【讨论】:

    • 我现在使用迭代器来遍历HashMap值:
    【解决方案4】:

    如果您需要将所有值作为 Integers 的 Set 而不是 Set 的 Set,那么您可以这样做

    Set<Integer> newVariable = new HashSet<Integer>();
    for (Set<Integer> set : myWordDict.values()) {
        newVariable.addAll(set);
    }
    

    或者,如果您希望它们成为Set 的Set,您需要执行类似的操作

    Set<Set<Integer>> newVariable = new HashSet<Set<Integer>>();
    newVariable.addAll(myWordDict.values());
    

    【讨论】:

      【解决方案5】:

      flatMap 将帮助您:

      myWordDict.values().stream().flatMap(Set::stream).collect(Collectors.toSet());
      

      【讨论】:

        【解决方案6】:

        使用myWordDict.values(),而不是myWordDict.entrySet()。 values 是一组映射的values,而entrySet 是一组映射(它是一组 java.util.map.Entry 对象,每个对象描述一个键和一个值)。

        【讨论】:

        • values() 是 Collections 类型,所以除非我做一个丑陋的演员表,否则还有数据不兼容的问题。
        • @TonyGW AFAIK 没有办法解决这个问题,除非您想通过添加条目集中的每个值来手动构造集合。
        【解决方案7】:

        假设您要添加地图中集合中的所有值,您的方法是使用addAll 方法:

        Set<Integer> newVariable = new HashSet<Integer>();
        for (Set<Integer> set : myWordDict.values()) {
            newVariable.addAll(set);
        }
        

        【讨论】:

        • @TonyGW 它会根据您的 myWordDict 中的条目数运行 - 如果您有 10 个条目,for 循环将进行 10 次迭代。
        【解决方案8】:
        Map<Integer,String>   map1 = new HashMap<Integer,String>();
        
        map1.put(1, "Rakesh");
        map1.put(2, "Amal");
        map1.put(3, "Nithish");
        
        Set<Entry<Integer,String>> set1 = map1.entrySet();
        

        【讨论】:

          【解决方案9】:

          所以,我想出了一个迭代器来遍历值 Set

          我有这个数据结构

          Map<String, Set<Integer>> myWordDict = new HashMap<String, Set<Integer>>();
          

          我创造了

          Iterator mapIterator = myWordDict.entrySet().iterator();
          

          然后只需使用 while() 循环将单个值添加到新的 Set 变量。我不确定这是最有效的方法,但它可以满足我的需求。`

          【讨论】:

            猜你喜欢
            • 2011-08-22
            • 1970-01-01
            • 1970-01-01
            • 2017-01-28
            • 2021-10-01
            • 1970-01-01
            • 1970-01-01
            • 2016-09-18
            • 1970-01-01
            相关资源
            最近更新 更多