Map.newHashMap返回的HashMap与新的HashMap [英] HashMap returned by Maps.newHashMap vs new HashMap

查看:2937
本文介绍了Map.newHashMap返回的HashMap与新的HashMap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我第一次尝试番石榴,我发现它真的很棒。



我正在Spring jdbc模板上执行一些参数化的检索查询。 DAO中的方法( AbstractDataAccessObject )就是这样。这里没有问题。

  public Map< String,Object> getResultAsMap(String sql,Map< String,Object> parameters){
try {
return jdbcTemplate.queryForMap(sql,parameters);
} catch(EmptyResultDataAccessException e){
//如果没有为此查询找到数据,忽略
logger.error(e.getMessage(),e);

}
return null;
}

这里有问题:



当我使用

  getResultAsMap(query,new HashMap< String,Object>(ImmutableMap.of gciList,gciList))); 

它工作得很好。



但是当我这样做

  getResultAsMap newHashMap(ImmutableMap.of(gciList,gciList))); 

编译器生气了



code> AbstractDataAccessObject类型中的方法getResultAsMap(String,Map< String,Object>)不适用于参数(String,HashMap< String,List< String>>) / p>

我做错了什么或可能是此投诉的原因?

解决方案

这是类型推断失败。 Maps.newHashMap 是一个静态参数化方法。它允许您使用

  Map< String,Integer> map = Maps.newHashMap()

而不是

  Map< String,Integer> map = new HashMap< String,Integer>()

c>< String,Integer> 两次。在Java 7中,钻石操作符允许您使用

  Map< String,Integer> map = new HashMap<>()

b
$ b

要回答你的问题,只需使用新的HashMap 版本,因为类型推理不适用于方法参数。 (您可以使用 Maps。< String,Object> newHashMap()但是无法使用方法)


I am trying Guava for the first time and I find it really awesome.

I am executing few parameterized retrieve queries on a Spring jdbc template. The method in the DAO (AbstractDataAccessObject) goes like this. No problem here.

public Map<String,Object> getResultAsMap(String sql, Map<String,Object> parameters) {
    try {
        return jdbcTemplate.queryForMap(sql, parameters);
    } catch (EmptyResultDataAccessException e) {
        //Ignore if no data found for this query
        logger.error(e.getMessage(), e);

    }
    return null;
}

Here's the problem :

When I call this method using

getResultAsMap(query, new HashMap<String,Object>(ImmutableMap.of("gciList",gciList)));

it works great.

But when I do this

getResultAsMap(query, Maps.newHashMap(ImmutableMap.of("gciList",gciList)));

the compiler gets upset saying

The method getResultAsMap(String, Map<String,Object>) in the type AbstractDataAccessObject is not applicable for the arguments (String, HashMap<String,List<String>>)

Am I doing something wrong or what could be the reason for this complaint?

解决方案

This is type inference failing. Maps.newHashMap is a static parameterized method. It allows you to use

Map<String,Integer> map = Maps.newHashMap()

instead of

Map<String,Integer> map = new HashMap<String,Integer>()

saving you from having to type <String,Integer> twice. In Java 7, the diamond operator allows you to use

Map<String,Integer> map = new HashMap<>()

so the method is then redundant.

To answer your question, just use the new HashMap version, since type inference doesn't work for method parameters. (You could use Maps.<String,Object>newHashMap() but that defeats the point of using the method)

这篇关于Map.newHashMap返回的HashMap与新的HashMap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆