英文:
Incompatible java.lang.Float and cannot be instantiated use MultiValueMap
问题
我是Java和Spring的新手。当我想在Spring中使用MultiValueMap时,它报告说MultiValueMap是抽象的,无法实例化。我的代码是这样的:
import java.util.*;
import org.springframework.util.MultiValueMap;
MultiValueMap<String, List<Float>> dataMap = new MultiValueMap<String, List<Float>>();
然后我将句子改为:
MultiValueMap<String, List<Float>> dataMap = new LinkedMultiValueMap<String, List<Float>>();
它报告说不兼容的java.util.List<java.lang.Float>无法转换为java.util.List<java.util.List<java.lang.Float>>。我正在使用jdku172和版本为5.2.2 RELEASE的Spring MVC库以及版本为5.2.2 RELEASE的Spring库。
英文:
I'm a new to Java and Spring. When I want to use the MultiValueMap in Spring it report is MultiValueMap is abstract and cannot be instantiated,My code is this:
import java.util.*;
import org.springframework.util.MutliValueMap;
MultiValueMap<String, List<Float>> dataMap = new MultiValueMap< String, List<Float>>();
and I change the sentence to
MultiValueMap<String, List<Float>> dataMap = new LinkedMultiValueMap<String, List<Float>>();
It report incompatible java.util.List<java.lang.Float> cannot transfer to java.util.List<java.util.List<java.lang.Float>>. I'm using the jdku172 and Spring MVC library of version 5.2.2 RELEASE Spring library of version 5.2.2RELEASE
答案1
得分: 1
如果您想将多个浮点值与单个键关联,您需要声明类型 MultiValueMap<String, Float>
。
类型 MultiValueMap
被定义为:
public interface MultiValueMap<K,V> extends Map<K,List<V>>
实例化方式:
MultiValueMap<String, Float> dataMap = new LinkedMultiValueMap<>();
然后:
dataMap.add("key1", 1.0f);
dataMap.add("key1", 1.1f);
dataMap.add("key1", 1.2f);
dataMap.add("key2", 2.0f);
dataMap.getFirst("key1"); // 1.0f
dataMap.get("key1"); // List.of("1.0f", "1.1f", "1.2f")
英文:
If you want to associate multiple floato values with a single key, you have to declare the type MultiValueMap<String, Float>
.
The type MultiValueMap
is defined as:
public interface MultiValueMap<K,V> extends Map<K,List<V>>
To instantiate:
MultiValueMap<String, Float> dataMap = new LinkedMultiValueMap<>();
then
dataMap.add("key1", 1.0f);
dataMap.add("key1", 1.1f);
dataMap.add("key1", 1.2f);
dataMap.add("key2", 2.0f);
dataMap.getFirst("key1"); // 1.0f
dataMap.get("key1"); // List.of("1.0f", "1.1f", "1.2f")
答案2
得分: 0
你需要修复你的包导入。以下是MultiValueMap
的示例用法:
import java.util.List;
import java.util.Map;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
public class MultiValueMapExample {
public static void main(String[] args) {
MultiValueMap<String, Float> map = new LinkedMultiValueMap<>();
map.add("Canon", 500.00f);
map.add("Canon", 501.00f);
map.add("Canon", 502.00f);
map.add("Nikon", 550.00f);
map.add("Nikon", 551.00f);
map.add("Nikon", 552.00f);
for (Map.Entry<String, List<Float>> e : map.entrySet()) {
System.out.println(e.getKey() + ": " + e.getValue());
}
}
}
英文:
You've to fix your package imports. Here is a an example usage of MultiValueMap
.
import java.util.List;
import java.util.Map;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
public class MultiValueMapExample {
public static void main(String[] args) {
MultiValueMap<String, Float> map = new LinkedMultiValueMap<>();
map.add("Canon", 500.00f);
map.add("Canon", 501.00f);
map.add("Canon", 502.00f);
map.add("Nikon", 550.00f);
map.add("Nikon", 551.00f);
map.add("Nikon", 552.00f);
for (Map.Entry<String, List<Float>> e : map.entrySet()) {
System.out.println(e.getKey() + ": " + e.getValue());
}
}
}
答案3
得分: 0
MultiValueMap<String, List<Float>> dataMap = new LinkedMultiValueMap<String, List<Float>>();
这是正确的。尝试使用IntelliJ Idea和Spring Boot。它将管理正确的jdk依赖和Spring工具。这行没有任何问题。
英文:
MultiValueMap<String, List<Float>> dataMap = new LinkedMultiValueMap<String, List<Float>>();
This is right. Try using IntelliJ Idea and Spring Boot. That will manage the correct jdk dependency and spring utilities. There is nothing wrong with this line.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论