返回一个 ImmutableMap

发布于 2024-11-28 02:38:23 字数 618 浏览 0 评论 0原文

我有一个返回地图的方法。我最初会返回该方法生成的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

赏烟花じ飞满天 2024-12-05 02:38:23

正确的语法是

return ImmutableMap.<File, File> builder().putAll(map).build();

注意点位于泛型之前,因为泛型属于方法调用,而不是类。

正如 Bozho 所指出的,在调用泛型方法时需要指定泛型类型。有时,如果编译器可以推断它们,则不需要。但类型推断非常有限,通常仅适用于方法参数,例如 copyOf 方法:该方法是泛型的,但编译器可以从方法参数推断泛型类型。

更新: Gabriel 建议将语句拆分为多行,如下所示:

Builder<File, File> builder = ImmutableMap.builder();
builder.putAll(map);
return builder.build();

这避免了需要使用显式类型参数来交换新的局部变量,恕我直言,在这种情况下,这不会增加可读性。另一方面,如果您多次添加到构建器,我更喜欢局部变量而不是长调用链。在只有一次 putAll 调用的特殊情况下,copyOf 反而提供了在一行中避免类型参数的好处。

The correct syntax would be

return ImmutableMap.<File, File> builder().putAll(map).build();

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:

Builder<File, File> builder = ImmutableMap.builder();
builder.putAll(map);
return builder.build();

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, a copyOf instead gives the benefit of avoided type parameters in a one-liner.

站稳脚跟 2024-12-05 02:38:23

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文