T h e J a v a c
- l
l e c t i
- n
s f r a me w
- r
k ( n
- t
- n
T h e J a v a c o l l e c t i o n s f r a me w - - PowerPoint PPT Presentation
T h e J a v a c o l l e c t i o n s f r a me w o r k ( n o t o n e x a m ) J a v a c o l l e c t i o n s f r a m e w o r k M a n y d a t a s t r u c t u r e s i m p l
– a
l s
I d e n t i t y H a s h M a p , L i n k e d H a s h M a p , L i n k e d H a s h S e t , a n d m
e
List i s u s e d f
c
l e c t i
s w h e r e e l e m e n t s
c u r i n a n
d e r ( e . g . l i n k e d l i s t s , d y n a m i c a r r a y s ) : interface List<E> extends Collection<E> { // Add an element at a specific index boolean add(int index, E e); // Remove the element at a given index boolean remove(int index); // Get the element at a given index E get(int index); // Modify the element at a given index E set(int index, E element); // Return a slice of the list, // containing only elements from index “from” // to index “to” List<E> sublist(int from, int to); ... }
LinkedList a n d ArrayList i m p l e m e n t a l l List
e r a t i
s , e v e n t h
e t h a t h a v e b a d p e r f
m a n c e ! Tie f
l
i n g e x a m p l e s w
k e v e n t h
g h t h e y a r e n
r e a l l y e ffj c i e n t : int sum(LinkedList<Integer> list) { int total = 0; // Using a linked list for random access :( for (int i = 0; i < list.size(); i++) total += list.get(i); return total; } void deleteFirst(ArrayList<Integer> list) { // Deleting from the front of a dynamic array :( list.delete(0); }
boolean containsKey(K key); V get(K key); void put(K key, V value); V remove(K key); Set<K> keySet(); Collection<V> valueSet(); Set<Entry<K,V>> entrySet(); // Entry<K,V> has methods // getKey, getValue, setValue
HashMap<K, V> map = …; for (K key: map.keySet()) { … }
interface Iterator<E> { // Return the next item in the iteration E next(); // Return true if the iteration has more elements boolean hasNext(); }
n c e y
h a v e g
e t h r
g h a l l e l e m e n t s i n t h e s e q u e n c e , hasNext() w i l l r e t u r n f a l s e
Iterator<E> iterator();
m a i n
e r a t i
: s p l a y i n g m
e s a g i v e n n
e t
h e r
t h e t r e e ( b y d
n g r
a t i
s )
n s e r t i
: u s e B S T i n s e r t i
a n d s p l a y t h e n e w n
e
u p : u s e B S T l
u p a n d s p l a y t h e c l
e s t n
e
e l e t i
: t
a l l y d i fg e r e n t ( a n d m u c h s i m p l e r ) a l g
i t h m t h a n f
B S T s !
A m
t i s e d c
p l e x i t y a n a l y s i s : h
t
a l c u l a t e t h e a m
t i s e d c
p l e x i t y
a d a t a s t r u c t u r e ?
n p r i n c i p l e : c a l c u l a t e t h e c
t
a n y s e q u e n c e
e r a t i
s
e d i d t h i s f
d y n a m i c a r r a y s b u t f
m
e c
p l e x s t r u c t u r e s i t g e t s t
a r d
O n e a p p r
c h : t h e b a n k e r ’ s m e t h
m a g i n e y
r d a t a s t r u c t u r e a s a c
n
e r a t e d m a c h i n e – e v e r y t i m e y
p u t i n a c
n i t e x e c u t e s
e i n s t r u c t i
n “ n
m a l ” d a t a s t r u c t u r e s , n u m b e r
c
n s c h a r g e d f
e a c h
e r a t i
= n u m b e r
i n s t r u c t i
s e x e c u t e d = c
p l e x i t y
n t h e b a n k e r ’ s m e t h
, w e c h a r g e e x t r a c
n s f
t h e c h e a p
e r a t i
s , a n d s a v e t h e m u p f
l a t e r
e n w e g e t t
n e x p e n s i v e
e r a t i
, w e u s e t h e s a v e d u p c
n s t
a y f
i t
d y n a m i c a r r a y s , i f w e c h a r g e a n e x t r a c
n f
e a c h a d d
e r a t i
, w e s a v e u p e n
g h c
n s t
a y f
t h e r e s i z e – w e c a n a l w a y s c h a r g e a c
s t a n t n u m b e r
c
n s p e r a d d a n d t h i s m e a n s w e h a v e a m
t i s e d O ( 1 ) c
p l e x i t y
I n t e r e s t i n g d a t a s t r u c t u r e s : s p l a y t r e e s , s k e w h e a p s
t t a k e s a b
t 2 c l
k c y c l e s f
t h e p r
e s s
t
e a d d a t a f r
m e m
y
u t r e c e n t l y
c c e s s e d p a r t s
m e m
y a r e s t
e d i n t h e p r
e s s
’ s c a c h e , a f a s t m e m
y
~ 3 2 K B , a n d t a k e
l y ~ 1 c l
k c y c l e t
e a d !
r r a y s h a v e m u c h b e t t e r l
a l i t y t h a n l i n k e d l i s t s : t h e i r e l e m e n t s a r e s t
e d c
t i g u
s l y i n m e m
y
c c e s s i n g t h e e l e m e n t s
a n a r r a y i n a l i n e a r
d e r i s e s p e c i a l l y g
– t h e p r
e s s
h a s s p e c i a l c i r c u i t r y t
e t e c t t h i s , a n d w i l l s t a r t p r e f e t c h i n g t h e a r r a y , r e a d i n g e l e m e n t s i n t
h e c a c h e b e f
e y
r p r
r a m a s k s f
t h e m
u i c k s
t a n d m e r g e s
t h a v e m u c h b e t t e r l
a l i t y t h a n h e a p s
t
t t p s : / / d z
e . c
/ a r t i c l e s / e v e r y
r
r a m m e r
h
l d
n
1 c a c h e r e f e r e n c e ( ~ r e a d i n g
e v a r i a b l e ) , . 5 n s
e n d i n g a p a c k e t U S A E u r
e U S A , 1 5 m s → →
e a d i n g f r
L 1 c a c h e , . 5 s (
e h e a r t b e a t )
e a d i n g f r
m a i n m e m
y , 1 s
e a d i n g f r
S S D , 1 . 7 d a y s
e a d i n g f r
h a r d d r i v e , 4 m
t h s !
e n d a p a c k e t U S A E u r
e U S A , 5 y e a r s ! → →
e r y f a s t i f y
c h
e a g
h a s h f u n c t i
: O ( 1 )
n
d e r e d : c a n ’ t b e u s e d t
e . g . , fj n d a l l v a l u e s i n a g i v e n r a n g e
p e r f
m a n c e i f y
c a n k e e p i t b a l a n c e d : O ( l
n )
u i t e s i m p l e t
m p l e m e n t
r
a b i l i s t i c p e r f
m a n c e
l y , t y p i c a l l y : O ( l
n )
r i e s : v e r y s i m p l e t
m p l e m e n t , O ( w ) p e r f
m a n c e
a d i x t r e e s : a b i t h a r d e r , b u t O ( m i n ( w , l
n ) ) p e r f
m a n c e
u t
l y f
c e r t a i n t y p e s
i n p u t d a t a
t h
Tie e x a m i s i n E n g l i s h , a n d y
c a n b r i n g a c h e a t s h e e t ,
e h a n d w r i t t e n A 4 p i e c e
p a p e r 6 q u e s t i
s ; y
g e t a 3 , 4
5 (
U ) f
e a c h q u e s t i
a n y q u e s t i
s a r e j u s t r i g h t
w r
g , w h e r e r i g h t = 5 , w r
g = U
s
e q u e s t i
s , y
m a y h a v e t
. g . a n s w e r a n e x t r a p a r t t
e t a 5
t h a t q u e s t i
– t h i s w i l l b e c l e a r l y s t a t e d
Tie g r a d e y
g e t i s d e c i d e d a s f
l
s :
e t g r a d e 3
t h e e x a m , y
m u s t g e t a 3
3 q u e s t i
s
e t g r a d e 4
t h e e x a m , y
m u s t g e t a 4
4 q u e s t i
s
e t g r a d e 5
t h e e x a m , y
m u s t g e t a 5
5 q u e s t i
s
B e s t p r e p a r a t i
: d
h e e x e r c i s e s , m a k e s u r e y
u n d e r s t a n d t h e l a b s , l
a t t h e
d e x a m s
l d e x a m s a r e i n S w e d i s h , b u t I w i l l u p l
d E n g l i s h t r a n s l a t i
s
s e l e c t e d q u e s t i
s – D I T 9 6 / D I T 9 6 1 e x a m q u e s t i
s a r e i n E n g l i s h a n d u s e f u l t
a t ( l i n k e d f r
t h e c
r s e w e b s i t e )
Wh a t y
n e e d t
n
: t h e f
l
i n g !
D y n a m i c a r r a y s Q u e u e , s t a c k a n d d e q u e i m p l e m e n t a t i
s B i n a r y s e a r c h t r e e s , A V L t r e e s , 2
t r e e s , A A t r e e s
d e l e t i
f
A V L , 2
A A t r e e s – b u t s t i l l f
p l a i n B S T s !
B
r e e s
B i n a r y h e a p s , l e f t i s t h e a p s L i n k e d l i s t s , s k i p l i s t s H a s h t a b l e s
e h a s h i n g , l i n e a r p r
i n g , l i n e a r c h a i n i n g
a s h f u n c t i
s : t h e g
l
a g
h a s h f u n c t i
, b u t n
h
t
s t r u c t t h e m
T r i e s , r a d i x t r e e s , s u ffj x t r e e s G r a p h s ( w e i g h t e d , u n w e i g h t e d , d i r e c t e d , u n d i r e c t e d )
e p r e s e n t e d u s i n g a d j a c e n c y l i s t s
. g . : P e r f
m D i j k s t r a ’ s a l g
i t h m
t h i s g r a p h
. g . i n s e r t a v a l u e i n t
h i s A A t r e e
f t e n : g e t t i n g a 5 r e q u i r e s a b e t t e r t i m e c
p l e x i t y
c a n u s u a l l y a n s w e r i n p s e u d
e : d
’ t n e e d t
p e l l
t e v e r y l a s t d e t a i l , b u t a p r
r a m m e r s h
l d b e a b l e t
u r n i t i n t
e w i t h
t h a v i n g t
h i n k
c a n u s u a l l y u s e e x i s t i n g d a t a s t r u c t u r e s a n d a l g
i t h m s i n y
r s
u t i
s