您好,登錄后才能下訂單哦!
ArrayList與LinkedList都是繼承至Collection對象,內部實現導致性能方面存在一定的區別
插入性能對比
private final static int COUNT = 10000000;
private static long insertObject(List<Object> list) {
Object obj = new Object();
long startTime = System.currentTimeMillis();
for (int i = 0; i < COUNT; i++) {
list.add(obj);
}
long endTime = System.currentTimeMillis();
return endTime - startTime;
}
public static void main(String[] args) {
int initCount = (int) (COUNT * 1.5);
System.out.println("ArrayList 增加" + COUNT + "條數據,耗時:" + insertObject(new ArrayList<Object>(initCount)));
System.out.println("LinkedList 增加" + COUNT + "條數據,耗時:" + insertObject(new LinkedList<Object>()));
}
上述代碼得出下列結果:
private final static int COUNT = 1000000;
private static Map<String, Object> insertObject(List<Object> list) {
User user = new User();
long startTime = System.currentTimeMillis();
for (int i = 0; i < COUNT; i++) {
user.setId(i);
user.setName("i:" + i);
user.setAge(i);
list.add(user);
}
long endTime = System.currentTimeMillis();
Map result = new HashMap<String, Object>();
result.put("time", endTime - startTime);
result.put("list", list);
return result;
}
private static long readObject(List<User> list) {
long startTime = System.currentTimeMillis();
for (User user : list) {
}
long endTime = System.currentTimeMillis();
return endTime - startTime;
}
public static void main(String[] args) {
// 插入
int initCount = (int) (COUNT * 1.5);
Map<String, Object> arrayListResult = insertObject(new ArrayList<Object>());
Map<String, Object> linkedListResult = insertObject(new LinkedList<Object>());
System.out.println("ArrayList 增加" + COUNT + "條數據,耗時:" + arrayListResult.get("time"));
System.out.println("LinkedList 增加" + COUNT + "條數據,耗時:" + linkedListResult.get("time"));
// 讀取
System.out.println("ArrayList 查詢" + COUNT + "條數據,耗時:" + readObject((List<User>) arrayListResult.get("list")));
System.out.println("LinkedList 查詢" + COUNT + "條數據,耗時:" + readObject((List<User>) linkedListResult.get("list")));
}
結果如下所示:
3.LinkedList的優勢
static final int N = 500000;
static long timeList(List<Object> list) {
long start = System.currentTimeMillis();
Object o = new Object();
for (int i = 0; i < N; i++) {
list.add(0, o);
}
long endTime = System.currentTimeMillis();
return endTime - start;
}
static long readList(List list) {
long start = System.currentTimeMillis();
for (int i = 0, j = list.size(); i < j; i++) {
}
return System.currentTimeMillis() - start;
}
static List addList(List list) {
Object o = new Object();
for (int i = 0; i < N; i++) {
list.add(0, o);
}
return list;
}
public static void main(String[] args) {
System.out.println("ArrayList添加" + N + "條耗時:" + timeList(new ArrayList<Object>()));
System.out.println("LinkedList添加" + N + "條耗時:" + timeList(new LinkedList<Object>()));
List list1 = addList(new ArrayList<>());
List list2 = addList(new LinkedList<>());
System.out.println("ArrayList查找" + N + "條耗時:" + readList(list1));
System.out.println("LinkedList查找" + N + "條耗時:" + readList(list2));
}
結果:
LinkedList的優勢在于頭部插入,而不是隨機插入,在使用過程中需要根據自己的需求進行選擇!!!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。