如何在Java中创建一个新的List
本文翻译自: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>();
您需要导入List
和ArrayList
。
#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相关推荐
- Java乐谱_如何在java中创建一个简单但结构良好的乐谱表(乐谱)?
我正在使用非常基本的声音合成在我的游戏中创建音频和效果.基本上,我有一些方法可以发出一个频率和频率的声音.幅度和幅度持续时间. 对于短语和旋律,我想提出一个基本的符号,这样我就可以轻松地重写或添加新的 ...
- java创建临时文件夹_如何在Java中创建一个临时目录/文件夹?
有没有在Java应用程序内部创建临时目录的标准可靠方法? Java的问题数据库中有一个条目,注释中包含一些代码,但是我想知道在一个常用的库(Apache Commons等)中是否找到标准解决方案? # ...
- java在未来_如何在java中创建一个完整的未来
在java中构建完成的未来的最好方法是什么?我已经实现了我自己的CompletedFuture下面,但希望像这样的东西已经存在. public class CompletedFuture implem ...
- java怎么添加到按钮组_如何在Java中创建一个带有连接按钮的ButtonGroup?
我目前正在尝试创建一组切换按钮,这些按钮类似于Eclipse的格式化程序首选项中使用的按钮: 目前我已通过以下方式尝试此操作: public class Exercise extends JFrame ...
- android创建一个新文件,如何在android中创建一个新文件夹DCIM
张贴在评论,我想你的代码,并它为我工作. MainActivity.java public class MainActivity extends Activity { private final st ...
- 怎么在java中创建一个自定义的collector
文章目录 简介 Collector介绍 自定义Collector 总结 怎么在java中创建一个自定义的collector 简介 在之前的java collectors文章里面,我们讲到了stream ...
- c++中的new_怎么在java中创建一个自定义的collector
简介 在之前的java collectors文章里面,我们讲到了stream的collect方法可以调用Collectors里面的toList()或者toMap()方法,将结果转换为特定的集合类. 今 ...
- idea中java文件怎么运行_Java入门基础篇-如何在Java中创建只读文件
本文选自千锋教育<Java语言程序设计>,如需转载请注明出处,谢谢! 1.如何创建只读文件 要使文件只读,我们只要将文件属性更改为只读就行:可以使用File类的setReadOnly()方 ...
- setyear java_如何在Java中创建不可变类
如果对象在构造后无法更改,则该对象是不可变的.不可变对象不会以任何方式暴露其他对象来修改其状态; 对象的字段仅在构造函数内初始化一次,并且永远不会再次更改. 在本文中,我们将定义在Java中创建不可变 ...
最新文章
- 1803无法升级到2004_Win10再度误伤“友军”:升级五月更新后OneDrive同步报错
- jQuery应用之(一)使用jQuery选择器(荐)
- 最长子序列和 动态规划python_算法基础之python实现动态规划中数字三角形和最长上升子序列问题...
- java建造_java建造者模式
- 在Ubuntu中用anaconda快速安装opencv3
- Mac pro 上安装 robotframework 时的一个版本问题
- Error Kernel Pattern
- ASP.NET前台JS与后台CS函数如何互相调用
- shell中的$()、${}、$(())、(())
- properties 特有的方法
- 在安卓手机上下载linux系统,如何在安卓手机上运行Ubuntu系统
- jQuery.position()其实不可靠!
- KaggleTianChi分类问题相关算法快速实现
- 三星计算机用户的密码忘了,如果三星计算机忘记了开机密码怎么办
- leetcode Add Digits
- 用iTunes管理iPhone
- 来自大数据的反思:需要你读懂的10个小故事
- clientX,clientY,screenX,screenY,offsetX,offsetY 区别测试
- go官网打不开,下载地址https://golang.google.cn/
- python刷新腾讯云cdn