简体   繁体   English

错误:找不到适合put(String,int)的方法

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

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. 我需要使用Long,而不是int我真的不知道如何解决它,如果您能帮助我,我将不胜感激。

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

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. 您正在尝试将具有Integer值的String键(其原始形式为int)放入指定为String to Long映射的映射中。 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. 通过在您的数字上添加“ L”,编译器将识别它为Long而不是默认的Integer。

so thatś why the following would be the solution: 因此,以下原因将是解决方案:

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

The problem is that you are trying to place a numeric literal in a map having String as key, Long as value. 问题是您试图将数字文字放置在具有String作为键, Long作为值的映射中。 By default in java the numerical literals are int 's so either write: 在Java中,默认情况下,数字文字是int ,所以可以这样写:

 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));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM