Java类型不匹配(但两者都来自同一类型?!):无法从HashMap转换<;整数,ArrayList<;整数>&燃气轮机;到HashMap<;整数,ArrayList<;整数>&燃气轮机;

Java类型不匹配(但两者都来自同一类型?!):无法从HashMap转换<;整数,ArrayList<;整数>&燃气轮机;到HashMap<;整数,ArrayList<;整数>&燃气轮机;,java,arraylist,collections,maps,Java,Arraylist,Collections,Maps,编辑:问题解决了:请参阅卡里姆·斯努西的答案和我在下面的评论 这是我在堆栈溢出时的第一个问题,所以我可能不会在一开始就把所有事情都做好。对不起。此外,我还不熟悉java和一般编程 我遇到了一个奇怪的错误,我无法真正理解解决方案可能是什么 在尝试将哈希映射从一个类传递到另一个类时,IDE eclipse说: 类型不匹配:无法从HashMap>转换为HashMap> 但如果我没弄错的话,它们都是同一类型的,所以我不知道问题出在哪里 这是我的代码:我只发布了部分内容,所以不会把所有内容都弄乱 我的类

编辑:问题解决了:请参阅卡里姆·斯努西的答案和我在下面的评论


这是我在堆栈溢出时的第一个问题,所以我可能不会在一开始就把所有事情都做好。对不起。此外,我还不熟悉java和一般编程

我遇到了一个奇怪的错误,我无法真正理解解决方案可能是什么

在尝试将哈希映射从一个类传递到另一个类时,IDE eclipse说: 类型不匹配:无法从HashMap>转换为HashMap>

但如果我没弄错的话,它们都是同一类型的,所以我不知道问题出在哪里

这是我的代码:我只发布了部分内容,所以不会把所有内容都弄乱

我的类中有一个名为graph.class的HashMap, 它用于方法public HashMap>getAllShortestPaths() 其中,将计算从每个节点到图中任何其他节点的所有最短路径 并存储到hashMap中进行进一步处理。 这很好,当把地图打印到屏幕上时,它会正确地显示所有信息,如果我想从里面的方法来做的话

但我的目的是将这个hashMap传递给另一个类,在这个类中,我将收集对图形的全部分析,并将其保存到一个新文件中

public class Graph {
.
.
    private HashMap<Integer, ArrayList<Integer>> shortestPathsMap = new HashMap<Integer, ArrayList<Integer>>();
公共类图{
.
.
私有HashMap shortestPathsMap=newhashmap();
。 .

public HashMap getShortestPathsMap(){return shortestPathsMap;}
.
.
.
public void getAllShortestPath(){
对于(int i=0;i
因此,为了进行测试,我将其传递给Main.class,并希望将其打印到屏幕上:

public HashMap<Integer, ArrayList<Integer>> getShortestPathsMap() { 
        return shortestPathsMap; 
}

public class Main {

    public static void main(String[] args) {
.
.
.
G.getAllShortestPaths();

                    HashMap<Integer, ArrayList<Integer>> spMap = new HashMap<Integer, ArrayList<Integer>>();
                    spMap = G.getShortestPathsMap();

                    // iterate and display values
                    for(Entry<Integer, ArrayList<Integer>> entry : spMap.entrySet()) {
                        int key = entry.getKey();
                        ArrayList<Integer> values = entry.getValue();

                        System.out.println("Key = " + key);
                        System.out.println("Values = " + values);
                    }
.
.
. 
public HashMap getShortestPathsMap(){
返回最短路径映射;
}
公共班机{
公共静态void main(字符串[]args){
.
.
.
G.获取所有最短路径();
HashMap spMap=新的HashMap();
spMap=G.getShortestPathsMap();
//迭代并显示值
for(条目:spMap.entrySet()){
int key=entry.getKey();
ArrayList values=entry.getValue();
System.out.println(“Key=“+Key”);
System.out.println(“值=”+值);
}
.
.
. 
在Main.class中,在以下行: spMap=G.getShortestPathsMap(); IDE显示

类型不匹配:无法从HashMap转换> 到HashMap>

但是:

HashMap spMap=newhashmap();
HashMap shortestPathsMap=newhashmap();
spMap和shortestPathsMap是同一类型的,不是吗


我很高兴收到任何有用的回复,并提前向您表示感谢。

尝试在您的主要方法中替换此选项

HashMap<Integer, ArrayList<Integer>> spMap = new HashMap<Integer, ArrayList<Integer>>();
spMap = G.getShortestPathsMap();
HashMap spMap=newhashmap();
spMap=G.getShortestPathsMap();
用这个

Map<Integer, ArrayList<Integer>> spMap = G.getShortestPathsMap();
Map spMap=G.getShortestPathsMap();

你能发布
GetShortestPathMap();
的代码吗?它只是一个简单的getter。我用getter方法试过一次,用getAllShortestPaths()方法中的return参数试过一次,但都不起作用。请清理你的问题。你的类型都已损坏。我看到很多“HashMap>”这表明某些软件试图修改您的问题,可能是出于“HTML清理”的原因。您粘贴的代码不完整。
G.getShortestPathsMap()
–在哪里定义了“G”?在哪里定义了“getShortestPathsMap”定义?许多其他东西也不一致。看看这个:是的,很不幸,我曾尝试复制粘贴代码,但一直以来它都会被自动修改,清理起来很混乱。已经尝试过了,但不幸地得到了相同的类型不匹配消息。但是谢谢,我真的不知道它有什么问题,但请尝试一下用
spMap=newhashmap(G.getShortestPathsMap());
太好了,非常感谢你的提示。这对我很有效:
HashMap-spMap=newhashmap(G.getShortestPathsMap());
不幸的是,它首先工作了,但在多次运行程序后,没有任何错误,它突然说:构造函数HashMap(HashMap)未定义。我将其更改为
HashMap spMap=new HashMap(G.getShortestPathsMap());
,尽管警告HashMap是原始类型,但它仍然有效。对泛型类型HashMap的引用应参数化
HashMap<Integer, ArrayList<Integer>> spMap = new HashMap<Integer, ArrayList<Integer>>();
spMap = G.getShortestPathsMap();
Map<Integer, ArrayList<Integer>> spMap = G.getShortestPathsMap();