返回一个 ImmutableMap
我有一个返回地图的方法。我最初会返回该方法生成的 HashMap,但认为返回 ImmutableMap 会更好。不幸的是,以下语句拒绝在 Eclipse 中工作:
HashMap<File, File> map = new HashMap<File, File>();
map.put(...);
.
.
.
return ImmutableMap.builder ().putAll (map).build ();
它一直说我正在返回一个不兼容的语句,即 Map
。
我最初尝试使用:
return ImmutableMap<File, File>.builder ().putAll (map).build ();
但这显然不起作用。我最好如何解决这个问题?我应该首先将其存储在类似的东西中
ImmutableMap<File, File> m = ImmutableMap.builder ().putAll (map).build ();
还是有更优雅的解决方案?
I have a method that returns a Map. I would initially return the HashMap that the method generated, but thought it would be better to return an ImmutableMap. Unfortunately, the following statement refuses to work in eclipse:
HashMap<File, File> map = new HashMap<File, File>();
map.put(...);
.
.
.
return ImmutableMap.builder ().putAll (map).build ();
It keeps saying that I'm returning an incompatible statement, a Map<Object, Object>
.
I initially tried to use:
return ImmutableMap<File, File>.builder ().putAll (map).build ();
but that obviously didn't work. How would I best go about fixing this? Should I first store it in something like
ImmutableMap<File, File> m = ImmutableMap.builder ().putAll (map).build ();
or is there a more elegant solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正确的语法是
注意点位于泛型之前,因为泛型属于方法调用,而不是类。
正如 Bozho 所指出的,在调用泛型方法时需要指定泛型类型。有时,如果编译器可以推断它们,则不需要。但类型推断非常有限,通常仅适用于方法参数,例如
copyOf
方法:该方法是泛型的,但编译器可以从方法参数推断泛型类型。更新: Gabriel 建议将语句拆分为多行,如下所示:
这避免了需要使用显式类型参数来交换新的局部变量,恕我直言,在这种情况下,这不会增加可读性。另一方面,如果您多次添加到构建器,我更喜欢局部变量而不是长调用链。在只有一次
putAll
调用的特殊情况下,copyOf
反而提供了在一行中避免类型参数的好处。The correct syntax would be
Note that the dot is before the generics, because the generics belong to the method invocation, not the class.
As Bozho noted, you need to specify the generic types when invoking generic methods. Sometimes, you don't need to, if the compiler can infer them. But the type inference is very limited, and usually only works with method arguments, like for the
copyOf
method: That method is generic, but the compiler can infer the generic types from the method argument.UPDATE : Gabriel suggested to split the statement in multiple lines, like so:
This avoids the need for the explicit type parameters in exchange for a new local variable, which IMHO does not add to readability in this case. On the other hand, if you add to the builder several times, I'd prefer the local variable over a long call chain. In the special case of only one
putAll
call, acopyOf
instead gives the benefit of avoided type parameters in a one-liner.ImmutableMap.copyOf(map)
应该可以。根据您的要求,
Collections.unmodifyingMap(map)
可能也适合您。不同之处在于,不可变映射是原始映射的副本,而不可修改映射是原始映射的视图,如果原始映射发生变化,视图也会发生变化。ImmutableMap.copyOf(map)
should do.Depending on your requirements,
Collections.unmodifiableMap(map)
may also fit you. The difference is that the immutable map is a copy of the original map, while the unmodifiable map is a view of the original, and if the original changes, the view will also change.