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

Quartz start at a specific time and run at specific interval
java upcasting polymorphism issue [duplicate]
IntelliJ IDEA tells me “Error:java: Compilation failed: internal java compiler error idea”
How to set default color for ColorPicker in FXML
rpc call to pass a file in Java
Unable to interrupt Avg threads properly
Java Spark: Non-blocking routes / callbacks with CompletableFutures
JavaFx out of memory error [duplicate]
how to filter an user input string and store the new filtered array
Communication between Play! project and Java project
How to change background if I choose RadioButton and click Button
Why do my Custom GridView show only one column?
Static Method Reference from Spring XML
Reading from an InputStream of a Socket
Android shared element transition not working with different img scale types
How to put ListView and TextView in the same layout in android? [duplicate]

Categories

HOME
testing
pdf
kde
webstorm
read-eval-print-loop
fsm
wamp
upload
node-pdfkit
midi
etl
google-cloud-ml
commonmark
leiningen
messages
netflix
propel
google-cloud-speech
shared-hosting
kryo
visjs
object-detection
clickonce
facebook-instant-articles
accessor
excel-2007
extjs5
pass-by-reference
microsoft-r
icloud-api
crystal-reports-2010
android-widget
fish
centos6.5
wpfdatagrid
strncpy
buck
large-file-upload
windowbuilder
kendo-datasource
bpel
nat
pdb
winrt-xaml-toolkit
kendo-ui-grid
windows-dev-center
mozilla
rotational-matrices
amazon-kinesis-kpl
sequential
jvm-languages
node-gyp
unixodbc
websphere-mq-fte
galleria
network-flow
upstart
komodoedit
blogengine.net
alphabet
recursive-datastructures
nodebb
knpmenubundle
sqldf
or-tools
specrun
word-vba-mac
visual-c++-2008
google-feed-api
removechild
vmware-tools
infix-notation
underscore.js-templating
nsfilemanager
xml-attribute
hexdump
nessus
itextpdf
wapiti
qtableview
quicklisp
embedded-code
splash
sortedlist
census
composite
inmobi
device-orientation
asp.net-web-api-odata
markers
rdl
php-5.4
expected-exception
marmalade
jboss-weld
hyprlinkr
dexterity
ftps
flash-builder4.5
qt-faststart
django-nonrel
interface-orientation
jelly
multipage
pendrive
inotifycollectionchanged
celltable
web-architecture
sef
exchange-server-2003
noscript
nerddinner
zend-decorators
gacutil

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App