不兼容的 java.lang.Float,无法实例化,请使用 MultiValueMap。

huangapple go评论78阅读模式
英文:

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&lt;String, List&lt;Float&gt;&gt; dataMap = new MultiValueMap&lt; String, List&lt;Float&gt;&gt;();

and I change the sentence to

 MultiValueMap&lt;String, List&lt;Float&gt;&gt; dataMap = new LinkedMultiValueMap&lt;String, List&lt;Float&gt;&gt;();

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&lt;String, Float&gt;

类型 MultiValueMap 被定义为

public interface MultiValueMap&lt;K,V&gt; extends Map&lt;K,List&lt;V&gt;&gt;

实例化方式:

MultiValueMap&lt;String, Float&gt; dataMap = new LinkedMultiValueMap&lt;&gt;();

然后:

dataMap.add(&quot;key1&quot;, 1.0f);
dataMap.add(&quot;key1&quot;, 1.1f);
dataMap.add(&quot;key1&quot;, 1.2f);
dataMap.add(&quot;key2&quot;, 2.0f);
dataMap.getFirst(&quot;key1&quot;); // 1.0f
dataMap.get(&quot;key1&quot;); // List.of(&quot;1.0f&quot;, &quot;1.1f&quot;, &quot;1.2f&quot;)
英文:

If you want to associate multiple floato values with a single key, you have to declare the type MultiValueMap&lt;String, Float&gt;.

The type MultiValueMap is defined as:

public interface MultiValueMap&lt;K,V&gt; extends Map&lt;K,List&lt;V&gt;&gt;

To instantiate:

MultiValueMap&lt;String, Float&gt; dataMap = new LinkedMultiValueMap&lt;&gt;();

then

dataMap.add(&quot;key1&quot;, 1.0f);
dataMap.add(&quot;key1&quot;, 1.1f);
dataMap.add(&quot;key1&quot;, 1.2f);
dataMap.add(&quot;key2&quot;, 2.0f);
dataMap.getFirst(&quot;key1&quot;); // 1.0f
dataMap.get(&quot;key1&quot;); // List.of(&quot;1.0f&quot;, &quot;1.1f&quot;, &quot;1.2f&quot;)

答案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&lt;String, Float&gt; map = new LinkedMultiValueMap&lt;&gt;();
        map.add(&quot;Canon&quot;, 500.00f);
        map.add(&quot;Canon&quot;, 501.00f);
        map.add(&quot;Canon&quot;, 502.00f);

        map.add(&quot;Nikon&quot;, 550.00f);
        map.add(&quot;Nikon&quot;, 551.00f);
        map.add(&quot;Nikon&quot;, 552.00f);

        for (Map.Entry&lt;String, List&lt;Float&gt;&gt; e : map.entrySet()) {
            System.out.println(e.getKey() + &quot;: &quot; + 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.

huangapple
  • 本文由 发表于 2020年8月29日 06:42:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63641708.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定