java 无法推断 hashmap<> 的类型参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47449635/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 09:37:37  来源:igfitidea点击:

Cannot infer type arguments for hashmap<>

javajava-8hashmapjava-stream

提问by Chaitanya Uttarwar

I am getting

我正进入(状态

   Cannot infer type arguments for java.util.HashMap<>

for the following code

对于以下代码

class Test {

    public static void main(String[] args) {

        Map<Integer, String> map = new HashMap<>();
        map.put(1, "x");
        map.put(2, "y");
        map.put(3, "x");
        map.put(4, "z");

  //the following line has error
        Map<String, ArrayList<Integer>> reverseMap = new java.util.HashMap<>(map.entrySet().stream()
                .collect(Collectors.groupingBy(Map.Entry::getValue)).values().stream()
                .collect(Collectors.toMap(item -> item.get(0).getValue(),
                        item -> new ArrayList<>(item.stream().map(Map.Entry::getKey).collect(Collectors.toList())))); 
        System.out.println(reverseMap);

    }

}

What went wrong and Can anyone Explain me this ? I have checked for proper imports and found out that I am importing java.util.hashmap and none other. Still the pesky error is buging me.

出了什么问题,谁能给我解释一下?我检查了正确的导入,发现我正在导入 java.util.hashmap 而不是其他。讨厌的错误仍然困扰着我。

The Error Persists

错误仍然存​​在

回答by Eugene

That's a bug in ecj(eclipse compiler), you can work around it and add more type information :

这是ecj(eclipse 编译器)中的一个错误,您可以解决它并添加更多类型信息:

item -> new ArrayList<Integer>(item.stream().map(Entry::getKey)

See how I've added ArrayList<Integer>.

看看我是如何添加的ArrayList<Integer>

It compiles fine with javac-8 and 9.

它编译得很好javac-8 and 9

And btw seems like there is a simpler way to do things:

顺便说一句,似乎有一种更简单的做事方法:

map.entrySet()
            .stream()
            .collect(Collectors.groupingBy(
                    Entry::getValue,
                    HashMap::new,
                    Collectors.mapping(Entry::getKey, Collectors.toList())));

回答by zee

In my case, the error disappeared after adding import java.util.Map;:

就我而言,添加后错误消失了 import java.util.Map;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import com.fasterxml.Hymanson.databind.ObjectMapper;


    public void saveFooOrder(Foo foo, long orderId) {

         Map<String, Object> values = new HashMap<>();
                                         /*^^^^ Error was here: Cannot 
                                                infer type arguments for HashMap<>*/
            values.put("fooOrder", orderId);
            values.put("foo", foo.getId());
            orderFooInserter.execute(values);   
    }

回答by ΦXoc? ? Пepeúpa ツ

your code is not completed, and you are missing a )

您的代码未完成,并且您缺少一个 )

try doing:

尝试做:

import java.util.*;
import java.util.stream.Collectors;
public class HelloWorld{

     public static void main(String []args){
        Map<Integer, String> map = new HashMap<>();
        map.put(1, "x");
        map.put(2, "y");
        map.put(3, "x");
        map.put(4, "z");

        Map<String, ArrayList<Integer>> reverseMap = new java.util.HashMap<>(map.entrySet().stream()
                .collect(Collectors.groupingBy(Map.Entry::getValue)).values().stream()
                .collect(Collectors.toMap(item -> item.get(0).getValue(),
                        item -> new ArrayList<>(item.stream().map(Map.Entry::getKey).collect(Collectors.toList())))));
        System.out.println(reverseMap);
     }
}

this produce the output

这产生输出

{x=[1, 3], y=[2], z=[4]}