1: The general differences between ArrayList and LinkedList are as follows:
1.ArrayList implements the data structure based on dynamic array. ArrayList implements the array with variable length and allocates continuous space in memory. The efficiency of traversal element and random access element is higher
2.LinkedList is based on the data structure of the linked list, so the efficiency of inserting and deleting elements is relatively high. Therefore, when the inserting and deleting operations are frequent, LinkedList can be used to improve the efficiency
LinkedList provides the method of adding and deleting the head and tail elements, and the efficiency of inserting / deleting the first and last elements is relatively high;
3: Both ArrayList and LinkedList are the implementation of List interface, which store a group of non unique and orderly (insertion order) objects, and add and delete elements [i.e. the nature of List]
4. For random access get and set, ArrayList is better than LinkedList because LinkedList needs to move pointer.
5. For add and remove operations, LinedList has an advantage because ArrayList moves data.
6:LinkedList takes up more memory than ArrayList
eg: (code example 01) --- Comparison of data adding and searching time between ArrayList and LinkedLis
import java.util.ArrayList; import java.util.LinkedList; import java.util.List; public class HFSD { static final int N=50000; //Add 5000 pieces of data static long timeList(List list){ //Time to add data long start=System.currentTimeMillis(); Object o = new Object(); for(int i=0;i<N;i++) { list.add(0, o); } return System.currentTimeMillis()-start; } static long readList(List list){ //Time to find data 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 add to"+N+"Time consuming:"+timeList(new ArrayList())); System.out.println("LinkedList add to"+N+"Time consuming:"+timeList(new LinkedList())); List list1=addList(new ArrayList<>()); List list2=addList(new LinkedList<>()); System.out.println("ArrayList lookup"+N+"Time consuming:"+readList(list1)); System.out.println("LinkedList lookup"+N+"Time consuming:"+readList(list2)); }
Add 50000 pieces of data to the ArrayList and LinkedList sets respectively. The test run results are as follows:
From the above results, we can see that ArrayList is more suitable for reading data, and linkedList is more suitable for adding or deleting data.
ArrayList: internally, it is implemented by using an increasable array, so it takes a few times to use get and set methods. However, if you insert and delete elements, unless the positions of insertion and deletion are at the end of the table, the code overhead will be high, because the array movement is required.
LinkedList: it is implemented with double linked list, so get will consume resources very much unless the location is close to the head. But inserting and deleting elements takes a small amount of time.
2: Shared method of ArrayList and LinkedList (i.e. List method):
eg: (code example 02) --- ArrayList code example
package JIhekuangjia006.ArrayList; /** * Piglets */ public class Pig { private String name; private String sex; public Pig(String name,String sex){ this.name=name; this.sex=sex; } public void setName(String name){ this.name=name; } public String getName(){ return name; } public void setSex(String sex){ this.sex=sex; } public String getSex(){ return sex; } }
package JIhekuangjia006.ArrayList; import java.util.*; /** * Using the ArrayList collection to manipulate data */ public class Test1 { public static void main(String[] args) { //1.Store pig information Pig xiaojia=new Pig("Xiaojia","mother");//Index is 0 Pig xiaolong=new Pig("Bruce Lee","common");//Index is 1 Pig jiajia=new Pig("Jiajia","female");//Index is 2 Pig longlong=new Pig("Dragon Dragon","male");//Index is 3 (same as array, starting from 0) //Order piglets List list=new ArrayList(); //Add sorting directly to elements list.add(xiaojia); list.add(jiajia); list.add(xiaolong); //Add dragon to index 2 list.add(2,longlong); //list.add(jiajia);//List An interface stores a set of non unique, ordered (insertion order) objects //2.Total number of piglets obtained //adopt list.size()Method gets the number of elements list.size(); System.out.println("The total number of piglets is"+list.size()); //3.Print pig information one by one //Method 1: for Circulation and get()Method cooperation to implement traversal for(int i=0;i<list.size();i++){ Pig center=(Pig)list.get(i);//because list.get()The return value of is Object So we need to force to Pig Type of System.out.println(center.getName()+","+center.getSex()); } //Method 2: through iterator Iterator Implement traversal // Iterator it=list.iterator(); // while (it.hasNext()){ // Pig center=(Pig)it.next(); // System.out.println(center.getName()+","+center.getSex()); // } System.out.println("*********************************************************"); //4.Delete pig information //Delete the first pig, Xiaojia list.remove(0);//Corresponding to array subscript //Delete the specified pig, Bruce Lee list.remove(xiaolong); //Output the information of the remaining piglets System.out.println("After deletion"+list.size()+"Pigs,\n namely:"); for(int i=0;i<list.size();i++){ /** * Object get(int index)Returns the element at the specified index location. The extracted element is of type Object, * Cast required before use */ Pig center=(Pig)list.get(i);//So we need to force Pig Type of System.out.println(center.getName()+","+center.getSex()); } System.out.println("*******************************************************"); //5.Determine whether the specified piglets are included in the set if (list.contains(xiaojia)){//use list.contains()Method to judge System.out.println("There are Xiaojia in the collection"); }else{ System.out.println("There is no Xiaojia in the collection"); } } }
The test run results are as follows:
3: LinkedList specific methods:
eg: (code example 03) --- LinkedList code example:
package JIhekuangjia006.LinkedList; /** * Piglets */ public class Pig { private String name; //Nickname? private String sex; //Gender public Pig(String name,String sex){ this.name=name; this.sex=sex; } public void setName(String name){ this.name=name; } public String getName(){ return name; } public void setSex(String sex){ this.sex=sex; } public String getSex(){ return sex; } }
package JIhekuangjia006.LinkedList; import java.util.Iterator; import java.util.LinkedList; /** * Using the LinkedList collection to manipulate data */ public class Test2 { public static void main(String[] args) { //1.Store pig information Pig xiaojia=new Pig("Xiaojia","mother");//Index is 0 Pig xiaolong=new Pig("Bruce Lee","common");//Index is 1 Pig jiajia=new Pig("Jiajia","female");//Index is 2 Pig longlong=new Pig("Dragon Dragon","male");//Index is 3 (same as array, starting from 0) //Order piglets LinkedList list=new LinkedList(); //Add sorting directly to elements list.add(xiaolong); list.add(longlong); list.addFirst(jiajia);//Add Jiajia to the first position list.addLast(xiaojia);//Add Bruce Lee to the last location //list.add(jiajia);//List An interface stores a set of non unique, ordered (insertion order) objects //2.Total number of piglets obtained //adopt list.size()Method gets the number of elements list.size(); System.out.println("The total number of piglets is"+list.size()); //3.Print pig's information one by one //Method 1: through iterator Iterator Implement traversal Iterator it=list.iterator(); while (it.hasNext()){ Pig center=(Pig)it.next(); System.out.println(center.getName()+","+center.getSex()); } //Method 2: for Circulation and get()Method to implement traversal // for(int i=0;i<list.size();i++){ // Pig pig=(Pig)list.get(i); // System.out.println(pig.getName()+","+pig.getSex()); // } System.out.println("************************************************"); //4. //Get information about the first piglet Pig center=(Pig)list.getFirst(); System.out.println("The first dog message is:"+center.getName()+","+center.getSex()); //Get information about the last piglet Pig center1=(Pig)list.getLast(); System.out.println("The last dog message is:"+center1.getName()+","+center1.getSex()); System.out.println("*****************************************************"); //5.Delete the first and last piglets list.removeFirst(); list.removeLast(); System.out.println("*****************************************************"); //6.Output the information of the remaining piglets System.out.println("The rest"+list.size()+"Pigs,\n namely:"); for(int i=0;i<list.size();i++){ Pig pig=(Pig)list.get(i); System.out.println(pig.getName()+","+pig.getSex()); } //7.Judge whether there is Xiaojia in the set if(list.contains(xiaojia)){ System.out.println("Small good in set"); }else{ System.out.println("There is no Xiaojia in the set"); } } }
The test run results are as follows: