In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article shares with you the content of an example analysis of the performance comparison between ArrayList and LinkedList in java. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Performance comparison between ArrayList and LinkedList in java
If you look at the code of a framework, you can see that LinkedList is used in some places where ArrayList can be used, and the scenario used is to insert sequentially in a loop.
It is well known that there are two implementations of the List interface in java, ArrayList and LinkedList, and their implementation principles are arrays and linked lists introduced in the c language respectively.
As you learned when learning data structures, the structure of linked lists is more efficient for inserts because inserts can be done by modifying the pointers of nodes, unlike arrays
You need to move the array elements after the insertion position back in turn.
However, is the actual situation really the same as the above assumption? let's see what conclusions can be drawn through a simple example.
Public static void main (String [] args) {List arrayList = new ArrayList (); List linkedList = new LinkedList (); long time1 = System.currentTimeMillis (); for (int I = 0; I < 1000000; iTunes +) {arrayList.add (new String ("abc"));} long time2 = System.currentTimeMillis (); for (int I = 0; I < 1000000; iTunes +) {linkedList.add ("abc") } long time3 = System.currentTimeMillis (); System.out.println ("arrayList.insert_time =" + (time2-time1)); System.out.println ("linkedList.insert_time =" + (time3-time2)); long time11 = System.currentTimeMillis (); for (int I = 0; I < 10000; iTunes +) {int random = RandomUtils.nextInt (10000); String temp = arrayList.get (random);} long time12 = System.currentTimeMillis () For (int I = 0; I < 10000; iTunes +) {int random = RandomUtils.nextInt (10000); String temp = linkedList.get (random);} long time13 = System.currentTimeMillis (); System.out.println ("arrayList.read_time =" + (time12-time11)); System.out.println ("linkedList.read_time =" + (time13-time12));}
Running result:
ArrayList.insert_time = 188linkedList.insert_time = 250arrayList.read_time = 16linkedList.read_time = 234
The results show that ArrayList is more efficient no matter what the situation. Especially for random reads, arrays are 14 times more efficient than linked lists.
On the other hand, the insertion operation takes about the same time, but the array is more efficient.
For the reasons, it is not difficult to think about it carefully.
When there is not much content stored in List, it takes about the same time to write the last element of List, ArrayList and LinkedList.
However, when the number of elements stored in List is very large, ArrayList insertion through the array structure can be accessed quickly through the array subscript, but LinkedList needs to visit each node until the last element is found, which is time-consuming, so the larger the number of lists, the more difficult it is for LinkedList.
Of course, for scenarios that require random insertion, LinkedList is better than ArrayList at this time.
Thank you for reading! On the "java ArrayList and LinkedList performance comparison example analysis" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, you can share it out for more people to see it!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.