Cannot add (int, double) pair to HashMap <Integer, Double>

I am working on a Java project right now and I have a class that I created called DistanceQueue. His signature is set

public class DistanceQueue<Integer> extends PriorityQueue<Integer> 

There is a method in this class

 public boolean add(int v) 

which adds a key-value pair (v, Double.MAX_VALUE) to the HashMap called the distance, which is in the DistanceQueue class. However inside add (int v) when I type

 distances.put(v, Double.MAX_VALUE); 

I get the following error:

 DistanceQueue.java:98: error: no suitable method found for put(int,double) distances.put(v, Double.MAX_VALUE); ^ method HashMap.put(Integer,Double) is not applicable (actual argument int cannot be converted to Integer by method invocation conversion) where Integer is a type-variable: Integer extends Object declared in class ShortestPaths.DistanceQueue 1 error 

Does anyone know why I am getting this error? I thought Java would automatically convert between int and Integer for you. Is there an easy way I can fix this?

Thanks!

+5
source share
1 answer

You use Integer as the name of a type parameter that hides java.lang.Integer .

 public class DistanceQueue<Integer> extends PriorityQueue<Integer> ^^^^^^^ 

You should probably just discard a type parameter:

 public class DistanceQueue extends PriorityQueue<Integer> 
+19
source

Source: https://habr.com/ru/post/1208578/


All Articles