我正在寻找初始化字符串列表和字符串数组的最短方法(在代码中),即包含的列表/数组 “s1”、“s2”、“s3”字符串元素。
有多种选择。我个人喜欢使用Guava:
List<String> strings = Lists.newArrayList("s1", "s2", "s3");
(当然,Guava 是一个值得拥有的库:)
仅使用 JDK,您可以使用:
List<String> strings = Arrays.asList("s1", "s2", "s3");
请注意,这将返回一个
ArrayList
,但这不是 正常的 java.util.ArrayList
- 它是一个内部的,可变但大小固定。我个人更喜欢 Guava 版本,因为它清楚地表明发生了什么(将返回的列表实现)。如果您静态导入该方法,也
仍然清楚发生了什么:
// import static com.google.common.collect.Lists.newArrayList;
List<String> strings = newArrayList("s1", "s2", "s3");
...而如果你静态导入
asList
它看起来有点奇怪。另一个 Guava 选项,如果您不想以任何方式修改列表:
ImmutableList<String> strings = ImmutableList.of("s1", "s2", "s3");
我通常希望
有一个完全可变的列表(在这种情况下Lists.newArrayList
是最好的)
或有一个完全不可变的列表(在这种情况下
ImmutableList.of
是最好的)。我很少真正想要一个可变但大小固定的列表。
,用法如下:
List<String> l = List.of("s1", "s2", "s3");
Java 8 及更早版本
// Short, but the resulting list is fixed size.
List<String> list1 = Arrays.asList("s1", "s2", "s3");
// Similar to above, but the resulting list can grow.
List<String> list2 = new ArrayList<>(Arrays.asList("s1", "s2", "s3"));
// Using initialization block. Useful if you need to "compute" the strings.
List<String> list3 = new ArrayList<String>() {{
add("s1");
add("s2");
add("s3");
}};
String[] arr = { "s1", "s2", "s3" };
如果您需要重新初始化它或创建它而不将其存储在变量中,您可以这样做
new String[] { "s1", "s2", "s3" }
如果字符串常量是 may,那么它看起来像
String[] arr = { "s1", "s2", "s3", "s4", "s5", "s6", "s7", "s8", "s9", "s10",
"s11", "s12", "s13" };
在这些情况下我通常更喜欢写作
String[] arr = "s1,s2,s3,s4,s5,s6,s7,s8,s9,s10,s11,s12,s13".split(",");
List<String> list = Arrays.asList("one", "two", "three");
//diamond operator
List<String> list = new ArrayList<>();
list.add("one");
list.add("two");
list.add("three");
List<String> list = Stream.of("one", "two", "three").collect(Collectors.toList());
List<String> list = List.of("one", "two", "three");
此外,Guava 等其他库还提供了许多其他方法。
,您可以编写以下内容:
List<String> list = Lists.mutable.with("s1", "s2", "s3");
您还可以更具体地了解类型以及它们是可变的还是不可变的。
MutableList<String> mList = Lists.mutable.with("s1", "s2", "s3");
ImmutableList<String> iList = Lists.immutable.with("s1", "s2", "s3");
您也可以对
Sets
、
Bags
和 Maps
执行相同操作:Set<String> set = Sets.mutable.with("s1", "s2", "s3");
MutableSet<String> mSet = Sets.mutable.with("s1", "s2", "s3");
ImmutableSet<String> iSet = Sets.immutable.with("s1", "s2", "s3");
Bag<String> bag = Bags.mutable.with("s1", "s2", "s3");
MutableBag<String> mBag = Bags.mutable.with("s1", "s2", "s3");
ImmutableBag<String> iBag = Bags.immutable.with("s1", "s2", "s3");
Map<String, String> map =
Maps.mutable.with("s1", "s1", "s2", "s2", "s3", "s3");
MutableMap<String, String> mMap =
Maps.mutable.with("s1", "s1", "s2", "s2", "s3", "s3");
ImmutableMap<String, String> iMap =
Maps.immutable.with("s1", "s1", "s2", "s2", "s3", "s3");
还有
SortedSets
、
SortedBags
和SortedMaps
的工厂。SortedSet<String> sortedSet = SortedSets.mutable.with("s1", "s2", "s3");
MutableSortedSet<String> mSortedSet = SortedSets.mutable.with("s1", "s2", "s3");
ImmutableSortedSet<String> iSortedSet = SortedSets.immutable.with("s1", "s2", "s3");
SortedBag<String> sortedBag = SortedBags.mutable.with("s1", "s2", "s3");
MutableSortedBag<String> mSortedBag = SortedBags.mutable.with("s1", "s2", "s3");
ImmutableSortedBag<String> iSortedBag = SortedBags.immutable.with("s1", "s2", "s3");
SortedMap<String, String> sortedMap =
SortedMaps.mutable.with("s1", "s1", "s2", "s2", "s3","s3");
MutableSortedMap<String, String> mSortedMap =
SortedMaps.mutable.with("s1", "s1", "s2", "s2", "s3","s3");
ImmutableSortedMap<String, String> iSortedMap =
SortedMaps.immutable.with("s1", "s1", "s2", "s2", "s3","s3");
注意:我是 Eclipse Collections 的提交者。
Arrays.asList
方法返回一个固定大小的列表。
众所周知,返回结构上不可变的列表。而且,
List.of
是基于值的,因此它的合约不保证每次都会返回一个新对象。
ArrayList
或任何与此相关的集合中。
List<String> list = Stream.of("s1", "s2", "s3").collect(Collectors.toCollection(ArrayList::new));
注意:aioobe 复制临时集合的解决方案(new ArrayList<>(Arrays.asList("s1", "s2", "s3"))
)也很出色。