740 likes | 890 Views
Last Class. Data Structure. To organize data to be able to perform operations on those data efficiently. Could be implemented in many different ways. The efficiency depend on the particular application. Algorithm.
E N D
Data Structure • To organize data to be able to perform operations on those data efficiently. • Could be implemented in many different ways. • The efficiency depend on the particular application.
Algorithm • It is a step-by-step procedure for solving a problem in a finite amount of time. • 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.
Algorithm • By inspecting the pseudo code, we count the number of primitive operation to analyze the running time. • We compare two algorithm by their asymptotic behaviour of their running time when the size of the input grow.
AsymptoticBehaviour • 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.
The Java Collections Framework 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 Generic algorithms that work on one or more interfaces - regardless of implementation
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<String> a = newArrayList<String>(); a.add("Hello"); a.add("World!"); System.out.println(a); ArrayList<Date> b = newArrayList<Date>(); b.add(new Date(0)); b.add(new Date()); System.out.println(b);
The Java Collections Framework ( Interfaces ) Two hierarchies: • Map: • Collection: • stores key/value pairs • 2 generic parameters • (type of key and type of value) • stores individual objects • 1 generic parameter • (type of object stored) Collection Map Set List Queue SortedMap SortedSet
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
Collection (example) Collection<String> names = newArrayList<String>(); names.add("sheldon"); names.add("penny"); System.out.println(names); System.out.println(names.size()); // 2 System.out.println(names.contains("leonard")); // false String[] newNames = {"howard","raj","wil","leslie","penny"}; for (String s : newNames ) names.add(s); names.addAll(Arrays.asList(newNames)); System.out.println(names.size()); // ? System.out.println(names); // ? 11
Iterators • All Collections have iterators • An iterator allows for listing the elements of a collection one by one // an example of the Collection iteration idiom publicstaticvoidprettyPrintStrings(Collection<String> c) { Iterator<String> i = c.iterator(); while (i.hasNext()) { System.out.print("**" + i.next() + "**"); } System.out.println(); } 12
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 // an example of a method that operates on generic collections publicstatic <T> voidprettyPrint(Collection<T> c) { Iterator<T> i = c.iterator(); while (i.hasNext()) { System.out.print("**" + i.next() + "**"); } System.out.println(); } 13
Collection Summary • For a Collection we cannot know • what happens to duplicate values • what order an iterator reports the elements in • Sets, Lists, and Queues treat these differently
Set A mathematical 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
Set (example) // Print the elements in a without printing any element twice publicstatic <T> voidprintWithoutDuplicates(T[] a) { Set<T> s = newHashSet<T>(); for (T x : a) { if (!s.contains(x)) { s.add(x); System.out.println(x); } } } In what order are the elements printed? 16
Set (example for a Collection) // Print the elements in c without printing any element twice publicstatic <T> voidprintWithoutDups(Collection<T> c) { Set<T> s = newHashSet<T>(); for (T x : c) { if (!s.contains(x)) { s.add(x); System.out.println(x); } } } 17
Set (example) // Removes duplicate elements from an array // Extra array positions are set to null publicstatic <T> void removeDuplicates(T[] a) { Set<T> s = newHashSet<T>(); for (T x : a) { s.add(x); } s.toArray(a); } // Removes duplicate elements from an array // Extra array positions are set to null publicstatic <T> void removeDuplicates(T[] a) {…} In what order do elements appear in the output array? 18
In-Class Exercise • 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 19
SortedSet • Just like Set, but elements are in sorted order An iterator outputs the elements in sorted order • We can use the natural ordering or define a Comparable object (more on this later) // Sort a and remove any duplicate elements // Fill extra positions with null publicstatic <T> voidsortAndRemoveDups(T[] a) { SortedSet<T> s = newTreeSet<T>(); for (T x : a) s.add(x); s.toArray(a); } 20
SortedSet • A SortedSet allows for range view • subSet(from,to), headSet(to), tailSet(from) • first(), last() SortedSet<String> snames = newTreeSet<String>(); 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")); 21
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 // Return a list of possible completions for a string s public void findCompletions(SortedSet<String> words, String s) { SortedSet<String> ts = words.tailSet(s); Iterator<String> i = ts.iterator(); String w; while (i.hasNext() && (w = i.next()).startsWith(s)) System.out.println(w); } 22
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
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)
List (example) List<String> l = newArrayList<String>(); 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)); 25
ListIterator (example) • ListIterators can iterate forward and backward • Forward: hasNext(), next() • Backward: hasPrevious, previous() ListIterator<String> it = l.listIterator(); while (it.hasNext()) System.out.print(it.next() + " "); System.out.println(); while (it.hasPrevious()) System.out.print(it.previous() + " "); 26
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)
Arrays as Lists • An array can be made into a list in one line • Arrays.asList • Set methods modify the original array String[] bus = {"Apple","Google","IBM","Microsoft","Oracle"}; List<String> 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" 28
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
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:
Queue (example) // An implementation of Unix 'tail' command // writes the last n lines of r onto w publicstaticvoid tail(BufferedReader r, PrintWriter w, int n) { String line; Queue<String> q = newLinkedList<String>(); while ((line = r.readLine()) != null) { q.add(line); if (q.size() > n) q.remove(); } while (!q.isEmpty()) { w.println(q.remove()); } } 31
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
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()
Map (example) - NASDAQ Map<String,String> m = newHashMap<String,String>(); 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()); 34
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<String,ArrayList<String>>
Storing Multiple Values per Key Map<String,Collection<String>> m2 = newHashMap<String,Collection<String>>(); m2.put("Shakespeare", newArrayList<String>()); m2.get("Shakespeare").add("Romeo and Juliet"); m2.get("Shakespeare").add("Macbeth"); m2.put("Bacon", newArrayList<String>()); 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")); 36
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
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()
Which Collection to use (not exact) • Associating keys with values? • Yes (Map): Exact search only? • Yes:Map • No:Sorted Map • No (Collection): Elements are ordered? • Yes:Sorted Order? • Yes:SortedSet or SortedMap • No:List • No:Sorted? • Yes:SortedSet or SortedMap • No:Set or Map
Mutable Objects and Collections • Objects stored in Setsor used as keysin Mapsshould 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.
Don’t do this Set<Person> s = new Set<Person>(); Person p = new Person("John", "Doe"); s.add(p); p.changeLastName("Smith"); // 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. 41
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: • Ifa.equals(b) thena.hashCode() =b.hashCode() • Objects stored in SortedSets must implement the Comparable interface • a.compareTo(b) < 0 if a < b • a.compareTo(b) > 0 if a > b • a.compareTo(b) = 0 if a = b
Comparators • To use something other than default ordering we can define a Comparator • An object that implements the Comparator<T> 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
Comparator (example) publicclassReverseStringComparator implements Comparator<String> { publicint compare(String a, String b) { int r = a.compareTo(b); if (r < 0) return 1; if (r > 0) return -1; return 0; } } ... SortedSet<String> r = newTreeSet<String>(newReverseStringComparator()); 44
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
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
Summary of Implementations Collection Set SortedSet Map SortedMap List Queue
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
LinkedHashSet example // Print the elements in T without printing any element // twice, in order of first occurrence in a publicstatic <T> voidprintInOrderWithoutDups(T[] a) { Set<T> s = newLinkedHashSet<T>(); for (T x : a) { s.add(x); } for (T x : s) { System.out.println(x); } }
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