本文翻译自:How to make a new List in Java

We create a Set as: 我们创建一个Set为:

Set myset = new HashSet()

How do we create a List in Java? 我们如何在Java中创建List


#1楼

参考:https://stackoom.com/question/3bLw/如何在Java中创建一个新的List


#2楼

Using Google Collections , you could use the following methods in the Lists class 使用Google Collections ,您可以在Lists类中使用以下方法

import com.google.common.collect.Lists;// ...List<String> strings = Lists.newArrayList();List<Integer> integers = Lists.newLinkedList();

There are overloads for varargs initialization and initialising from an Iterable<T> . varargs初始化和从Iterable<T>初始化有重载。

The advantage of these methods is that you don't need to specify the generic parameter explicitly as you would with the constructor - the compiler will infer it from the type of the variable. 这些方法的优点是您不需要像使用构造函数那样显式地指定泛型参数 - 编译器将根据变量的类型推断它。


#3楼

List<Object> nameOfList = new ArrayList<Object>();

您需要导入ListArrayList


#4楼

Let me summarize and add something: 让我总结并添加一些内容:

JDK JDK

1. new ArrayList<String>();
2. Arrays.asList("A", "B", "C")

Guava 番石榴

1. Lists.newArrayList("Mike", "John", "Lesly");
2. Lists.asList("A","B", new String [] {"C", "D"});

Immutable List 不变的清单

1. Collections.unmodifiableList(new ArrayList<String>(Arrays.asList("A","B")));
2. ImmutableList.builder()                                      // Guava.add("A").add("B").build();
3. ImmutableList.of("A", "B");                                  // Guava
4. ImmutableList.copyOf(Lists.newArrayList("A", "B", "C"));     // Guava

Empty immutable List 空不可变列表

1. Collections.emptyList();
2. Collections.EMPTY_LIST;

List of Characters 字符列表

1. Lists.charactersOf("String")                                 // Guava
2. Lists.newArrayList(Splitter.fixedLength(1).split("String"))  // Guava

List of Integers 整数列表

Ints.asList(1,2,3);                                             // Guava

#5楼

As an option you can use double brace initialization here: 作为选项,您可以在此处使用双括号初始化:

List<String> list = new ArrayList<String>(){{add("a");add("b");}
};

#6楼

List arrList = new ArrayList();

Its better you use generics as suggested below: 它更好地使用泛型,如下所示:

List<String> arrList = new ArrayList<String>();arrList.add("one");

Incase you use LinkedList. 如果你使用LinkedList。

List<String> lnkList = new LinkedList<String>();

如何在Java中创建一个新的List相关推荐

  1. Java乐谱_如何在java中创建一个简单但结构良好的乐谱表(乐谱)?

    我正在使用非常基本的声音合成在我的游戏中创建音频和效果.基本上,我有一些方法可以发出一个频率和频率的声音.幅度和幅度持续时间. 对于短语和旋律,我想提出一个基本的符号,这样我就可以轻松地重写或添加新的 ...

  2. java创建临时文件夹_如何在Java中创建一个临时目录/文件夹?

    有没有在Java应用程序内部创建临时目录的标准可靠方法? Java的问题数据库中有一个条目,注释中包含一些代码,但是我想知道在一个常用的库(Apache Commons等)中是否找到标准解决方案? # ...

  3. java在未来_如何在java中创建一个完整的未来

    在java中构建完成的未来的最好方法是什么?我已经实现了我自己的CompletedFuture下面,但希望像这样的东西已经存在. public class CompletedFuture implem ...

  4. java怎么添加到按钮组_如何在Java中创建一个带有连接按钮的ButtonGroup?

    我目前正在尝试创建一组切换按钮,这些按钮类似于Eclipse的格式化程序首选项中使用的按钮: 目前我已通过以下方式尝试此操作: public class Exercise extends JFrame ...

  5. android创建一个新文件,如何在android中创建一个新文件夹DCIM

    张贴在评论,我想你的代码,并它为我工作. MainActivity.java public class MainActivity extends Activity { private final st ...

  6. 怎么在java中创建一个自定义的collector

    文章目录 简介 Collector介绍 自定义Collector 总结 怎么在java中创建一个自定义的collector 简介 在之前的java collectors文章里面,我们讲到了stream ...

  7. c++中的new_怎么在java中创建一个自定义的collector

    简介 在之前的java collectors文章里面,我们讲到了stream的collect方法可以调用Collectors里面的toList()或者toMap()方法,将结果转换为特定的集合类. 今 ...

  8. idea中java文件怎么运行_Java入门基础篇-如何在Java中创建只读文件

    本文选自千锋教育<Java语言程序设计>,如需转载请注明出处,谢谢! 1.如何创建只读文件 要使文件只读,我们只要将文件属性更改为只读就行:可以使用File类的setReadOnly()方 ...

  9. setyear java_如何在Java中创建不可变类

    如果对象在构造后无法更改,则该对象是不可变的.不可变对象不会以任何方式暴露其他对象来修改其状态; 对象的字段仅在构造函数内初始化一次,并且永远不会再次更改. 在本文中,我们将定义在Java中创建不可变 ...

最新文章

  1. 1803无法升级到2004_Win10再度误伤“友军”:升级五月更新后OneDrive同步报错
  2. jQuery应用之(一)使用jQuery选择器(荐)
  3. 最长子序列和 动态规划python_算法基础之python实现动态规划中数字三角形和最长上升子序列问题...
  4. java建造_java建造者模式
  5. 在Ubuntu中用anaconda快速安装opencv3
  6. Mac pro 上安装 robotframework 时的一个版本问题
  7. Error Kernel Pattern
  8. ASP.NET前台JS与后台CS函数如何互相调用
  9. shell中的$()、${}、$(())、(())
  10. properties 特有的方法
  11. 在安卓手机上下载linux系统,如何在安卓手机上运行Ubuntu系统
  12. jQuery.position()其实不可靠!
  13. KaggleTianChi分类问题相关算法快速实现
  14. 三星计算机用户的密码忘了,如果三星计算机忘记了开机密码怎么办
  15. leetcode Add Digits
  16. 用iTunes管理iPhone
  17. 来自大数据的反思:需要你读懂的10个小故事
  18. clientX,clientY,screenX,screenY,offsetX,offsetY 区别测试
  19. go官网打不开,下载地址https://golang.google.cn/
  20. python刷新腾讯云cdn

热门文章

  1. 算法-----有序数组的平方
  2. ScrollView充满屏幕
  3. Android状态栏
  4. 已知3个坐标点xy画圆弧_这25张图片,让你彻底看懂25个复杂的数学公式!
  5. Zxing二维码开源项目
  6. 【iOS数据持久化】偏好设置(UserDefaults)
  7. python3语法、菜鸟教程_Python3
  8. uniapp中radio颜色渐变
  9. redis安装步骤(单机配置)
  10. BZOJ 2287 【POJ Challenge】消失之物