列表List
List:列表
- 有序的Collection
- 允许重复元素
- {1,2,4,{5,2},1,3}
List主要实现
- ArrayList(非同步的)
- LinkedList(非同步)
- Vector(同步)
ArrayList:
- 以数组实现的列表,不支持同步
- 利用索引位置可以快速定位访问
- 不适合指定位置的插入,删除操作
- 适合变动不大,主要用于查询的数据
- 和Java数组相比,其容量是可动态调整的
- ArrayList在元素填满容器时会自动扩充容器大小的50%
import java.util.ArrayList;
import java.util.Iterator;
public class ArrayListTest {
public static void main(String[] args) {
ArrayList<Integer> al = new ArrayList<Integer>();
al.add(3);
al.add(2);
al.add(1);
al.add(4);
al.add(5);
al.add(6);
al.add(new Integer(6));
System.out.println("The third elements is " + al.get(3));
al.remove(3);//删除第四个单元,后面元素往前挪动
al.add(3,9);//将9插入第4个元素,后面元素往后挪动
//遍历方法:
ArrayList<Integer> as = new ArrayList<Integer>(100000);
for (int i = 0; i < 100000; i++) {
as.add(i);
}
traverseByIterator(as);
traverseByIndex(as);
traverseFor(as);
}
private static void traverseFor(ArrayList<Integer> al) {
long startTime = System.nanoTime();
System.out.println("for循环遍历:");
for(Integer item : al) {
;
}
long endTime = System.nanoTime();
long duration = endTime - startTime;
System.out.println(duration + "纳秒");
}
private static void traverseByIndex(ArrayList<Integer> al) {
long startTime = System.nanoTime();
System.out.println("随机索引遍历:");
for(int i = 0; i < al.size(); i++) {
al.get(i);
}
long endTime = System.nanoTime();
long duration = endTime - startTime;
System.out.println(duration + "纳秒");
}
public static void traverseByIterator(ArrayList<Integer> al) {
long startTime = System.nanoTime();
System.out.println("迭代器遍历:");
Iterator<Integer> iter1 = al.iterator();
while(iter1.hasNext()) {
iter1.next();
}
long endTime = System.nanoTime();
long duration = endTime - startTime;
System.out.println(duration + "纳秒");
}
}
The third elements is 4
迭代器遍历:
27090200纳秒
随机索引遍历:
14441700纳秒
for循环遍历:
24612900纳秒
LinkedList:
- 以双向链表实现的链表,不支持同步
- 可被当作堆栈,队列和双端队列进行操作
- 顺序访问高效,随机访问较差,中间插入和删除高效
- 适用于经常变化的数据
import java.util.Iterator;
import java.util.LinkedList;
public class LinkedListTest {
public static void main(String[] args) {
LinkedList<Integer> ll = new LinkedList<Integer>();
ll.add(3);
ll.add(2);
ll.add(5);
ll.add(6);
ll.add(6);
System.out.println(ll.size());
ll.addFirst(9);//在头部添加9
ll.add(3,10);//将10插入到第四个元素,4以及后续的元素往后挪动
ll.remove(3);//将第4个元素删除
LinkedList<Integer> list = new LinkedList<Integer>();
for(int i = 0; i < 100000; i++) {
list.add(i);
}
traverseByIterator(list);
traverseByIndex(list);
traverseFor(list);
}
private static void traverseFor(LinkedList<Integer> list) {
long startTime = System.nanoTime();
System.out.println("for循环遍历:");
for(Integer item : list) {
;
}
long endTime = System.nanoTime();
long duration = endTime - startTime;
System.out.println(duration + "纳秒");
}
private static void traverseByIndex(LinkedList<Integer> list) {
long startTime = System.nanoTime();
System.out.println("随机索引遍历:");
for(int i = 0; i < list.size(); i++) {
list.get(i);
}
long endTime = System.nanoTime();
long duration = endTime - startTime;
System.out.println(duration + "纳秒");
}
private static void traverseByIterator(LinkedList<Integer> list) {
long startTime = System.nanoTime();
System.out.println("迭代器遍历:");
Iterator<Integer> iter1 = list.iterator();
while(iter1.hasNext()) {
iter1.next();
}
long endTime = System.nanoTime();
long duration = endTime - startTime;
System.out.println(duration + "纳秒");
}
}
长度:
5
迭代器遍历:
6611800纳秒
随机索引遍历:
5266442500纳秒
for循环遍历:
7116100纳秒
Vector(同步):
- 和ArrayList类似,可变数组实现的列表
- Vector同步,适合在多线程下使用
- 原先不属于JCF框架,属于Java最早的数据结构,性能较差
- 官方文档建议在非同步情况下,优先采用ArrayList
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Vector;
public class VectorTest {
public static void main(String[] args) {
Vector<Integer> v = new Vector<Integer>();
v.add(1);
v.add(2);
v.add(3);
v.remove(2);
v.add(1, 5);
System.out.println("长度:" + v.size());
System.out.println("遍历方法:");
Vector<Integer> v2 = new Vector<Integer>(100000);
for(int i = 0; i < 100000; i++) {
v2.add(i);
}
traverseByIterator(v2);
traverseByIndex(v2);
traverseFor(v2);
traverseBuEnumeration(v2);
}
private static void traverseBuEnumeration(Vector<Integer> v) {
long startTime = System.nanoTime();
System.out.println("Enumeration遍历:");
for(Enumeration<Integer> enu = v.elements(); enu.hasMoreElements();) {
enu.nextElement();
}
long endTime = System.nanoTime();
long duration = endTime - startTime;
System.out.println(duration + "纳秒");
}
private static void traverseFor(Vector<Integer> v2) {
long startTime = System.nanoTime();
System.out.println("for循环遍历:");
for(Integer item : v2) {
;
}
long endTime = System.nanoTime();
long duration = endTime - startTime;
System.out.println(duration + "纳秒");
}
private static void traverseByIndex(Vector<Integer> v2) {
long startTime = System.nanoTime();
System.out.println("随机索引遍历:");
for(int i = 0; i < v2.size(); i++) {
v2.get(i);
}
long endTime = System.nanoTime();
long duration = endTime - startTime;
System.out.println(duration + "纳秒");
}
private static void traverseByIterator(Vector<Integer> v2) {
long startTime = System.nanoTime();
System.out.println("迭代器遍历:");
Iterator<Integer> iter1 = v2.iterator();
while(iter1.hasNext()) {
iter1.next();
}
long endTime = System.nanoTime();
long duration = endTime - startTime;
System.out.println(duration + "纳秒");
}
}
长度:3
遍历方法:
迭代器遍历:
20057900纳秒
随机索引遍历:
6838800纳秒
for循环遍历:
5963200纳秒
Enumeration遍历:
7932200纳秒
总结
- ArrayList/LinkedList/Vector
- 同步采用Vector
- 非同步情况下,根据数据特点选取ArrayList/LinkedList