error: no suitable method found for put(String,int)

99封情书 提交于 2019-12-13 04:34:40

问题


I got errors when compiling this:

TreeMap <String, Long> myMap = new TreeMap <String, Long>();
//populate the map
myMap.put("preload_buffer_size", 1024);
myMap.put("net_buffer_length", 1024);
//etc...


error: no suitable method found for put(String,int)
    myMap.put("preload_buffer_size", 1024);
         ^
method TreeMap.put(String,Long) is not applicable
  (actual argument int cannot be converted to Long by method invocation conversion)
method AbstractMap.put(String,Long) is not applicable
  (actual argument int cannot be converted to Long by method invocation conversion)

And I need to use Long, not int I don't really know how to resolve it, I would appreciate if you could help me with this.


回答1:


myMap.put("preload_buffer_size", 1024L);



回答2:


You are trying to put a String key with an Integer value (in its primitive form int) into a map which you designated as a String to Long map. So it only accepts

myMap.put(String, Long);

By adding an "L" to your number, the compiler will recognize it as being a Long instead of the default Integer.

so thatś why the following would be the solution:

myMap.put("preload_buffer_size", 1024L);



回答3:


The problem is that you are trying to place a numeric literal in a map having String as key, Long as value. By default in java the numerical literals are int's so either write:

 TreeMap <String, Long> myMap = new TreeMap <String, Long>();
 //populate the map
 myMap.put("preload_buffer_size", 1024L);
 myMap.put("net_buffer_length", 1024L);

or

TreeMap <String, Long> myMap = new TreeMap <String, Long>();
//populate the map
myMap.put("preload_buffer_size", new Long(1024));
myMap.put("net_buffer_length", new Long(1024));


来源:https://stackoverflow.com/questions/22079429/error-no-suitable-method-found-for-putstring-int

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!