本文介绍了Lists.newArrayList vs new ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
创建字符串 List 的最佳构造是什么?它是 Lists.newArrayList()(来自guava)或 new ArrayList()?
What is the best construction for creating a List of Strings? Is it Lists.newArrayList() (from guava) or new ArrayList()?
只是个人偏好吗?
或者只是输入泛型类型推断?
或者在使用列表时是否有任何理论或实用价值? newArrayList()?
is it just a personal preference?
or is it just Type generic type inference?
or is there any theoretical or practical value in using Lists.newArrayList()?
推荐答案
guava生成器可以多次保存输入类型参数。比较:
The guava builder saves typing the type arguments multiple times. Compare:
List<Foo<Bar, Baz>> list = Lists.newArrayList(); List<Foo<Bar, Baz>> list = new ArrayList<Foo<Bar, Baz>>();
在Java 7中,它已经过时了,因为你有钻石操作符:
In Java 7 it's a bit obsolete though, because you have the diamond operator:
List<Foo<Bar, Baz>> list = new ArrayList<>();
这篇关于Lists.newArrayList vs new ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!