LinkList 是基于链表的,
ArrayList 是基于数据的,
本人自己测试的结果是ArrayList的增删盖查的速度均好于LinkList。
以下是测试的代码:
package main;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.RandomAccess;
/**
* 测试Random Access List(随机访问列表)如 ArrayList 和 Sequence Access List(顺序访问列表)如 LinkedList </br>
* 不同遍历算法的效率</br>
* 结论:前者用循环,后者用迭代器
*/
@SuppressWarnings({"rawtypes", "unchecked"})
public class ListTest {
public static void printlnListRandomAccess(List list) {
if (list instanceof RandomAccess) {
System.out.println("实现了 RandomAccess 接口...");
} else {
System.out.println("没实现 RandomAccess 接口...");
}
}
/**
* 查询指定位置的数
* @param list
* @param position
* @param <T>
* @return
*/
public static <T> List getList(List list, int position) {
printlnListRandomAccess(list);
long startTime = System.currentTimeMillis();
list.get(position);
long endTime = System.currentTimeMillis();
System.out.println("remove时间:" + (endTime - startTime));
return list;
}
/**
* remove指定位置的数
* @param list
* @param position
* @param <T>
* @return
*/
public static <T> List removeList(List list, int position) {
printlnListRandomAccess(list);
long startTime = System.currentTimeMillis();
list.remove(position);
long endTime = System.currentTimeMillis();
System.out.println("remove时间:" + (endTime - startTime));
return list;
}
/**
* 在指定位置添加数
* @param list
* @param n
* @param position
* @param <T>
* @return
*/
public static <T> List addList(List list, int n,int position) {
printlnListRandomAccess(list);
long startTime = System.currentTimeMillis();
list.add(position,n);
long endTime = System.currentTimeMillis();
System.out.println("add时间:" + (endTime - startTime));
return list;
}
/**
* 更新指定位置的数
* @param list
* @param n
* @param position
* @param <T>
* @return
*/
public static <T> List setList(List list, int n,int position) {
printlnListRandomAccess(list);
long startTime = System.currentTimeMillis();
list.set(position,n);
long endTime = System.currentTimeMillis();
System.out.println("set时间:" + (endTime - startTime));
return list;
}
/**
* 初始化 list,添加n个元素
* @param list
* @return
*/
public static <T> List initList(List list, int n) {
if (list instanceof RandomAccess) {
System.out.println("实现了 RandomAccess 接口...");
} else {
System.out.println("没实现 RandomAccess 接口...");
}
long startTime = System.currentTimeMillis();
for (int i = 0; i < n; i++)
list.add(i);
long endTime = System.currentTimeMillis();
System.out.println("遍历时间:" + (endTime - startTime));
return list;
}
/**
* 遍历 list,判断是否实现 RandomAccess 接口来使用不同的遍历方法
* @param list
*/
public static void accessList(List list) {
long startTime = System.currentTimeMillis();
if (list instanceof RandomAccess) {
System.out.println("实现了 RandomAccess 接口...");
for (int i = 0; i < list.size(); i++) {
list.get(i);
}
} else {
System.out.println("没实现 RandomAccess 接口...");
for (Iterator iterator = list.iterator(); iterator.hasNext(); ) {
iterator.next();
}
}
long endTime = System.currentTimeMillis();
System.out.println("遍历时间:" + (endTime - startTime));
}
/**
* loop 遍历 list
*/
public static void accessListByLoop(List list) {
long startTime = System.currentTimeMillis();
for (int i = 0; i < list.size(); i++) {
list.get(i);
}
long endTime = System.currentTimeMillis();
System.out.println("loop遍历时间:" + (endTime - startTime));
}
/**
* 迭代器遍历
*/
public static void accessListByIterator(List list) {
long startTime = System.currentTimeMillis();
for (Iterator iterator = list.iterator(); iterator.hasNext(); ) {
iterator.next();
}
long endTime = System.currentTimeMillis();
System.out.println("Iterator遍历时间:" + (endTime - startTime));
}
public static void main(String[] args) {
ArrayList<Integer> aList = (ArrayList<Integer>) initList(new ArrayList<>(), 10000000);
LinkedList<Integer> lList = (LinkedList<Integer>) initList(new LinkedList<>(), 10000000);
// accessList(aList);
// accessList(lList);
// System.out.println("ArrayList");
// accessListByLoop(aList);
// accessListByIterator(aList);
// System.out.println("LinkedList");
// accessListByLoop(lList);
// accessListByIterator(lList);
addList(aList,987654321,8000000);
addList(lList,987654321,8000000);
removeList(aList,8000000);
removeList(lList,8000000);
setList(aList,987654321,8000000);
setList(lList,987654321,8000000);
getList(aList,8000000);
getList(lList,8000000);
}
}
测试结果:
1583227064(1).jpg
网友评论