import
java.util.ArrayList;
import
java.util.Arrays;
import
java.util.Collection;
import
java.util.Collections;
import
java.util.List;
public
class
AddingGroups {
public
static
void
main(String[] args) {
Collection<Integer> collection =
new
ArrayList<Integer>(Arrays.asList(
1, 2, 3, 4, 5));
Integer[] moreInts={6,7,8,9,10};
collection.addAll(Arrays.asList(moreInts));
for
(Integer i : collection)
System.
out
.print(i +
","
);
}
}
|
接口
|
实现类
|
保持插入顺序
|
可重复
|
排序
|
使用说明
|
List
|
ArrayList
|
Y
|
Y
|
N
|
长于随机访问元素;但插入、删除元素较慢(数组特性)。
|
LinkedList
|
Y
|
Y
|
N
|
插入、删除元素较快,但随即访问较慢(链表特性)。
|
|
Set
|
HashSet
|
N
|
N
|
N
|
使用散列,最快的获取元素方法。
|
TreeSet
|
N
|
N
|
Y
|
将元素存储在红-黑树数据结构中。默认为升序。
|
|
LinkedHashSet
|
Y
|
N
|
N
|
使用散列,同时使用链表来维护元素的插入顺序。
|
|
Map
|
HashMap
|
N
|
N
|
N
|
使用散列,提供最快的查找技术。
|
TreeMap
|
N
|
N
|
Y
|
默认按照比较结果的升序保存键。
|
|
LinkedHashMap
|
Y
|
N
|
N
|
按照插入顺序保存键,同时使用散列提高查找速度。
|