Java:LinkedList 操作练习

本练习使用LinkedList处理100,000条数据

从遍历速度上看:
使用index非常慢,应该避免
使用Iterator速度快,但仍不及ArrayList一半速度。

从添加数据(尾部插入)来看,
比ArrayList慢很多,原因是ArrayList后期一次扩容量大。

对于遍历模式,
ArrayList用不用迭代器遍历似乎没区别,多次测试后两者速度,相同或相若。
但参考https://blog.csdn.net/weixin_39148512/article/details/79234817
这篇文章提到不用迭代器会更快?

关于LinkedList详细性能,
可以参考https://www.cnblogs.com/ming-szu/p/9533019.html这篇实验结论:

ming_szu:

  1. 在尾部插入数据,数据量较小时LinkedList比较快,因为ArrayList要频繁扩容,当数据量大时ArrayList比较快,因为ArrayList扩容是当前容量*1.5,大容量扩容一次就能提供很多空间,当ArrayList不需扩容时效率明显比LinkedList高,因为直接数组元素赋值不需new Node

  2. 在首部插入数据,LinkedList较快,因为LinkedList遍历插入位置花费时间很小,而ArrayList需要将原数组所有元素进行一次System.arraycopy

  3. 插入位置越往中间,LinkedList效率越低,因为它遍历获取插入位置是从两头往中间搜,index越往中间遍历越久,因此ArrayList的插入效率可能会比LinkedList高

  4. 插入位置越往后,ArrayList效率越高,因为数组需要复制后移的数据少了,那么System.arraycopy就快了,因此在首部插入数据LinkedList效率比ArrayList高,尾部插入数据ArrayList效率比LinkedList高

import java.util.*;
import java.util.regex.*;
/*
 * This class is mean to practise data manipulation of LinkedList
 *
 * LinkedList has high performance for data inserting and removing
 * noted that iterator Binary Search could highly improve its searching
 * performance
 */
public class LinkedListPrac{
  //start system time
  private static long start;

  public static void main(String[] args){

    //1. declare a LinkedList and instantiate it
    LinkedList<String> linkedList= new LinkedList<String>();

    //2. declare an ArrayList and instantiate it
    ArrayList<String> arrayList= new ArrayList<String>();

     //3. add 100,000 data to linkedList and count duration
     System.out.println("==============3==============");
     getListType(linkedList);

     start = System.currentTimeMillis();
     insertData(linkedList);
     countTime(start);

     //4.add 100,000 data to arrayList and count duration
     //noted that arraylist is much faster while inserting
     System.out.println("==============4==============");
     getListType(arrayList);

     start = System.currentTimeMillis();
     insertData(arrayList);
     countTime(start);

     //5.go through all data,and get the sum
     //compare time spent with and without iterator
     System.out.println("==============5==============");
     getListType(linkedList);
     //with index,count time
     start = System.currentTimeMillis();
     traverseIndex(linkedList);
     countTime(start);
     //with iterator,count time
     start = System.currentTimeMillis();
     traverseIterator(linkedList);
     countTime(start);

     //6. count sum again by using ArrayList
     System.out.println("==============6==============");
     getListType(arrayList);
     //with index,count time
     start = System.currentTimeMillis();
     traverseIndex(arrayList);
     countTime(start);
     //with iterator,count time
     start = System.currentTimeMillis();
     traverseIterator(arrayList);
     countTime(start);

     //7. remove the first and the last data of linkList
     // then print the first and the last data of the new list
     System.out.println("==============7==============");
     getListType(linkedList);
     start = System.currentTimeMillis();

     linkedList.removeFirst();
     linkedList.removeLast();
     System.out.println("first data: "+linkedList.getFirst());
     System.out.println("last data: "+linkedList.getLast());

     countTime(start);

     //8. inserting all data of arraylist into linkedList
     //starting after the mid index of the linkedlist
     System.out.println("==============8==============");
     System.out.println("inserting arraylist data into linkedList...");
     start = System.currentTimeMillis();

     int insertPos =linkedList.size()>1?linkedList.size()/2:1;
     linkedList.addAll(insertPos,arrayList);

     countTime(start);

     System.out.println("size of new linkedlist is "+linkedList.size()
     +" ,and the first and last data is "
     + linkedList.getFirst() + "||"+linkedList.getLast());

     //9. get rid of identical data from linkedList
     System.out.println("==============9==============");
     System.out.println("getting rid of identical data from linkedList...");
     start = System.currentTimeMillis();

     LinkedHashSet<String> linkedHashSet = new LinkedHashSet<String>(linkedList.size());
     linkedHashSet.addAll(linkedList);
     linkedList.clear();
     linkedList.addAll(linkedHashSet);

     countTime(start);

     System.out.println("size of new linkedlist is "+linkedList.size()
     +" ,and the first and last data is "
     + linkedList.getFirst() + "||"+linkedList.getLast());

  }
  /*
   * insert numbers as String from 1 to 100,000 into a List<String>
   */
  public static void insertData(List<String> list){

      for (int i=1;i<100001;i++){
        list.add(String.valueOf(i));
      }
  }
  /*
   * go through the list by index
   */
  public static void traverseIndex(List<String> list){
    int sum=0;

    for (int i=0;i<list.size();i++){
      sum+=Integer.parseInt(list.get(i));
    }
    System.out.println("The sum of all data counted without iterator: "+ sum);
  }

  /*
   * go through the list by iterator
   */
  public static void traverseIterator(List<String> list){
    int sum=0;
    Iterator iterator = list.iterator();

    while (iterator.hasNext()){
      sum+=Integer.parseInt(iterator.next().toString());
    }
    System.out.println("The sum of all data counted with iterator: "+ sum);
  }

  /*
   * count time of a process by given start system time
   */
  public static void countTime(long start){
    long end = System.currentTimeMillis();
    long duration = end - start;
    System.out.println("Time spent on this process: " + duration +" ms");
  }


  /*
   * get the class name of the list for data access
   */
  public static void getListType(List<String> list){
    Pattern ptn = Pattern.compile("[A-Z]\\D+");
    Matcher mtn = ptn.matcher(list.getClass().getName().trim());
    mtn.find();
    System.out.println(mtn.group());
  }

}



输出结果:

==============3==============
LinkedList
Time spent on this process: 12 ms
==============4==============
ArrayList
Time spent on this process: 7 ms
==============5==============
LinkedList
The sum of all data counted without iterator: 705082704
Time spent on this process: 6753 ms
The sum of all data counted with iterator: 705082704
Time spent on this process: 10 ms
==============6==============
ArrayList
The sum of all data counted without iterator: 705082704
Time spent on this process: 7 ms
The sum of all data counted with iterator: 705082704
Time spent on this process: 5 ms
==============7==============
LinkedList
first data: 2
last data: 99999
Time spent on this process: 5 ms
==============8==============
inserting arraylist data into linkedList...
Time spent on this process: 28 ms
size of new linkedlist is 199998 ,and the first and last data is 2||99999
==============9==============
getting rid of identical data from linkedList...
Time spent on this process: 43 ms
size of new linkedlist is 100000 ,and the first and last data is 2||100000

猜你喜欢

转载自blog.csdn.net/OliverZang/article/details/84769429