Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP2402 The Java Collections Framework II: Implementations and Algorithms Pat Morin.

Similar presentations


Presentation on theme: "COMP2402 The Java Collections Framework II: Implementations and Algorithms Pat Morin."— Presentation transcript:

1 COMP2402 The Java Collections Framework II: Implementations and Algorithms Pat Morin

2 2 Summary of Implementations

3 3 Set: HashSet or LinkedHashSet HashSet and LinkedHashSet are both fast – add(x), remove(x), contains(x), size(), isEmpty() all execute in constant time on average LinkedHashSet remembers order elements are added – iteration reproduces this order

4 4 LinkedHashSet example // Print the elements in T without printing any element // twice, in order of first occurrence in a public static void printInOrderWithoutDups(T[] a) { Set s = new LinkedHashSet (); for (T x : a) { s.add(x); } for (T x : s) { System.out.println(x); }

5 5 Map: HashMap or LinkedHashMap HashMap and LinkedHashMap are both fast – put(k,v), get(k), remove(k), containsKey(k), size(), isEmpty() all execute in constant time on average – containsValue(v) is slow LinkedHashMap remembers order keys are added – keySet() is a LinkedHashSet

6 6 ArrayList or LinkedList ArrayList – represents a list as an array – lookups are fast but modifications are slow LinkedList – represents a list as a doubly-linked list – lookups are slow but modifications are fast

7 7 ArrayList or LinkedList ArrayList – fast random-access (get(i), set(i,x)) – additions and removals can be slow LinkedList – random-access can be slow (get(i), set(i,x)) – additions and removals are fast (constant time)

8 8 ArrayList A list represented as an array – Usually the array has room for more elements get(i), and set(i,x) are as fast as reading and writing a value in an array add(x) is usually fast – translates to set(size(),x); bcdea bcdexa

9 9 ArrayList (Cont'd) add(i,x) is slow when i << size() – elements have to be shifted to make room for x – requires moving size(x) - i array values bcdea bcdexa add(1,x)

10 10 ArrayList (Cont'd) remove(i) is slow when i << size() – elements have to be shifted – requires moving size(x) - i - 1 array values bcdea bcdexa remove(1)

11 11 ArrayList Summary Fast when – a lot of random access is needed – additions and removals are at or near the end of the list Provides a fast implementation of – an array that can grow and shrink if needed – a stack push(x) → add(x) pop() → remove(size()-1);

12 12 LinkedList Implements a List as a doubly-linked list – each node stores a reference to the next and previous element – has a pointer to the first and last elements abcdef

13 13 LinkedList (Cont'd) get(i), set(i,x) are fast when i is small abcdef get(0) abcdef get(1)

14 14 LinkedList (Cont'd) add(i,x) and remove(i) are fast when i is small – find list node i-1 – modify next/previous at node i-1, i, and new node for x abcdef add(1,x) abcdef x

15 15 LinkedList Removal remove(i) is fast when i is small – find node i - 1 – modify next/previous at nodes i-1 and i+1 abcdef remove(1) abcdef

16 16 LinkedList (Cont'd) add(i,x), add(x), remove(i), get(i), set(i,x) are fast when i is large – if i > size() / 2 then we traverse the list backwards abcdef remove(4) abcdef

17 17 LinkedList (Cont'd) get(i), set(i,x), add(i,x), remove(i) must traverse – min{i, size()-i-1} elements Fast when i ~ 0 or i ~ size() Slow when i >> 0 and i << size() ab yz l.get(l.size()/2) m...

18 18 LinkedList (Cont'd) LinkedLists are good for implementing – stacks – queues – deques

19 19 ListIterators The ListIterator interface provides – hasNext(), next(), hasPrevious(), previous(), nextIndex(), previousIndex() These are fast (constant-time) for both ArrayList and LinkedList – remove(), set(x), add(x) These are fast for LinkedList These can be slow for ArrayList

20 20 LinkedList versus ArrayList Summary ArrayList – Fast to get to the location you want – Slow to insert or remove at that location except at the end (back) LinkedList – Slow to get to the location you want except at the front and back; or if you have an iterator at that location – Fast to insert or remove at that location

21 21 Exercises Is this fast or slow if – l is an ArrayList? – l is a LinkedList? public static void frontGets(List l, int n) { for (int i = 0; i < n; i++) { l.get(0); }

22 22 Exercises Is this fast or slow if – l is an ArrayList? – l is a LinkedList? public static void backGets(List l, int n) { for (int i = 0; i < n; i++) { l.get(l.size()-1); }

23 23 Exercises Is this fast or slow if – l is an ArrayList? – l is a LinkedList? public static void randomGets(List l, int n) { Random gen = new Random(); for (int i = 0; i < n; i++) { l.get(gen.nextInt(l.size())); }

24 24 Exercises What about this? public static void insertAtBack(List l, int n) { l.clear(); for (int i = 0; i < n; i++) { l.add(new Integer(i)); }

25 25 Exercises And this? public static void insertAtFront(List l, int n) { l.clear(); for (int i = 0; i < n; i++) { l.add(0, new Integer(i)); }

26 26 Exercises How about this one? public static void insertInMiddle(List l, int n) { l.clear(); for (int i = 0; i < n; i++) { l.add(new Integer(i)); } for (int i = 0; i < n; i++) { l.add(n/2+i, new Integer(i)); }

27 27 Exercises The same example, but using an iterator public static void insertInMiddle2(List l, int n) { l.clear(); for (int i = 0; i < n; i++) { l.add(new Integer(i)); } ListIterator li = l.listIterator(n/2); for (int i = 0; i < n; i++) { li.add(new Integer(i)); }

28 28 Summary Unordered implementation of Set and Map – HashSet and HashMap SortedSet and SortedMap implementations – TreeSet and TreeMap Implementations of Set and Map that maintain insertion order – LinkedHashSet and LinkedHashMap List implementions – ArrayList: fast random access – LinkedList: fast insertion at an easy-to-get-to location

29


Download ppt "COMP2402 The Java Collections Framework II: Implementations and Algorithms Pat Morin."

Similar presentations


Ads by Google