在java中初始化字符串列表的最短方法是什么?

问题描述 投票:0回答:7

我正在寻找初始化字符串列表和字符串数组的最短方法(在代码中),即包含的列表/数组 “s1”、“s2”、“s3”字符串元素。

java arrays string list initialization
7个回答
119
投票

有多种选择。我个人喜欢使用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是最好的)。我很少真正
想要
一个可变但大小固定的列表。


38
投票

Java 9 引入了一种便捷方法

List.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(",");



15
投票
所有这些对象都存在于 JDK 中。

PS:

正如aioobe所述,这使得列表大小固定。


4
投票
JDK2

List<String> list = Arrays.asList("one", "two", "three");

JDK7

//diamond operator List<String> list = new ArrayList<>(); list.add("one"); list.add("two"); list.add("three");

JDK8

List<String> list = Stream.of("one", "two", "three").collect(Collectors.toList());

JDK9

List<String> list = List.of("one", "two", "three");

此外,Guava 等其他库还提供了许多其他方法。


3
投票
Arrays

类:

http://download.oracle.com/javase/6/docs/api/java/util/Arrays.html#asList(T
...) List<String> strings = Arrays.asList("s1", "s2", "s3");

请注意,结果列表是固定大小的(您无法添加)。


3
投票
Eclipse Collections

,您可以编写以下内容: 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 的提交者。


0
投票

  1. Arrays.asList

    方法返回一个固定大小的列表。

    
    众所周知,

  2. {{双括号初始化}}会造成类路径混乱并
  3. 减慢执行速度

    。它还需要每个元素一行代码。

  4. Java 9

    :静态工厂方法List.of

    返回结构上不可变的列表。而且,
    List.of
    基于值的
    ,因此它的合约不保证每次都会返回一个新对象。

这是一个 Java 8 单行代码,用于将多个单独的对象收集到一个
ArrayList

或任何与此相关的集合中。


List<String> list = Stream.of("s1", "s2", "s3").collect(Collectors.toCollection(ArrayList::new));

注意

:aioobe 复制临时集合的解决方案(new ArrayList<>(Arrays.asList("s1", "s2", "s3")))也很出色。

    

© www.soinside.com 2019 - 2024. All rights reserved.