Per imparare a programmare bisogna programmare Brian Kernighan Java collections framework Commonly reusable collection data structures Abstract Data Type ADTs store data and allow various operations on the data to access and change it ADTs are mathematical models ADTs are extremely useful when designing a complex algorithms ADTs are not classes (well, not quite) Abstract Data Type ADTs are abstract because they specify the operations of the data structure and leave implementation details to later More similar to abstract classes or (whether the language supports them) Note: Not all implementation details can be deferred! 3 4 Why study ADTs? How many of you will actually go out and create your own ADT from scratch? Different ADTs, each one with its own pros and cons Picking the right one for the job is an important step in design! Get your data structures correct first, and the rest of the program will write itself David S. Johnson (winner of Knuth s Prize in 2010) Why study ADTs? The goal is to learn how to learn how to use and create ADTs 5 6 1
Built-in ADT High level languages often provide built in ADTs. E.g.: Standard Template Library (C++) Java Collections Framework (Java) Common Ground Almost every ADT provide a way to add an item remove an item find, retrieve, or access an item Most Collection ADTs provide more possibilities check whether the collection is empty make the collection empty give me a subset of the collection 7 8 A very simple ADT: Santa s Sack Sack s Operations inserttoy(toy) Insert a toy in the sack Duplicates are obviously allowed extracttoy(toy) Remove the given toy from the sack and make a children happy counttoys() Count how many toys actually are stored in the sack 9 10 Santa s Sack Santa s Sack (more efficient) inserttoy( ) counttoys() inserttoy( ) counttoys() extracttoy( ) extracttoy( ) keep track of the number of objects 11 12 2
The lesson ADTs do not specify the details of the implementation BUT Some information about the algorithms is essential to choose the right ADT Very high-level, qualitative information Complexity Java Collections Framework (JCF) Collection an object that represents a group of objects Collection Framework A unified architecture for representing and manipulating collections Such collections are manipulated independent of the details of their representation JCF vs. ADT 13 14 A little bit of history JDK < 1.2 Standard practice: Vector and Hashtable Compatibility with C++ Standard Template Library (STL) Doug Lea s Collections package ObjectSpace Generic Collection Library (JGL) JDK 1.2 Sun drops compatibility with C++ STL Joshua Bloch s JCF (now Chief Java Architect @ Google) A little bit of history Java 5 Introduction of <generics> Clean, safe definition of the Collection Interface Trees, linked lists, stacks, hash tables, and other classes are implementations of Collection Arrays do not implement the Vector redefined to implement Collection 15 16 A little bit of history Doug Lea later developed a concurrency package JCF s Main Elements Infrastructure Interfaces that provide essential support for the collection General-purpose Implementations Primary implementations (basic and bulk) of the collection 17 18 3
Algorithms Algorithms Static methods that perform useful functions on collections, such as sorting a list JCF s Utility Implementations Legacy Implementations The collection classes from earlier releases, Vector and Hashtable, have been retrofitted to implement the collection Convenience Implementations High-performance "mini-implementations" of the collection Wrapper Implementations Add functionality, such as synchronization, to other implementations 19 20 Abstract Implementations Partial implementations (skeletons) of the collection to facilitate custom implementations Infrastructure These form the basis of the framework Some types of collections allow duplicate elements, others do not Some types of collections are ordered, others are unordered The Java platform doesn t provide any direct implementations of the, but provides implementations of more specific sub-, such as Set and List and Maps 21 22 A Collection represents a group of objects known as its elements The is the least common denominator that all collections implement. It is Used to pass collections around to manipulate them when maximum generality is desire Collection extends Iterable A note on iterators An Iterator is an object that enables you to traverse through a collection (and to remove elements from the collection selectively) You get an Iterator for a collection by calling its iterator() method. Several languages supports iterators. E.g., C++, PHP, Python, Ruby, Go public interface Iterator<E> { boolean hasnext(); E next(); void remove(); 23 24 4
Main Interfaces List A more flexible version of an array Queue & Priority Queue The order of arrival does matter, or the urgency Set No order, no duplicate elements A Map is an object that maps keys to values A map cannot contain duplicate keys: each key can map to at most one value Map does not extend Iterable, but it is possible to get an iterator through entryset() Notez bien: Maps do not extend from java.util.collection, but they re still considered to be part of the collections framework 25 26 Collection Family Tree Iterator<E> iterator(); boolean addall(collection<? extends E> c); boolean removeall(collection<?> c); boolean retainall(collection<?> c); 27 28 Basic Operations generics Iterator<E> iterator(); boolean addall(collection<? extends E> c); boolean removeall(collection<?> c); boolean retainall(collection<?> c); wildcard Iterator<E> Bulk Operations iterator(); boolean addall(collection<? extends E> c); boolean removeall(collection<?> c); boolean retainall(collection<?> c); either extends or implements 29 30 5
Iterator<E> iterator(); Map Family Tree boolean addall(collection<? extends E> c); boolean removeall(collection<?> c); boolean retainall(collection<?> c); Array Operations 31 32 public interface Map<K,V> { V put(k key, V value); V get(object key); V remove(object key); boolean containskey(object key); boolean containsvalue(object value); void putall(map<? extends K,? extends V> m); Basic Operations public interface Map<K,V> { V put(k key, V value); V get(object key); V remove(object key); boolean containskey(object key); boolean containsvalue(object value); Bulk Operations void putall(map<? extends K,? extends V> m); 33 34 Collection Views public Set<K> keyset(); public Collection<V> values(); public Interface Set<Map.Entry<K,V>> for entryset elements entryset(); public Set<K> keyset(); public Collection<V> values(); public Set<Map.Entry<K,V>> entryset(); public interface Entry { K getkey(); V getvalue(); V setvalue(v value); public interface Entry { K getkey(); V getvalue(); V setvalue(v value); 35 36 6
Licenza d uso Queste diapositive sono distribuite con licenza Creative Commons Attribuzione - Non commerciale - Condividi allo stesso modo (CC BY-NC-SA) Sei libero: di riprodurre, distribuire, comunicare al pubblico, esporre in pubblico, rappresentare, eseguire e recitare quest'opera di modificare quest'opera Alle seguenti condizioni: Attribuzione Devi attribuire la paternità dell'opera agli autori originali e in modo tale da non suggerire che essi avallino te o il modo in cui tu usi l'opera. Non commerciale Non puoi usare quest'opera per fini commerciali. Condividi allo stesso modo Se alteri o trasformi quest'opera, o se la usi per crearne un'altra, puoi distribuire l'opera risultante solo con una licenza identica o equivalente a questa. http://creativecommons.org/licenses/by-nc-sa/3.0/ 37 38 7