Download presentation
Presentation is loading. Please wait.
Published byEgbert Price Modified over 9 years ago
1
Last Class
2
To organize data to be able to perform operations on those data efficiently. Data Structure Could be implemented in many different ways. The efficiency depend on the particular application.
3
It is a step-by-step procedure for solving a problem in a finite amount of time. Algorithm Its running time depend on the input size and on the input data The best case is the input that cause the lower running time and the worst case the input that cause the biggest running time.
4
By inspecting the pseudo code, we count the number of primitive operation to analyze the running time. Algorithm We compare two algorithm by their asymptotic behaviour of their running time when the size of the input grow.
5
Asymptotic Behaviour Big Oh define an asymptotically upper bound Big Omega define an asymptotically lower bound A function f(n) is Θ(g(n)) if it is both big Oh and big Omega.
6
Comming up
7
The Java Collections Framework Generic algorithms that work on one or more interfaces - regardless of implementation The JCF consists of: Interfaces The basic data types and their operations ( Set, List, Map,...) Implementations Classes that implement interfaces Different implementations have different performance ( tradeoffs ) Algorithms
8
Java generics Many classes, interfaces, and methods are generic They work on a variety of data types Each instance works for a specific data type ArrayList a = new ArrayList (); a.add("Hello"); a.add("World!"); System. out.println(a); ArrayList b = new ArrayList (); b.add(new Date(0)); b.add(new Date()); System. out.println(b);
9
The Java Collections Framework ( Interfaces ) CollectionMap SetListQueue SortedSet SortedMap Two hierarchies: Collection: − stores individual objects 1 generic parameter (type of object stored) Map: − stores key/value pairs 2 generic parameters (type of key and type of value)
10
Collection A group of objects − no particular order − possibly with duplicates Basic operations − size, isEmpty, contains, add, remove, iterator Bulk operations − containsAll, addAll, removeAll, retainAll, clear Array operations − toArray
11
11 1. Collection names = new ArrayList (); 2. names.add("sheldon"); 3. names.add("penny"); 4. System.out.println(names); 5. System.out.println(names.size()); // 2 6. System.out.println(names.contains("leonard")); // false 7. String[] newNames = {"howard","raj","wil","leslie","penny"}; 8. for (String s : newNames ) names.add(s); 9. names.addAll(Arrays.asList(newNames)); 10. System.out.println(names.size()); // ? System.out.println(names); // ? Collection (example)
12
12 // an example of the Collection iteration idiom public static void prettyPrintStrings(Collection c) { Iterator i = c.iterator(); while (i.hasNext()) { System.out.print("**" + i.next() + "**"); } System.out.println(); } Iterators All Collections have iterators − An iterator allows for listing the elements of a collection one by one
13
13 // an example of a method that operates on generic collections public static void prettyPrint(Collection c) { Iterator i = c.iterator(); while (i.hasNext()) { System.out.print("**" + i.next() + "**"); } System.out.println(); } Generic (example) If we don't care what kind of objects are in a Collection we can write a method that operates on generic collections
14
Collection Summary Sets, Lists, and Queues treat these differently For a Collection we cannot know −what happens to duplicate values −what order an iterator reports the elements in
15
Set All elements are unique − An element only occurs once in a set - even if added multiple times No order is associated with the elements − The order of elements in an iteration is unspecified Supports the same operations as Collection − size, isEmpty, contains, add, remove, iterator, containsAll, addAll, removeAll, retainAll, clear, toArray A mathematical set
16
16 // Print the elements in a without printing any element twice public static void printWithoutDuplicates(T[] a) { Set s = new HashSet (); for (T x : a) { if (!s.contains(x)) { s.add(x); System.out.println(x); } Set (example) In what order are the elements printed?
17
17 // Print the elements in c without printing any element twice public static void printWithoutDups(Collection c) { Set s = new HashSet (); for (T x : c) { if (!s.contains(x)) { s.add(x); System.out.println(x); } Set (example for a Collection)
18
18 // Removes duplicate elements from an array // Extra array positions are set to null public static void removeDuplicates(T[] a) { Set s = new HashSet (); for (T x : a) { s.add(x); } s.toArray(a); } Set (example) In what order do elements appear in the output array? // Removes duplicate elements from an array // Extra array positions are set to null public static void removeDuplicates(T[] a) {…}
19
19 Implement the following function Input: an array a Output: print each element of a that occurs exactly once – Don't print any element not in a – Don't print any element that occurs more than once in a In-Class Exercise
20
20 // Sort a and remove any duplicate elements // Fill extra positions with null public static void sortAndRemoveDups(T[] a) { SortedSet s = new TreeSet (); for (T x : a) s.add(x); s.toArray(a); } SortedSet Just like Set, but elements are in sorted order We can use the natural ordering or define a Comparable object (more on this later) An iterator outputs the elements in sorted order
21
21 SortedSet snames = new TreeSet (); String[] names = hlprsw {"sheldon”,"penny“,"howard","raj","wil","leslie","penny"}; snames.addAll(names); System.out.println(snames); System.out.println(snames.subSet("leslie", "sheldon")); System.out.println(snames.headSet("penny")); System.out.println(snames.tailSet("penny")); SortedSet A SortedSet allows for range view − subSet(from,to), headSet(to), tailSet(from) − first(), last()
22
22 // Return a list of possible completions for a string s public void findCompletions(SortedSet words, String s) { SortedSet ts = words.tailSet(s); Iterator i = ts.iterator(); String w; while (i.hasNext() && (w = i.next()).startsWith(s)) System.out.println(w); } SortedSet SortedSet supports searches for elements not in the set − s.headSet(x).last() is the largest value less than x − s.tailSet(x).first() is the smallest value greater than or equal to x
23
Set Summary Elements are unique. No order is associated with the elements. Support the same operation that Collection SortedSet Summary Like Set, but elements are in sorted order Support range view operations supports searches for elements not in the set
24
List Represents a sequence of elements − Elements are stored in a specific order − Elements can occur more than once Positional access − get(i), set(i,x), add(i,x), add(x), remove(i), addAll(i,c) Searching and iteration − indexOf(o), lastIndexOf(o), listIterator(), listIterator(i) Range view −subList(i, j)
25
25 List l = new ArrayList (); String[] bus = {"Apple","Google","IBM","Microsoft","Oracle"}; for (String s : bus) l.add(s); System.out.println(l); l.add("Yahoo"); // append System.out.println(l); l.add(1, "Cognos"); // add as second element System.out.println(l); l.set(1, "Cisco"); // replace second element System.out.println(l.get(0) + " " + l.get(1) + " " + l.get(2)); System.out.println(l.subList(0,3)); List (example)
26
26 ListIterator it = l.listIterator(); while (it.hasNext()) System.out.print(it.next() + " "); System.out.println(); while (it.hasPrevious()) System.out.print(it.previous() + " "); ListIterator (example) ListIterators can iterate forward and backward − Forward: hasNext(), next() − Backward: hasPrevious, previous()
27
List Summary Like arrays −Store a sequence of elements in a particular order −Can be accessed by position −Elements can be modified Not Like arrays −Elements can be added at the end −Elements can be inserted in the middle and front Performance −An implementation can be fast at one or the other, but not both (more later)
28
28 String[] bus = {"Apple","Google","IBM","Microsoft","Oracle"}; List l2 = Arrays.asList(bus); System.out.println(l2); System.out.println(bus[1]); // "Google" l2.set(1, "Hewlett-Packard"); System.out.println(l2); System.out.println(bus[1]); // "Hewlett-Packard" Arrays as Lists An array can be made into a list in one line − Arrays.asList − Set methods modify the original array
29
List Algorithms A number of algorithms are implemented that apply to lists −Collections.sort, Collections.shuffle, −Collections.reverse, Collections.rotate, −Collections.swap, Collections.replaceAll, −Collections.fill,Collections.copy, −Collections.binarySearch, −Collections.indexOfSublist, −Collections.lastIndexOfSublist We will use these in examples later
30
Queue A queue stores elements for processing − Usually elements come out in the same order they went in (FIFO) − Add an element to the end of the queue and remove (or look at) an element at the front of the queue Two versions of each method: Throws ExceptionsReturns Special Value Insertadd(e)offer(e) Removeremove()poll() Examineelement()peek()
31
31 // An implementation of Unix 'tail' command // writes the last n lines of r onto w public static void tail(BufferedReader r, PrintWriter w, int n) { String line; Queue q = new LinkedList (); while ((line = r.readLine()) != null) { q.add(line); if (q.size() > n) q.remove(); } while (!q.isEmpty()) { w.println(q.remove()); } Queue (example)
32
Collection Exercises Write a function that takes a Collection of Strings and returns a List of strings in the collection whose length is greater than 5. Write a function that takes a Collection of Strings and prints them in sorted order, eliminating duplicates Write a function that takes a Collection of Strings, selects one at random and returns it
33
Maps A Map associates keys of type K onto values of type V −Similar to an array/list except that elements of type K are used for indexing, rather than integers Basic operations −put(k,v), get(k), remove(k), containsKey(k), containsValue(v), size(), isEmpty() Collection views −keySet(), values(), entrySet()
34
34 Map m = new HashMap (); m.put("GOOG", "Google Inc."); m.put("AAPL", "Apple Inc."); m.put("MSFT", "Microsoft Corporation"); m.put("ORCL", "Oracle Corporation"); m.put("CSCO", "Cisco Systems, Inc."); System.out.println("Stock symbol GOOG is for " + m.get("GOOG")); System.out.println("Stock symbol PLCS is for " + m.get("PLCS")); System.out.println("Symbols: " + m.keySet()); System.out.println("Companies: " + m.values()); System.out.println("Mappings: " + m.entrySet()); Map (example) - NASDAQ
35
Maps The keys in a map are all unique, values may not be unique To store multiple values for each key, associate a collection with each key −e.g. Map >
36
36 Map > m2 = new HashMap >(); m2.put("Shakespeare", new ArrayList ()); m2.get("Shakespeare").add("Romeo and Juliet"); m2.get("Shakespeare").add("Macbeth"); m2.put("Bacon", new ArrayList ()); m2.get("Bacon").add("New Atlantis"); m2.get("Bacon").add("The Comedy of Errors(?)"); System.out.println("William Shakespeare wrote: " + m2.get("Shakespeare")); System.out.println("Sir Francis Bacon wrote: " + m2.get("Bacon")); Storing Multiple Values per Key
37
In-Class Exercises Implement the following function. Input: an array a Output: print each element of a that occurs at most 10 times − Don't print any element not in a − Don't print any element that occurs more than 10 times
38
SortedMap Identical to a Map, except that keys are stored in sorted order Operations that use order − subMap(from,to), − headMap(to), − tailMap(from), − firstKey(), − lastKey()
39
Which Collection to use (not exact) Associating keys with values? − Yes (Map): Exact search only ? − No (Collection): Elements are ordered? o Yes: Map o No: Sorted Map − Yes: Sorted Order ? o Yes: SortedSet or SortedMap o No: List − No: Sorted ? o Yes: SortedSet or SortedMap o No: Set or Map
40
Mutable Objects and Collections Objects stored in Sets or used as keys in Maps should not be changed − The object should not be modified in a way that changes the results of equals() or hashCode() − Ideally, they should be immutable (unmodifiable) Objects stored in SortedSets and used as keys in SortedMaps should not be changed in a way that modifies their order − The result of compareTo() should not change while the object is stored.
41
41 Set s = new Set (); Person p = new Person("John", "Doe"); s.add(p); p.changeLastName("Smith"); // Don't do this! Don’t do this p gets stored in s at a location that is defined by the contents (firstName and lastName) of p Changing the contents (lastName) of p means that p is no longer stored at the correct location in s.
42
equals, compareTo, and hashCode The equals() method can be overridden for any object − In this case, you must also override the hashCode() method to guarantee that: − a.compareTo(b) < 0 if a < b − a.compareTo(b) > 0 if a > b − a.compareTo(b) = 0 if a = b o If a.equals(b) then a.hashCode() = b.hashCode() Objects stored in SortedSets must implement the Comparable interface
43
Comparators To use something other than default ordering we can define a Comparator An object that implements the Comparator interface implements the compare(t1,t2) method that compares two elements of class T Most SortedSet implementations allow for the specification of a Comparator during initialization
44
44 public class ReverseStringComparator implements Comparator { public int compare(String a, String b) { int r = a.compareTo(b); if (r < 0) return 1; if (r > 0) return -1; return 0; }... SortedSet r = new TreeSet (new ReverseStringComparator()); Comparator (example)
45
Summary: JCF Interfaces Sets for representing mathematical sets −Unordered −No duplicates Lists for representing sequences −Order matters −Positional operators Maps for mapping keys onto values −keys form a Set Sorted versions of Set and Map −allows searching for elements/keys that are not present
46
In-Class Exercises Write an application that reads a text file and parses it into words − The application counts the number of occurrences of each word − The top 50 words, ordered in decreasing order by number of occurrences, are printed
47
47 Interface Implementation Technique Hash table ArrayTreeLinked list Hash table + Linked list SetHashSetTreeSetLinkedHashSet Sorted SetTreeSet ListArrayListLinkedList QueuePriorityQueueLinkedList MapHashMapTreeMapLinkedHashMap Sorted Map TreeMap Summary of Implementations Collection Map Set List Queue SortedSet SortedMap
48
48 HashSet and LinkedHashSet are both fast – add(x), remove(x), contains(x), size(), isEmpty() all execute in constant time on average Set: HashSet or LinkedHashSet LinkedHashSet remembers order elements are added – iteration reproduces this order
49
49 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); }
50
50 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 Map: HashMap or LinkedHashMap LinkedHashMap remembers order keys are added – keySet() is a LinkedHashSet
51
51 ArrayList – represents a list as an array – lookups are fast but modifications are slow – fast random-access ( get(i), set(i,x) ) – additions and removals can be slow ArrayList or LinkedList LinkedList – represents a list as a doubly-linked list – lookups are slow but modifications are fast – random-access can be slow ( get(i), set(i,x) ) – additions and removals are fast (constant time)
52
52 A list represented as an array – Usually the array has room for more elements ArrayList 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
53
53 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 ArrayList (Cont'd) bcdea bcdexa add(1,x)
54
remove(i) is slow when i << size() – elements have to be shifted. – requires moving size(x) – i – 1 array values ArrayList (Cont'd) bcdea bcdexa remove(1)
55
55 Fast when – a lot of random access is needed – additions and removals are at or near the end of the list. ArrayList summary Provides a fast implementation of – an array that can grow and shrink if needed – a stack push(x) → add(x) pop() → remove(size()-1);
56
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 LinkedList abcdef
57
57 get(i), set(i,x) are fast when i is small LinkedList(Cont'd) abcdef get(0) abcdef get(1)
58
add(i,x) are fast when i is small – find list node i-1 – modify next/previous at node i-1, i, and new node for x LinkedList(Cont'd) abcdef add(1,x) abcdef x
59
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 LinkedList(Cont'd)
60
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 LinkedList(Cont'd) abcdef remove(4) abcdef
61
add(i,x), remove(i), get(i), set(i,x) must traverse – min{i, size()-i-1} elements Fast when i ~ 0 or i ~ size() Slow when i >> 0 and i << size() LinkedList(Cont'd) ab yz l.get(l.size()/2) m...
62
LinkedLists are good for implementing LinkedList(Cont'd) −Stacks −Queues −Dequeues
63
63 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 ListIterators
64
ArrayList – Fast to get to the location you want – Slow to insert or remove at that location except at the end (back). LinkedList versus ArrayList summary 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
65
Among ArrayList or LinkedList which type of implementation make this code faster? Excercises public static void frontGets(List l, int n) { for (int i = 0; i < n; i++) { l.get(0); }
66
Among ArrayList or LinkedList which type of implementation make this code faster? Excercises public static void backGets(List l, int n) { for (int i = 0; i < n; i++) { l.get(l.size()-1); }
67
Among ArrayList or LinkedList which type of implementation make this code faster? Excercises 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())); }
68
Among ArrayList or LinkedList which type of implementation make this code faster? Excercises public static void insertAtBack(List l, int n) { l.clear(); for (int i = 0; i < n; i++) { l.add(new Integer(i)); }
69
Among ArrayList or LinkedList which type of implementation make this code faster? Excercises public static void insertAtFront(List l, int n) { l.clear(); for (int i = 0; i < n; i++) { l.add(0, new Integer(i)); }
70
Among ArrayList or LinkedList which type of implementation make this code faster? Excercises 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)); }
71
Among ArrayList or LinkedList which type of implementation make this code faster? Excercises 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)); }
72
Unordered implementation of Set and Map – HashSet and HashMap summary 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
73
73 Interface Implementation Technique Hash table ArrayTreeLinked list Hash table + Linked list SetHashSetTreeSetLinkedHashSet Sorted SetTreeSet ListArrayListLinkedList QueuePriorityQueueLinkedList MapHashMapTreeMapLinkedHashMap Sorted Map TreeMap Summary of Implementations Collection Map Set List Queue SortedSet SortedMap
74
Next Class Array Based List Implementations
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.