繁体   English   中英

如何从 Java 中的嵌套 HashMap 创建值的数组列表?

[英]How do i create an arraylist of values from a nested HashMap in java?

我已经使用嵌套的 HashMap 编写了我的代码,我正在尝试弄清楚如何将内部映射的键指向的所有值放入一个 ArrayList 中,以便对其进行正确排序。 我的地图是这样的:

HashMap<String, HashMap<String, Double>> vlist;

我的想法是创建另一个具有与之前显示的内部映射相同的键和值的 HashMap,然后以这种方式填充它。

HashMap<String, Double> vlistValues = new HashMap<>(vlist.values());

我收到一个编译错误,我可以弄清楚编译器不知道我的外部映射的值是映射本身,但是阅读 hashmap 文档我没有找到适合我的情况的方法。

基本上我想把这里声明的内部映射的所有值HashMap<String ,HashMap<String, Double>> vlist; 到这样的列表ArrayList<Double> listOfValues;

如果不清楚,我对编程完全陌生:-)

我将展示一个示例:我的地图HashMap<String, Hashmap<String,Double>>表示加权图的邻接列表。 我需要对所有边进行排序(因为我正在尝试实现 Kruskal 算法),我的想法是将所有权重放在一个列表中,执行如下操作:

ArrayList<String> vertexList; //all the vertices of the graph
ArrayList<Double> weights; 
HashMap<String, String> orderedEdges = new HashMap<>(); //here i put ordered edges
double min = Collections.min(weights); //i use this double to keep track of the minimum element in weights

  for(String vertex1 : vertexlist){
    makeSet(vertex1);
    for(String vertex2 : ajacents(vertex1)){
      if(getEdgeWeight(v1,v2) <= min){ //method "getEdgeWeight" is to retrieve weight of an edge
        orderedEdges.put(v1,v2);
        min = getEdgeWeight(v1,v2) 
        weights.remove(min) //i'm not sure this line is correct
      }
    }
  }

在网上查看一些伪代码,我看到它能够在同一个 for 循环中创建不相交的集合并排序边缘。 可能我的代码效率不高,但我真的不知道如何在不访问所有图形的情况下对边进行排序。 Ps 我不能使用优先队列,但我完全知道我正在尝试做的是类似的事情

所以你说:

基本上我想把这里声明的内部映射的所有值HashMap<String ,HashMap<String, Double>> vlist ; 到这样的列表ArrayList<Double> listOfValues

这是示例 hashMap。

      Map<String, Map<String, Double>> mm = Map.of("A",
            Map.of("R", 1.2, "S", 3.4, "T", 3.8),
            "B",
            Map.of("S", 9.8, "V", 2.8),
            "Z",
            Map.of("P", 22.3));

      System.out.println(mm);

这是地图。

{Z={P=22.3}, B={S=9.8, V=2.8}, A={T=3.8, S=3.4, R=1.2}}

要转换为双打List ,您可以执行此操作。 获取valuesstream (即内部映射),然后通过flatMap combine all the values in those maps成一个公共流,然后收集到List

      List<Double> dubs =
            mm.values().stream().flatMap(k -> k.values().stream()).collect(
                  Collectors.toList());

      System.out.println(dubs);

这是清单。

[22.3, 9.8, 2.8, 3.8, 3.4, 1.2]

如果你想要一个Map<String, List<Doubles>>其中 String 是外部 Map 的键,你可以这样做。 创建outer mapentrySet stream并将其传递给collector collector使用outer maps key创建一个映射,然后获取inner map (这是一个集合)的值并将它们作为参数传递给ArrayList<>以创建一个List

  Map<String, List<Double>> mapOfDubs =
        mm.entrySet().stream().collect(Collectors.toMap(e -> e.getKey(),
              e -> new ArrayList<>(e.getValue().values())));

  System.out.println(mapOfDubs);

这是地图。

{A=[1.2, 3.4, 3.8], B=[2.8, 9.8], Z=[22.3]}

来自像HashMap<String, HashMap<String, Double>> vlist; 你有

  • vlist.keys()一个List<String> ,它包含所有的键
  • vlist.values()一个List<HashMap<String, Double>> ,其中包含所有作为值的映射

要获得特定的List<Double>您需要一个键,因此请选择从中读取双打的内部映射

HashMap<String, HashMap<String, Double>> outer = ...; // don't call a map *list*
HashMap<String, Double> inner  = outer.get("mykey");
List<Double> values = new ArrayList<>(inner.values()); // as values() returns a Collection

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM