Java LinkedList vs ArrayList: Memory Usage, Performance, and equals() Method - Prof. Dougl, Quizzes of Computer Science

This document compares the memory usage, performance, and equals() method implementation of java's linkedlist and arraylist. It discusses the differences in adding, removing, and accessing elements in both data structures and provides performance test results.

Typology: Quizzes

Pre 2010

Uploaded on 07/23/2009

koofers-user-p3m
koofers-user-p3m 🇺🇸

10 documents

1 / 55

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS251 Intermediate Programming
Lecture 20
Data Structures #2
• Quiz 5 next Monday on Text I/O and Lists.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37

Partial preview of the text

Download Java LinkedList vs ArrayList: Memory Usage, Performance, and equals() Method - Prof. Dougl and more Quizzes Computer Science in PDF only on Docsity!

CS251 Intermediate Programming

Lecture 20

Data Structures

  • Quiz 5 next Monday on Text I/O and Lists.

ArrayList

s have the following advantages:

elements.Flexible size: it can grow to accommodate any number ofout of order.Random access: there is no penalty for accessing elementsLow overhead: it is little more than an array.

ArrayList

s have the following disadvantages:

ArrayList

s don't help you organize data. You pick and

ArrayList Inserting into or removing elements from the middle of anthe object.keep track of the unique index values you wish to use to hold

is slow.

one.go through every element in sequence, looking for the right Searching for a particular object is slow because you have to

Object

An element in a linked list is stored in an entry object like this:

Entry

string of pearls:The elements in a linked list are stored in a sequence of entries connected like a

Object Entry 0

Object Entry 1

Object Entry 2

Object

Entry 3

null

null

From each element, you can

fi nd the next element or the previous element, if

have the previous references.)there is one. This is called a doubly-linked list. (A singly-linked list does not

it. the list is closest to the object's index, and then follow the links until you reachIn order to get an object from a linked list, you start from whichever end of

Entry 0

Entry 1

Entry 2

Entry 3

null

null

To

fi nd element

, we start from the

fi rst entry, and follow the

next

reference

1 time. Then we follow that entry's

element

reference and return that object.

To

fi nd element

49

in a size

100

linked list requires following stepping through

49 references. This gets slow for large lists.

first

last

size = 4

Object

Object

Object

Object

To remove an object from a linked list,

fi rst we have to

fi nd its entry. Once we

have it, we

fi nd it's previous and next neighbor. We connect their previous and

next references in such a way that bypasses the removed entry.

Entry 0

Entry 1

Entry 2

null

null

size reached, so it will be cleaned up by the garbage collector. We decrement the The removed entry has been effectively cut out of the chain and cannot be

fi eld to re

fl

ect the absence of the entry.

first

last

size = 3

Object

Object

Object

Object

entry's elementTo add an object to a linked list, we construct an entry for it and point the new

fi eld to the object. Entry 0

Entry 1

Entry 2

null

null

first

last

size = 3

Object

Object

Object

Object

are inserting an object at index 2, weThen, we look up the entries the new object should be inserted bet ween. If we

fi nd entries 1 and 2. Then we connect

that "splices" the new entry into the list.their previous and next references, and the new entry's references in a way

Entry 0

Entry 1

Entry 2

null

null

first

last

size = 3

Object

Object

Object

Object

We're assigning four references.

characteristics. They are each efThe t wo concrete list implementations have different performance

fi cient in different cases. Let's consider

the computer.several list operations and see how many fundamental steps they require in

size = 10

size = 10

first

last

LinkedList

ArrayList

As far as memory usage...

size = 10

size = 10

first

last

LinkedList

ArrayList

11

objects created (the list and 10 entries)

20

bytes per entry =

200

bytes

20

bytes for the list object

220

bytes total.

2

objects created (the list and the array)

52

bytes for the array

12

bytes for the list object

64

bytes total.

Don't memorize this

ArrayList

will always be more memory ef

fi cient than

LinkedList

Remove the object at index

size = 9

size = 9

first

last

LinkedList

ArrayList

1. Look up

(^) first

(^) element

2. Follow

(^) next

(^) reference to element 1

3. Set the

(^) previous

(^) reference of element 1 to null

4. Set

(^) first

(^) to point to element 1

LinkedList

will usually remove elements faster than

ArrayList

, especially

because elements are usually removed from the beginning or the end of the list.

10. Set reference 9 to null9. Copy reference 9 to slot 88. Copy reference 8 to slot 77. Copy reference 7 to slot 66. Copy reference 6 to slot 55. Copy reference 5 to slot 44. Copy reference 4 to slot 33. Copy reference 3 to slot 22. Copy reference 2 to slot 1 1. Copy reference 1 to slot 0

Some performance tests:

Test

ArrayList

LinkedList

Add 100 items to list

8.



sec 7.



sec

Get item 50 from list of 100

0.



sec 1.



sec

Set item 50 in list of 100

0.



sec 1.



sec

Add item at end of list of 100

0.



sec 0.



sec

Remove

fi rst item from list of 100

0.



sec 0.



sec

Insert item at beginning of list of 100

1.



sec 0.



sec

dependent upon one particular machine con Take these numbers with a grain of salt. Performance benchmarks are highly

fi guration and the nature of the

benchmark.

μ

= micro =

-

and interfaces we have so far.These are the collections classes

The

operator is another place where the distinction

When used to compare primitives, thebet ween primitive types and objects becomes important.

operator compares

compare object references,the actual data contained in the primitives. When used to

compares the references, not

In other words,the data in the objects pointed to.

only evaluates to

true

if t wo references

String This can lead to unexpected behavior, such as when comparingpoint to the identical object.

s.