简体   繁体   English

关于map函数的方法参考,当键的类型为String时编译错误

[英]Method reference on map function, compilation error when the key is of type String

Context : 背景

I want to use the function computeIfAbsent on a Map . 我想在Map上使用computeIfAbsent函数。 But, I get a compilation error when I use 但是,当我使用时,我收到编译错误

  • method reference and the key is a String . 方法引用 ,键是String

I get no compilation error when I use 我使用时没有编译错误

  • method reference and the key is an Integer . 方法引用 ,键是Integer
  • lambda and the key is a String . lambda和key是一个String

Illustration : 插图

The following statements are legal : 以下陈述是合法的

Map<Integer, List<Long>> map = new HashMap<>();
Integer key = Integer.valueOf(0);
Long value = Long.valueOf(2);
map.computeIfAbsent(key, ArrayList::new).add(value); // No compilation error

The following statements are illegal : 以下陈述是非法的

Map<String, List<Long>> map = new HashMap<>();
String key = "myKey";
Long value = Long.valueOf(2);
map.computeIfAbsent(key, ArrayList::new).add(value); // Compilation error: The type ArrayList does not define ArrayList(String) that is applicable here

The following statements are legal : 以下陈述是合法的

Map<String, List<Long>> map = new HashMap<>();
String key = "myKey";
Long value = Long.valueOf(2);
map.computeIfAbsent(key, x -> new ArrayList<>()).add(value); // No compilation error

Question : I don't get why a String as key is that special when combined with method reference. 问题 :当结合方法引用时,我不明白为什么String作为键是特殊的。 Any idea? 任何的想法?

When you call ArrayList::new instead of x -> new ArrayList<>() it's equals to call x -> new ArrayList<>(x) . 当你调用ArrayList::new而不是x -> new ArrayList<>()它等于调用x -> new ArrayList<>(x)

Method computeIfAbsent requires lambda expression with one lambda parameter as second input argument, or a reference to method that consumes one argument of String type. 方法computeIfAbsent需要lambda表达式,其中一个lambda参数作为第二个输入参数,或者对使用String类型的一个参数的方法的引用。

Your error 你的错误

Compilation error: The type ArrayList does not define ArrayList(String) that is applicable here 编译错误:类型ArrayList未定义此处适用的ArrayList(String)

is talking: you trying to call constructor with one String argument . 正在谈论: you trying to call constructor with one String argument Because, as I told above, lambda x -> someObject.method(x) is equal to someObject::method . 因为,正如我上面所说,lambda x -> someObject.method(x)等于someObject::method Or lambda x -> new SomeClass(x) is equal to SomeClass::new . 或者lambda x -> new SomeClass(x)等于SomeClass::new

You can't use method (constructor) reference here, because here required method (constructor) that consumes one parameter, or a lambda expression. 你不能在这里使用方法(构造函数)引用,因为这里需要使用一个参数或一个lambda表达式的方法(构造函数)。 If there was lambda without any parameters, you will be able to call empty constructor. 如果有lambda没有任何参数,你将能够调用空构造函数。

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

相关问题 使用类型集合时编译错误对方法的模糊引用 - Compilation error ambiguous reference to method when using typed collection 如何将String映射到未知返回类型的java方法引用? - How to map a String to a java method reference for unknown return type? 将Factory和方法一起使用进行类型捕获时的编译错误 - Compilation error when using together a Factory and a method doing type capture 使用类型参数覆盖泛型方法时的编译错误 - Compilation error when overriding a generic method with a type parameter 在Java 8中使用方法引用时出现编译错误 - Compilation error while using Method reference in Java 8 Java:具有泛型函数参考的编译错误 - Java: compilation error with generic function reference 错误:对put的引用不明确,JSONObject中的方法put(String,Collection)和JSONObject中的方法put(String,Map)都匹配 - error: reference to put is ambiguous, both method put(String,Collection) in JSONObject and method put(String,Map) in JSONObject match 无法在主要方法中引用我的方法,出现编译错误 - Cannot reference my method in the main method with compilation error 使用字符串键和对象引用值创建地图 - create map with string key and object reference value 通配符类型不匹配(int)编译错误,但不是(String) - Wildcard Type Mismatch (int) compilation error, but not (String)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM