我如何从 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; 的地图中,您有

  • vlist.keys() a List<String> ,其中包含所有键
  • vlist.values()一个List<HashMap<String, Double>>,其中包含所有是一个值
  • 的map

要获得特定的 List<Double> 你需要一把钥匙,所以 select 你将从中读取双打的内部地图

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

所以你说:

基本上我想把声明的内部地图的所有值放在这里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(它们是内部映射),然后 combine all the values in those maps 通过 flatMap 进入公共流,然后收集到 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>>,其中字符串是外部 Map 的键,您可以这样做。创建 outer mapentrySetstream 并将其传递给 collectorcollector 使用 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]}