Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在嵌套映射中输入值_Java_Hashmap - Fatal编程技术网

Java 在嵌套映射中输入值

Java 在嵌套映射中输入值,java,hashmap,Java,Hashmap,我正在考虑为一个图实现一个邻接列表的方法,并认为嵌套映射可能是一个好主意(因为查找时间很快),对于外部映射我可以说map.put,但是对于未命名的内部映射,如何将值放入 Scanner sc = new Scanner(System.in); Map<Integer,Map<Integer,Integer>> graph = new HashMap<>(); int V = sc.nextInt(); int E = sc.ne

我正在考虑为一个图实现一个邻接列表的方法,并认为嵌套映射可能是一个好主意(因为查找时间很快),对于外部映射我可以说map.put,但是对于未命名的内部映射,如何将值放入

    Scanner sc = new Scanner(System.in);
    Map<Integer,Map<Integer,Integer>> graph = new HashMap<>();
    int V = sc.nextInt();
    int E = sc.nextInt();
    for(int i=0;i<E;i++){
        int v1 = sc.nextInt();
        int v2 = sc.nextInt();
        int w = sc.nextInt();
        graph.put(v1, Map.entry(v2, w));
        graph.put(v2, Map.entry(v1, w));

    }
Scanner sc=新扫描仪(System.in);
映射图=新的HashMap();
int V=sc.nextInt();
int E=sc.nextInt();

对于(int i=0;i我不确定Map.entry方法是从哪里获得的,但通常您会创建内部映射,就像外部映射一样,然后将其放入。类似于:

Scanner sc = new Scanner(System.in);
Map<Integer,Map<Integer,Integer>> graph = new HashMap<>();
int V = sc.nextInt();
int E = sc.nextInt();
for(int i=0;i<E;i++){
    int v1 = sc.nextInt();
    int v2 = sc.nextInt();
    int w = sc.nextInt();

    Map<Integer,Integer> innerMap = new HashMap<>();
    innerMap.put(v2, w);
    graph.put(v1, innerMap);

    Map<Integer,Integer> andAnotherMap = new HashMap<>();
    andAnotherMap.put(v2, w);
    graph.put(v2, andAnotherMap);

}
Scanner sc=新扫描仪(System.in);
映射图=新的HashMap();
int V=sc.nextInt();
int E=sc.nextInt();

对于(inti=0;i,可以这样放入图中

if (!graph.contains(v1)) {
    grah.put(v1, new HashMap<>());
}

if (!graph.contains(v2)) {
    grah.put(v2, new HashMap<>());
}

graph.get(v1).put(v2, w);
graph.get(v1).put(v2, w);
if(!graph.contains(v1)){
put(v1,newhashmap());
}
如果(!graph.contains(v2)){
put(v2,newhashmap());
}
图.get(v1).put(v2,w);
图.get(v1).put(v2,w);
  • 如果缺少
    graph.putIfAbsent(v1,new HashMap());
  • 获取内部映射图。获取(v1)
  • 将值放入内部映射
    graph.get(v1).Put(v2,2)
  • graph.putIfAbsent(v1,newhashmap());
    putIfAbsent(v2,newhashmap());
    图.get(v1).put(v2,w);
    图.get(v2).put(v1,w);
    
    yeah Map.entry出现语法错误,我想你可以用它来保存地图条目,但不创建新条目。根据你的代码,每次需要在内部地图上创建一个新条目时,它都会覆盖以前的条目,而不是将其链接到以前的地图。在这种情况下,我只需要放置一个!graph.contains(v1/v2)如@sc0der您是对的,@sc0der的解决方案在这种情况下更好。
    if (!graph.contains(v1)) {
        grah.put(v1, new HashMap<>());
    }
    
    if (!graph.contains(v2)) {
        grah.put(v2, new HashMap<>());
    }
    
    graph.get(v1).put(v2, w);
    graph.get(v1).put(v2, w);
    
    graph.putIfAbsent(v1, new HashMap<>());
    graph.putIfAbsent(v2, new HashMap<>());
    graph.get(v1).put(v2, w);
    graph.get(v2).put(v1, w);