java--ArrayList,LinkedList应用比较

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class ListDemo {
    //jdk8
    public static void main(String[] args){
//        add("ArrayList",50000);//11
//        add("LinkedList",50000);//8
//        add("ArrayList",1000000);//81
//        add("LinkedList",1000000);//132

//        addFirst("ArrayList",5000);//7
//        addFirst("LinkedList",5000);//2
//        addFirst("ArrayList",50000);//160
//        addFirst("LinkedList",50000);//5

//        remove("ArrayList",5000);//1
//        remove("LinkedList",5000);//2
//        remove("ArrayList",1000000);//2
//        remove("LinkedList",1000000);//15
//

//        removeFirst("ArrayList",5000);//5
//        removeFirst("LinkedList",5000);//1
//        removeFirst("ArrayList",100000);//795
//        removeFirst("LinkedList",100000);//2

//        removeMiddle("ArrayList",5000);//3
//        removeMiddle("LinkedList",5000);//23
//        removeMiddle("ArrayList",100000);//446
//        removeMiddle("LinkedList",100000);//5637
    }
    public static void add(String s,int a){
        if (s.equals("ArrayList")){
            List<Object> list = new ArrayList<>();
            long begin = System.currentTimeMillis();
            for (int i = 0; i < a; i++) {
                list.add(new Object());
            }
            long end = System.currentTimeMillis();
            System.out.println(a+" "+s+":"+(end - begin));
        }else {
            LinkedList<Object> list = new LinkedList<>();
            long begin = System.currentTimeMillis();
            for (int i = 0; i < a; i++) {
                list.add(new Object());
            }
            long end = System.currentTimeMillis();
            System.out.println(a+" "+s+":"+(end - begin));
        }
    }
    public static void addFirst(String s,int a){
        if (s.equals("ArrayList")){
            ArrayList<Object> list = new ArrayList<>();
            long begin = System.currentTimeMillis();
            for (int i = 0; i < a; i++) {
                list.add(0,new Object());
            }
            long end = System.currentTimeMillis();
            System.out.println(a+" "+s+":"+(end - begin));
        }else {
            LinkedList<Object> list = new LinkedList<>();
            long begin = System.currentTimeMillis();
            for (int i = 0; i < a; i++) {
                list.addFirst(new Object());
            }
            long end = System.currentTimeMillis();
            System.out.println(a+" "+s+":"+(end - begin));
        }
    }
    public static void remove(String s,int a){
        if (s.equals("ArrayList")){
            List<Object> list = new ArrayList<>();
            for (int i = 0; i < a; i++) {
                list.add(i);
            }
            long begin = System.currentTimeMillis();
            for (int i = 0; i < a; i++) {
                list.remove(list.size()-1);
            }
            long end = System.currentTimeMillis();
            System.out.println(a+" "+s+":"+(end - begin));
        } else {
            LinkedList<Object> list = new LinkedList<>();
            for (int i = 0; i < a; i++) {
                list.add(i);
            }
            long begin = System.currentTimeMillis();
            for (int i = 0; i < a; i++) {
                list.removeLast();
            }
            long end = System.currentTimeMillis();
            System.out.println(a+" "+s+":"+(end - begin));
        }
    }
    public static void removeFirst(String s,int a){
        if (s.equals("ArrayList")){
            List<Object> list = new ArrayList<>();
            for (int i = 0; i < a; i++) {
                list.add(0,new Object());
            }
            long begin = System.currentTimeMillis();
            for (int i = 0; i < a; i++) {
                list.remove(0);
            }
            long end = System.currentTimeMillis();
            System.out.println(a+" "+s+":"+(end - begin));
        } else {
            LinkedList<Object> list = new LinkedList<>();
            for (int i = 0; i < a; i++) {
                list.add(0,new Object());
            }
            long begin = System.currentTimeMillis();
            for (int i = 0; i < a; i++) {
                list.removeFirst();
            }
            long end = System.currentTimeMillis();
            System.out.println(a+" "+s+":"+(end - begin));
        }
    }
    public static void removeMiddle(String s,int a){
        if (s.equals("ArrayList")){
            List<Object> list = new ArrayList<>();
            for (int i = 0; i < a; i++) {
                list.add(i);
            }
            long begin = System.currentTimeMillis();
            while (list.size()>0){
                list.remove(list.size()>>1);//右移/2,左移*2
            }
            long end = System.currentTimeMillis();
            System.out.println(a+" "+s+":"+(end - begin));
        } else {
            LinkedList<Object> list = new LinkedList<>();
            for (int i = 0; i < a; i++) {
                list.add(i);
            }
            long begin = System.currentTimeMillis();
            while (list.size()>0){
                list.remove(list.size()>>1);//右移/2,左移*2
            }
            long end = System.currentTimeMillis();
            System.out.println(a+" "+s+":"+(end - begin));
        }
    }
}

posted @ 2019-08-13 09:45  fly_bk  阅读(167)  评论(0编辑  收藏  举报