【问题标题】:How to get a Set from a HashMap within a HashMap如何从 HashMap 中的 HashMap 获取 Set
【发布时间】:2020-02-01 17:12:12
【问题描述】:

我正在尝试从另一个 HashMap 中的 HashMap 创建一个 Set 和 Collection。问题是getKey()方法不适用。

HashMap<HashMap<String, String>, String> list = getList(issues);
Set <String> set1 = list.getKey().keySet();
Collection <String> set2 = list.getKey().values();

【问题讨论】:

  • 你能给我一个例子或更多说明你想要达到的目标吗?
  • 应该是HashMap&lt;String, HashMap&lt;String, String&gt;&gt;? (或者更好的Map&lt;String, Map&lt;String, String&gt;&gt;
  • 除非这是一个 OCP/OCA 问题,否则您肯定会修改您的代码设计。
  • @TomHawtin-tackline 为什么这种结构会更好?
  • @PalaniSriram 我想获取 Set 和 Collection 然后将它们转换为 ArrayLists。

标签: java hashmap set


【解决方案1】:

从您的集合中,要获取每个条目的所有键,您必须遍历每个条目并获取键,要使用 Java-8+ 做到这一点,您可以使用:

Set<String> allKeys = list.entrySet().stream() 
        .flatMap(e -> e.getKey().keySet().stream())
        .collect(Collectors.toSet());

要获取值,您可以这样做:

List<String> allValues = list.entrySet().stream()
        .flatMap(e -> e.getKey().values().stream())
        .collect(Collectors.toList());

【讨论】:

    【解决方案2】:

    获取密钥

    //Get Keys of childHashMap
    Set<String> keys = parent.keySet().stream().
    flatMap(child -> child.keySet().stream()).
    collect(Collectors.toSet());
    

    1.Stream on keySet of parent hashMap(迭代子 hashmap)

    • parent.keySet().stream()

    2.返回子hashmap的keySet(类似于{1},{2,3},{4,5}

    • child -&gt; child.keySet().stream()

    3.Flat上一步设置(就像将{1},{2,3},{4,5}转换为{1,2,3,4,5}

    • flatMap(child -&gt; child.keySet().stream()).

    4.返回键

    • collect(Collectors.toSet());

    获取值

    //Get Values of childHashMap
    Collection<String> values = parent.keySet().stream()
    .map(HashMap::values)
    .flatMap(Collection::stream)
    .collect(Collectors.toList());
    

    1.Stream keySet 的父 hashMap(迭代子 hashmap)

    • parent.keySet().stream()

    2.子hashMap的返回值(如:{1},{2},{3}

    • map(HashMap::values)

    3.使用flatMap将上一步变平(将{1},{2},{3}转换为{1,2,3}

    • flatMap(Collection::stream)

    4.将值收集到列表中

    • collect(Collectors.toList())

    示例

      //Parent
      HashMap<HashMap<String, String>, String> parent = new HashMap<>();
    
            //Child HashMap 1
            HashMap<String, String> childHashMap = new HashMap<>();
            childHashMap.put("a", "b");
    
            //Child HashMap2
            HashMap<String, String> childHashMap2 = new HashMap<>();
            childHashMap2.put("d", "e");
    
            //Parent init
            parent.put(childHashMap, "c");
            parent.put(childHashMap2, "f");
    
            //Get Keys
            Set<String> keys = parent.keySet().stream().flatMap(child -> child.keySet().stream()).collect(Collectors.toSet());
    
            //Get values
            Collection<String> values = parent.keySet().stream().map(HashMap::values).flatMap(Collection::stream).collect(Collectors.toList());
    
            //Print key and values
            keys.forEach(System.out::println);
            values.forEach(System.out::println);
    

    结果 =

    键 = a ,d

    值 = e , b

    【讨论】:

      猜你喜欢
      • 2019-05-06
      • 2016-02-05
      • 2013-08-03
      • 1970-01-01
      • 1970-01-01
      • 2015-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多