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
play

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


  1. 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 )

  2. 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 e m e n t e d a s c l a s s e s , a l l f o u n d i n j a v a . u t i l . * ● L i s t s : A r r a y L i s t , L i n k e d L i s t ● Q u e u e s / s t a c k s : A r r a y D e q u e , L i n k e d L i s t ● S e t s / m a p s : H a s h S e t , H a s h M a p , T r e e S e t , T r e e M a p – a l s o : 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 o r e ● P r i o r i t y Q u e u e Tie s e c l a s s e s i m p l e m e n t v a r i o u s i n t e r f a c e s ● C o l l e c t i o n < E > – a c o l l e c t i o n o f v a l u e s ● I t e r a t o r < E > - f o r i t e r a t i n g o v e r a l l v a l u e s ● L i s t < E > - f o r s e q u e n t i a l a n d r a n d o m a c c e s s

  3. T h e C o l l e c t i o n i n t e r f a c e M o s t d a t a s t r u c t u r e s i m p l e m e n t C o l l e c t i o n , w h i c h p r o v i d e s m e t h o d s f o r a d d i n g a n d r e m o v i n g v a l u e s , c h e c k i n g f o r m e m b e r s h i p o f a v a l u e e t c . : interface Collection<E> extends Iterable<E> { boolean add(E e); boolean addAll(Collection<E> c); void clear(); boolean contains(Object o); boolean isEmpty(); boolean remove(Object o); boolean removeAll(Collection<?> c); int size(); Object[] toArray(); ... }

  4. T h e L i s t i n t e r f a c e List i s u s e d f o r c o l l e c t i o n s w h e r e e l e m e n t s o c c u r i n a n ( e . g . l i n k e d o r d e r 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); ... }

  5. U s e o f s u b l i s t I t e r a t i n g t h r o u g h a l l e l e m e n t s b e t w e e n i n d e x f r o m a n d i n d e x t o : for(E element: list.sublist(from, to)) { … } D e l e t i n g a l l e l e m e n t s b e t w e e n i n d e x f r o m a n d i n d e x t o : list.sublist(from, to).clear(); sublist Tii s w o r k s b e c a u s e r e t u r n s a v i e w o f t h e u n d e r l y i n g s t r u c t u r e , n o t a c o p y !

  6. P e r f o r m a n c e LinkedList ArrayList List a n d i m p l e m e n t a l l o p e r a t i o n s , e v e n t h o s e t h a t h a v e b a d p e r f o r m a n c e ! Tie f o l l o w i n g e x a m p l e s w o r k e v e n t h o u g h t h e y a r e n o t 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); }

  7. T h e S e t i n t e r f a c e Set Tie i n t e r f a c e i s j u s t t h e s a m e a s Collection : interface Set<E> extends Collection<E> { } Tii s i s b e c a u s e C o l l e c t i o n a l r e a d y c o n t a i n s s e t o p e r a t i o n s : a d d , r e m o v e , c o n t a i n s e t c . Tie d i fg e r e n c e i s : i f a c l a s s i m p l e m e n t s S e t , i t s h o u l d b e g u a r a n t e e d t h a t d u p l i c a t e e l e m e n t s a r e i g n o r e d

  8. T h e M a p i n t e r f a c e Map<K,V> : I m p o r t a n t m e t h o d s o f 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 F o r e x a m p l e , y o u c a n i t e r a t e o v e r a l l k e y s o f a m a p l i k e s o : HashMap<K, V> map = …; for (K key: map.keySet()) { … }

  9. I t e r a t o r s I n J a v a , y o u c a n l o o p o v e r a l l k i n d s o f d a t a s t r u c t u r e s : ArrayList<E> list; for (E x: list) { … } LinkedList<E> list; for (E x: list) { … } HashMap<K, V> map; for (K x: map.keySet()) { … } Tia t ’ s b e c a u s e a l l t h e s e s t r u c t u r e s i m p l e m e n t Iterable .

  10. I t e r a t o r a n d I t e r a b l e Iterator A n i s a n o b j e c t t h a t r e p r e s e n t s a s e q u e n c e o f i t e m s b e i n g l o o p e d o v e r : interface Iterator<E> { // Return the next item in the iteration E next(); // Return true if the iteration has more elements boolean hasNext(); } next() , U s a g e : e a c h t i m e y o u c a l l i t g i v e s y o u t h e n e x t e l e m e n t i n t h e s e q u e n c e ● O n c e y o u h a v e g o n e t h r o u 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 Iterable A n h a s o n e m e t h o d f o r p r o d u c i n g a n i t e r a t o r : Iterator<E> iterator();

  11. I t e r a t o r a n d I t e r a b l e S u p p o s e l i s t i s a n A r r a y L i s t : ArrayList<Integer> list; Tie n t h e f o l l o w i n g c o d e l o o p s t h r o u g h a l l e l e m e n t s o f t h e l i s t : Iterator<Integer> iter = list.iterator(); while (iter.hasNext()) { int x = iter.next(); … } J a v a l e t s u s w r i t e t h i s a s a f o r - l o o p i n s t e a d ! for (int x: list) { … }

  12. L i s t I t e r a t o r C l a s s e s t h a t i m p l e m e n t L i s t s u p p o r t a s o u p e d - u p i t e r a t o r : ● L i s t I t e r a t o r < E > i t e r a t o r ( ) ; L i s t I t e r a t o r i s a n e x t e n s i o n o f I t e r a t o r t h a t s u p p o r t s : ● m o d i f y i n g t h e c u r r e n t e l e m e n t d u r i n g i t e r a t i o n ● a d d i n g a n d r e m o v i n g e l e m e n t s a t t h e c u r r e n t p o s i t i o n ● g o i n g b a c k a n d f o r w a r d i n t h e l i s t S o y o u c a n u s e t h i s t o d o a n y t h i n g y o u w o u l d w a n t t o d o w i t h a l i n k e d l i s t

  13. M i s c e l l a n e o u s b i t s a n d b o b s S o r t e d S e t , N a v i g a b l e S e t : e x t r a i n t e r f a c e s t h a t a r e i m p l e m e n t e d f o r s e t s t h a t a r e b a s e d o n o r d e r i n g ● S o r t e d S e t < E > s u b S e t ( E f r o m , E t o ) : r e t u r n a l l i t e m s b e t w e e n f r o m a n d t o ● E fm o o r ( E e ) : r e t u r n t h e g r e a t e s t i t e m l e s s t h a n e ● E p o l l F i r s t ( ) : r e m o v e a n d r e t u r n t h e s m a l l e s t e l e m e n t ● e t c . S o r t e d M a p , N a v i g a b l e M a p : s i m i l a r b u t f o r m a p s j a v a . u t i l . C o l l e c t i o n s : m a n y u s e f u l f u n c t i o n s d e fj n e d i n a g e n e r i c w a y , e . g . , b i n a r y s e a r c h , s o r t i n g , c o p y i n g , s h u ffm i n g , e t c .

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend