Array List vs Linked List *** Interview Question ***

Complete Step By Step Java for Testers Java Advanced - Collections Framework
10 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$99.99
List Price:  $139.99
You save:  $40
€93.98
List Price:  €131.57
You save:  €37.59
£80.28
List Price:  £112.40
You save:  £32.11
CA$138.08
List Price:  CA$193.32
You save:  CA$55.23
A$155.74
List Price:  A$218.04
You save:  A$62.30
S$136.25
List Price:  S$190.76
You save:  S$54.50
HK$782.98
List Price:  HK$1,096.21
You save:  HK$313.22
CHF 91.14
List Price:  CHF 127.60
You save:  CHF 36.46
NOK kr1,098.40
List Price:  NOK kr1,537.80
You save:  NOK kr439.40
DKK kr701.21
List Price:  DKK kr981.73
You save:  DKK kr280.51
NZ$169.22
List Price:  NZ$236.92
You save:  NZ$67.69
د.إ367.20
List Price:  د.إ514.09
You save:  د.إ146.89
৳10,977.89
List Price:  ৳15,369.49
You save:  ৳4,391.59
₹8,365.11
List Price:  ₹11,711.49
You save:  ₹3,346.38
RM479.10
List Price:  RM670.76
You save:  RM191.66
₦120,104.65
List Price:  ₦168,151.32
You save:  ₦48,046.66
₨27,833.48
List Price:  ₨38,967.99
You save:  ₨11,134.50
฿3,681.88
List Price:  ฿5,154.78
You save:  ฿1,472.90
₺3,251.57
List Price:  ₺4,552.33
You save:  ₺1,300.75
B$528.60
List Price:  B$740.07
You save:  B$211.46
R1,902.92
List Price:  R2,664.17
You save:  R761.24
Лв183.76
List Price:  Лв257.28
You save:  Лв73.51
₩138,255.79
List Price:  ₩193,563.64
You save:  ₩55,307.85
₪376.37
List Price:  ₪526.94
You save:  ₪150.56
₱5,722.47
List Price:  ₱8,011.69
You save:  ₱2,289.21
¥15,461.20
List Price:  ¥21,646.30
You save:  ¥6,185.10
MX$1,698.40
List Price:  MX$2,377.83
You save:  MX$679.43
QR364.94
List Price:  QR510.94
You save:  QR145.99
P1,382.56
List Price:  P1,935.64
You save:  P553.08
KSh13,248.67
List Price:  KSh18,548.67
You save:  KSh5,300
E£4,879.07
List Price:  E£6,830.89
You save:  E£1,951.82
ብር5,691.67
List Price:  ብር7,968.56
You save:  ብር2,276.89
Kz83,741.62
List Price:  Kz117,241.62
You save:  Kz33,500
CLP$98,036.19
List Price:  CLP$137,254.59
You save:  CLP$39,218.40
CN¥723.78
List Price:  CN¥1,013.33
You save:  CN¥289.54
RD$5,909.81
List Price:  RD$8,273.97
You save:  RD$2,364.16
DA13,475.51
List Price:  DA18,866.26
You save:  DA5,390.74
FJ$227.51
List Price:  FJ$318.52
You save:  FJ$91.01
Q778.01
List Price:  Q1,089.25
You save:  Q311.23
GY$20,943.86
List Price:  GY$29,322.24
You save:  GY$8,378.38
ISK kr14,144.58
List Price:  ISK kr19,802.98
You save:  ISK kr5,658.40
DH1,017.39
List Price:  DH1,424.39
You save:  DH407
L1,786.82
List Price:  L2,501.62
You save:  L714.80
ден5,782.34
List Price:  ден8,095.50
You save:  ден2,313.16
MOP$806.92
List Price:  MOP$1,129.72
You save:  MOP$322.80
N$1,901.80
List Price:  N$2,662.60
You save:  N$760.80
C$3,681.76
List Price:  C$5,154.62
You save:  C$1,472.85
रु13,382.26
List Price:  रु18,735.71
You save:  रु5,353.44
S/373.93
List Price:  S/523.52
You save:  S/149.59
K378.80
List Price:  K530.34
You save:  K151.53
SAR375.07
List Price:  SAR525.11
You save:  SAR150.04
ZK2,508.25
List Price:  ZK3,511.66
You save:  ZK1,003.40
L467.60
List Price:  L654.66
You save:  L187.05
Kč2,369.93
List Price:  Kč3,318
You save:  Kč948.06
Ft36,898.16
List Price:  Ft51,658.91
You save:  Ft14,760.74
SEK kr1,094.62
List Price:  SEK kr1,532.51
You save:  SEK kr437.89
ARS$86,865.91
List Price:  ARS$121,615.75
You save:  ARS$34,749.84
Bs693.67
List Price:  Bs971.16
You save:  Bs277.49
COP$390,802.63
List Price:  COP$547,139.32
You save:  COP$156,336.68
₡50,098.77
List Price:  ₡70,140.28
You save:  ₡20,041.51
L2,469.54
List Price:  L3,457.46
You save:  L987.91
₲740,758.16
List Price:  ₲1,037,091.06
You save:  ₲296,332.90
$U3,889.78
List Price:  $U5,445.86
You save:  $U1,556.07
zł408.85
List Price:  zł572.41
You save:  zł163.55
Already have an account? Log In

Transcript

Hello, guys, welcome back to the next video. So in the last classes we have covered about ArrayList, and the linked list. And we're going to just continue talking about the differences between the earliest and the linked list. And this family is going to show you the practical example of that. So and it's a very important interview question as well. So let's quickly bring up our Eclipse.

And let's see what we talked about. So this is our implementation in the last tutorial of the linked list. And I just moved it on the top. And this is what we left off. So let's pick it up here. So what I want to talk about is, what's the differences I was talking about?

These things, right, that when you have to add at the end, and the Remove from removed from the end accolades, and so and getting as far as then you say, release, and when you have to add at the beginning and remove from the beginning, then you still increase, right. So that's what we were talking about. So let's see. Let's see what's the differences. So let's minimize this right now. It's just taking so much space, and I'm just gonna create a method public static and maybe Yeah, Why'd we have to make invite because we don't want to return anything and curation.

So I'm just naming it run duration, because I want to calculate the duration of these things when we add and remove stuff. And it's going to accept a list. Type in teacher, and let's name it list. And then string is the list type that I'm providing. So I'm going to pass what type of list that I'm adding. So we know what we are doing, basically.

So inside it, what I'm going to do is just first let me just print a few steps to make it look good. And it's easy to understand the code. So what I'm printing right now is making the duration method basically, so we know what we are doing, and for maybe just adding this type so we know that for weightless type, our method is starting. So we have two types, right ArrayList and the link lists and then Let's put something like capturing the time. So you want to capture the time. So let's declare it along and start time equals to system dot current milliseconds.

So this thing captures the system time in milliseconds. And then let's have as a long end time, so let's name it end time. And then let's have a duration, right? So long duration, which is in time, minus time, right? And then let's print out one more statement to actually print but this thing is, so duration of the list and type, whichever type we are printing right now. So let's type and then S. And we're going to print the duration, so plus duration, and then plus milliseconds, right?

So we have kind of crazy The prototype need code, and let's see what we are trying to do next. So what I'm gonna do is I'm going to add a lot of items to the list. So for n equals two, n i equals to zero. And from zero, I'm going to start and i less than equals to maybe hundred thousand, so once 10 hundred thousand, 10,000 hundred thousand, and then i plus plus, right? That's going to be a helicopter song you might be hearing sorry about that are in my control. So we got to Add list, right?

So list dot add. And we already saw that these add methods, they work on both ArrayList and the linked list because they both implement the interface called lists. And that's what we are doing right, less than a face. So they both work in the same manner. So you know what, what we're gonna do is in our main method, we're just going to call these two methods right away. So after this line, let's call these so run ration, right?

And let's type costs Let's call the earliest and then the type is released, right? And then let's call run duration, this time linked list and the type is linked list. So what I'm doing here is I'm calling this method twice. First, I'm passing the ArrayList. Then I'm passing the linked list and putting the type so we know which one we're writing for. Let's quickly run the code, right?

So I'm going to hit the run. So it shows me for the ArrayList. Adding it took seven milliseconds for the link list, it took four milliseconds, it's not a big difference. It's still pretty much the same. If I have like more elements, it might show a little bit of different but still pretty much the same, right? So we're almost pretty much the same, not lot of big issues in performance and all those things right.

So let's move this code out of the start time right out of the time capture thing, let's move on to code. Let's again minimize this, it's taking up a lot of space. So what I'm going to do next is I'm going to actually, first let me just calculate, since we already created a list, we added some elements. So let's find out the size, right? So in size equals to list dot size. And then let's say I want to add more elements.

So maybe elements to add equals to size plus maybe more hundred and 100,000. More, one stands hundred thousand, 10,000 hundred thousand, right? So it's 10,100 100,000. So right now, so what I'm doing is I'm adding more elements to the list, right? So that's what I'm going to do. So let's create one more for loop.

And for in i equals to the same we're gonna start from size. So last time, we already added from the zeroeth index. So the list is on Ready full. Now I want to add from the size. So this is the last right? So when the list is already full I, that's where I want to start.

So I want to add more elements from here. And then I should be less than element start at, right? That's what we want. And I and then plus plus, right? So here we adding more elements to the list. So let's, let's do this thing.

So list dot add, and then I right. And now, what we're doing is we have a list, we have some items, and now we're adding more items to the list at the end, right? So let's do that. Let's run the program. And let's see, if you guys can see it's very easily. Again, adding to the ArrayList took only three milliseconds and to the list.

It took seven milliseconds, right. So it takes takes less time when we are adding to at the end right? Using the ArrayList. You guys can again see that it took three milliseconds it took seven milliseconds here. So if not the as much of a difference, you know what? Let's actually increase this.

Let's make it 200,000. And on that, so you guys can still see a list of priests again, three milliseconds and linkless took five milliseconds, right? So this is what I was talking about. But I showed you one more method, right? To add elements in the last class, that method is list dot add, and to a particular index. So let's say every time I want to add in the beginning of the list, so I'm gonna put the index zero, and then element is I, right?

So when I do this thing, let's see the time difference. Let's run the code. So it's still running. All righty. That's the difference I was talking about. Right?

You guys can easily notice it. So duration for the ArrayList is 2754 milliseconds. But for the linked list, it's still five milliseconds, it's pretty much the same, right? We added at the end, it took about five to seven milliseconds, and even adding in the beginning to the same time. So that's what I was talking about today. I was mentioning that when you have to add in the beginning, then just use the linkless.

Don't use ArrayList. In that case, that's what I was talking about. So let's, let's actually comment this code out, right? So I'll show you the Remove example as well. So let's say I have all these elements added up all those things I know right? So let's start removed.

And if I remove from maybe 90,000, or something, maybe, maybe let's say let's say let's create a for loop and let's remove some items, right. So for end, i equals to maybe start from 90,000 90,000 and go till 92,000 and then i plus plus something like that, and then list dot remove, right and I so if I remove Because from the end of the list, right? Not exactly. And but it's close to end, right, and let's run this code and see if we can see if I'm removing towards the end ArrayList took only two milliseconds, but language took 40 milliseconds, right? That's a huge difference. That's what I want to show about when we are removing from towards the end of the list.

A list is taking a lot less time, right? But let's quickly copy this and then comment this one. Like that. I want to come in come complete blog on Friday, and then again, habit. So this time, I want to start from the beginning, if I'm removing from the beginning, and let's say first hundred elements are removed, right, so let's run the code again. And this time, see, it's not a big difference, because it's only hundred elements.

And we only have 1000 elements. So ArrayList took two milliseconds, but linkless took zero milliseconds, which means linkless is faster. When We're starting from the beginning of the list, right? And it does not take a lot of time. So that's what I wanted to show you guys, what I was talking about these things are keys. So keep these things in mind when you are actually having lists in your code.

So these things are really important to know that what you want to use if you want to use the ArrayList or the linked list. So that's pretty much it, guys. And stay tuned for the next lectures. Thanks for attending this one. Thanks, guys.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.