















































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 55
This page cannot be seen from the preview
Don't miss anything!
















































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
As far as memory usage...
size = 10
size = 10
first
last
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
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:
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