【问题标题】:Cannot infer type arguments for hashmap<>无法推断 hashmap<> 的类型参数
【发布时间】:2018-05-07 01:45:46
【问题描述】:

我来了

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

以下代码

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

    }

}

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

【问题讨论】:

    标签: java java-8 hashmap java-stream


    【解决方案1】:

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

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

    看看我是如何添加ArrayList&lt;Integer&gt;的。

    它与javac-8 and 9 编译得很好。

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

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

    【讨论】:

    • 确实,IntelliJ IDEA 编译原代码没有错误。
    • 我提交了a bug against ecj,您可以按照流程进行修复。
    【解决方案2】:

    在我的情况下,添加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.jackson.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);   
        }
    

    【讨论】:

      【解决方案3】:

      您的代码未完成,您缺少)

      尝试做:

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

      这会产生输出

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-06
        • 2014-08-07
        相关资源
        最近更新 更多