java


Java generics: Calling generic Method “…is not applicable for the arguments…”


I have 2 HashMaps with <Integer, "sometype">. So the "sometype" may differ and therefore i tried to make it generic. In this case my two varaibles are as follows
private HashMap<Integer, UI_FieldPV> values_map = new HashMap<Integer, UI_FieldPV>();
private HashMap<Integer, JComponent> input_map = new HashMap<Integer, JComponent>();
The first call of the method is fine:
this.input_map = MapOperations.<JComponent> rearrengeHashMapIdx(this.input_map);
the second call passes a HashMap with <Integer, CustomClass>
this.values_map = MapOperations.<UI_FieldPV>rearrengeHashMapIdx(this.input_map);
which gives me the following error:
The parameterized method <UI_FieldPV>rearrengeHashMapIdx(HashMap<Integer,UI_FieldPV>) of type UI.MapOperations is not applicable for the arguments (HashMap<Integer,JComponent>)
Coding of the class which contains the generic method (btw: I tried to create a generic method within the calling class but it didnÄt work. Do i have to create a embedded class in order to make generic method paramaters work?)
private static class MapOperations<T> {
public static <T> HashMap<Integer, T> rearrengeHashMapIdx(HashMap<Integer, T> source) {
HashMap<Integer, T> temp = new HashMap<Integer, T>();
for (Integer i = 0; i < 81; i++) {
Integer rowNum = (i / 3) % 3;
Integer block = i / 9;
Integer delta = (rowNum - block);
Integer newIdx = i + (delta * 6);
temp.put(i, source.get(newIdx));
}
return temp;
}
}
So what am I doing wrong?
Thanks for your help in advance!
The compiler error is clear enough. The first invocation of method:
this.input_map = MapOperations.<JComponent>rearrengeHashMapIdx(this.input_map);
would return a HashMap<Integer, JComponent>, as you have given the explicit type parameter (well, that is not really needed here. The type T will anyways be inferred from the type of HashMap you are passing). That is fine, as you have declared your input_map to be of that type only.
Then you are passing input_map as argument to the next method invocation:
this.values_map = MapOperations.<UI_FieldPV>rearrengeHashMapIdx(this.input_map);
Now, as per the declaration of the method, the argument to the method should be of type HashMap<Integer, T>. In the second method invocation, the type parameter T is inferred as UI_FieldPV. So, the method expects a HashMap<Integer, UI_FieldPV>, but you are passing a HashMap<Integer, JComponent>. Of course the method invocation would fail, as both the maps are incompatible.
Perhaps, in the second invocation, you meant to pass values_map as argument. So this would work fine:
this.values_map = MapOperations.<UI_FieldPV>rearrengeHashMapIdx(this.values_map);
Note that the type parameter T used in the method is unrelated to the type parameter T used with the class declaration, although that doesn't make any difference here. But just FYI.
I don't understand, ¿you put "input_map" intentionally?:
this.values_map = MapOperations.<UI_FieldPV>rearrengeHashMapIdx(this.input_map);
Maybe you need something like:
this.values_map = MapOperations.<UI_FieldPV>rearrengeHashMapIdx(this.values_map);

Related Links

Trim() vs IndexOf()
Should I create a Gradle project and add Drools to it or vice versa?
Draw on one side of a JPanel
use jersey2.3 tomcat8 , the page get 404
Which value is taken when bidirectionalbinding two properties with different values? [duplicate]
NullPointerException while modyfing elements on second Pane JavaFX
gradlew command not found?
Minecraft Bukkit 1.8 saving list into config.yml
Is it possible to open a file having any extension in Java? [duplicate]
Configuring FIPS mode for JRE
Exported Javafx application does not run as Jar file (Eclipse)
How to restart a Java Thread in an Android game? [duplicate]
Starting a simple JUnit / Maven project in IntelliJ Idea
Java 8 Streams map API - interpretation of method reference
MongoDB: What's the debug operation for?
Adding jackson-mapper-asl causes java.lang.ClassNotFoundException

Categories

HOME
vim
client
openlayers
netsuite
tizen
onedrive
cplex
kalman-filter
google-docs
qore
virtualization
google-translate
here-api
volttron
handsontable
remote-access
clearcase-ucm
uitypeeditor
propel
emulator
dcevm
centos6.5
fgetcsv
social-media
elasticsearch-ruby
ejabberd-module
compatibility
libuv
vision
noraui
usbserial
android-kernel
force-layout
main
restlet
android-browser
hue
bootstrapper
node-sass
qcombobox
dotcover
parentheses
chain-builder
multipeer-connectivity
worksheet
sage-one
integrity
quartz-composer
instant
database-optimization
file-writing
phishing
measures
asp.net-4.5
merge-conflict-resolution
autorest
nsviewcontroller
ready-api
testng-dataprovider
netmq
phpcas
prerequisites
collapse
cannon.js
django-unittest
tarjans-algorithm
bridge.net
kineticjs
tween
googlemock
didselectrowatindexpath
buster.js
gwt-rpc
comaddin
chrono
uiviewanimation-curve
postgresql-performance
locationlistener
yui-datatable
winbugs14
pyinotify
radcombobox
gallio
.nettiers
modelstate
mediarss
signal-handling

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App