【问题标题】:What is the shortest way to initialize List of strings in java?在java中初始化字符串列表的最短方法是什么?
【发布时间】:2011-09-25 02:39:16
【问题描述】:

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

【问题讨论】:

    标签: java arrays string list initialization


    【解决方案1】:

    有多种选择。我个人喜欢使用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,它看起来有点奇怪。

    另一个番石榴选项,如果您不想要一个可以以任何方式修改的列表:

    ImmutableList<String> strings = ImmutableList.of("s1", "s2", "s3");
    

    我通常希望要么有一个完全可变的列表(在这种情况下Lists.newArrayList 是最好的)一个完全不可变的列表(在这种情况下ImmutableList.of 是最好的)。我很少真正想要一个可变但固定大小的列表。

    【讨论】:

    • 为什么不只是 Arrays.asList("s1", "s2", "s3")?
    • @BegemoT:我补充说 - 我个人更喜欢 Guava 版本的明确性质。
    • which is unmodifiable -- 它实际上是可修改的(但大小固定)。
    • @Matt:没问题。我会删除回复你的,然后尽快删除 this 一个,如果你也想删除你的第二个:)
    • 对 Jon Skeet 表示敬意。用 Arrays.asList() 代替 ImmutableList.of() 不是更好吗,因为后面会维护复制的数组。你能分享一下这个吗?
    【解决方案2】:
    List<String> stringList = Arrays.asList("s1", "s2", "s3");
    

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

    PS:正如aioobe 所说,这使得列表大小固定。

    【讨论】:

    • 结果列表是固定大小的。
    【解决方案3】:

    您可以在标准 Java API 中使用Arrays 类:http://download.oracle.com/javase/6/docs/api/java/util/Arrays.html#asList(T...)

    List<String> strings = Arrays.asList("s1", "s2", "s3");
    

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

    【讨论】:

      【解决方案4】:

      Java 9+

      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" }
      

      如果字符串常量是可能的,它看起来像

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

      【讨论】:

      • @精英绅士:Arrays.asList 的结果是不可变的。如果在str2 中构造了一个新的 ArrayList,那么之后可以添加到列表中。
      • @Mathias Schwarz,不是一成不变的,固定大小的。
      • @aioobe:你是对的!可以覆盖现有条目。
      • 您应该注意,初始化块变体创建了 ArrayList 类的匿名子类。
      【解决方案5】:

      我对 Guava 不熟悉,但几乎所有其他答案中提到的解决方案和使用 JDK 的解决方案都存在一些缺陷:

      1. Arrays.asList 方法返回一个固定大小的列表。

      2. {{双括号初始化}} 已知会造成类路径混乱和slow down execution。它还需要每个元素一行代码。

      3. Java 9:静态工厂方法List.of返回结构上不可变的列表。而且List.of就是value-based,所以它的契约不保证每次都会返回一个新的对象。


      这是一个 Java 8 单行代码,用于将多个单独的对象收集到 ArrayList 或任何相关的集合中。

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

      注意:aioobe 的复制临时集合 (new ArrayList&lt;&gt;(Arrays.asList("s1", "s2", "s3"))) 的解决方案也很出色。

      【讨论】:

        【解决方案6】:

        使用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");
        

        你也可以对套装、包和地图做同样的事情:

        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 的提交者。

        【讨论】:

          【解决方案7】:

          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 等其他库还提供了许多其他方法。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2010-09-17
            • 2021-11-24
            • 1970-01-01
            • 1970-01-01
            • 2013-12-30
            • 1970-01-01
            相关资源
            最近更新 更多