Java Guava ImmutableMap Builder 语法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21921507/
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-08-13 11:24:55  来源:igfitidea点击:

Guava ImmutableMap Builder syntax

javaguavaimmutability

提问by Durandal

I've been using Guava's ImmutableMapwith a Builderfor awhile without giving much thought to how/why it works. I've been using the builder in the way described by the Javadoc:

我一直在使用番石榴的已经ImmutableMapBuilder一段时间没有过多考虑如何/为什么它的工作原理。我一直在以Javadoc描述的方式使用构建器:

ImmutableMap<String, Integer> WORD_TO_INT =
          new ImmutableMap.Builder<String, Integer>()
              .put("one", 1)
              .put("two", 2)
              .put("three", 3)
              .build();

I've also seen examples elsewhereon SO of this syntax:

我还在其他地方看到过这种语法的例子:

ImmutableMap<String,Integer> myMap = ImmutableMap.<String, Integer>builder()
    .put("one", 1) 
    .put("two", 2) 
    .put("three", 3) 
    .build();

Both seem to produce the exact same result.

两者似乎产生完全相同的结果。

A quick glance at the ImmutableMapsourceshows that the static builder()call in the 2nd example returns: new Builder<K, V>()

快速浏览一下ImmutableMap源代码表明builder()第二个示例中的静态调用返回:new Builder<K, V>()

There doesn't appear to be any functional difference between the two methods.

这两种方法之间似乎没有任何功能差异。

Is there any difference under the hood? Any reason to prefer one or the other?

引擎盖下有什么区别吗?有什么理由选择其中之一?

EDIT: Adding bytecode difference.

编辑:添加字节码差异。

The bytecode generated is almost identical except for one line:

除了一行之外,生成的字节码几乎相同:

Method 1(Builder<K,V>):

方法一(Builder<K,V>):

static {};
     0  new com.google.common.collect.ImmutableMap$Builder [12]
     3  dup
     4  invokespecial com.google.common.collect.ImmutableMap$Builder() [14]
     7  ldc <String "key1"> [17]
     ........

Method 2:(<K,V>builder())

方法二:( <K,V>builder())

static {};
     0  invokestatic com.google.common.collect.ImmutableMap.builder() : com.google.common.collect.ImmutableMap$Builder [12]
     3  ldc <String "key1"> [18]
     ........

After that they're pretty much the same, the first line being different makes sense given the source differences, but I'm not sure why the first one calls dupand the second doesn't.

之后它们几乎相同,考虑到源代码的差异,第一行不同是有道理的,但我不确定为什么第一个调用dup而第二个没有。

采纳答案by Boris the Spider

No, they are identical. The reason for the first syntax is that it makes a nice one-liner.

不,它们是相同的。第一种语法的原因是它是一个很好的单行。

The reason for the second syntax is the you might have a loop or other flow control, you may need to pass the Builderaround.

第二种语法的原因是你可能有一个循环或其他流控制,你可能需要传递Builder

For example:

例如:

public static void main(final String[] args) throws Exception {
    final ImmutableList.Builder<Integer> lb = ImmutableList.builder();
    for (final String arg : args) {
        lb.add(Integer.parseInt(arg));
    }
}

And before you say it, yes a FluentIterablewould probably be better in this case - but it's just an example.

在你说之前,是的FluentIterable,在这种情况下可能会更好 - 但这只是一个例子。

回答by Guy Bouallet

As stated in the javadoc, The generated builder is equivalent to the builder created by the ImmutableMap.Builder constructor.

javadoc中所述,生成的构建器等效于由 ImmutableMap.Builder 构造函数创建的构建器。

It is a good practice to write such a method when applying the Builder pattern. This offers a fluent way to use it and can be considered as a "syntactic sugar".

在应用构建器模式时编写这样的方法是一个很好的做法。这提供了一种流畅的使用方式,可以被视为一种“语法糖”。