![Page 1: Principles of Software Constructioncharlie/courses/15-214/2016-fall/slides/15... · The Design of the Collections API – Parts 1 & 2 Josh Bloch Charlie Garrod. 15-214 2 Administrivia](https://reader036.vdocuments.us/reader036/viewer/2022071004/5fc1a817b7d7216e3f6d9ed7/html5/thumbnails/1.jpg)
115-214
SchoolofComputerScience
PrinciplesofSoftwareConstruction:TheDesignoftheCollectionsAPI– Parts1&2
JoshBloch CharlieGarrod
![Page 2: Principles of Software Constructioncharlie/courses/15-214/2016-fall/slides/15... · The Design of the Collections API – Parts 1 & 2 Josh Bloch Charlie Garrod. 15-214 2 Administrivia](https://reader036.vdocuments.us/reader036/viewer/2022071004/5fc1a817b7d7216e3f6d9ed7/html5/thumbnails/2.jpg)
215-214
Administrivia
• Homework4bduetoday• GrabanAPIdesignquickreference!
– https://drive.google.com/open?id=0B941PmRjYRpnWDBYZTVhZkE5Vm8
![Page 3: Principles of Software Constructioncharlie/courses/15-214/2016-fall/slides/15... · The Design of the Collections API – Parts 1 & 2 Josh Bloch Charlie Garrod. 15-214 2 Administrivia](https://reader036.vdocuments.us/reader036/viewer/2022071004/5fc1a817b7d7216e3f6d9ed7/html5/thumbnails/3.jpg)
315-214
Wetakeyoubacknowtothelate‘90s
• Itwasasimplertime– JavahadonlyVector,Hashtable&Enumeration– Butitneededmore;platformwasgrowing!
• Thebarbarians werepoundingthegates– JGLwasatransliterationofSTLtoJava– Ithad130(!)classesandinterfaces– TheJGLdesignerswantedbadlytoputitintheJDK
• Itfelltometodesignsomethingbetter☺
![Page 4: Principles of Software Constructioncharlie/courses/15-214/2016-fall/slides/15... · The Design of the Collections API – Parts 1 & 2 Josh Bloch Charlie Garrod. 15-214 2 Administrivia](https://reader036.vdocuments.us/reader036/viewer/2022071004/5fc1a817b7d7216e3f6d9ed7/html5/thumbnails/4.jpg)
415-214
Here’sthefirstcollectionstalkever
• DebutedatJavaOne 1998• Nooneknewwhatacollectionsframeworkwas
– Orwhytheyneededone• Talkaimedto
– Explaintheconcept– SellJavaprogrammersonthisframework– Teachthemtouseit
![Page 5: Principles of Software Constructioncharlie/courses/15-214/2016-fall/slides/15... · The Design of the Collections API – Parts 1 & 2 Josh Bloch Charlie Garrod. 15-214 2 Administrivia](https://reader036.vdocuments.us/reader036/viewer/2022071004/5fc1a817b7d7216e3f6d9ed7/html5/thumbnails/5.jpg)
515-214
SchoolofComputerScience 5
The JavaTM PlatformCollections Framework
Joshua BlochSr. Staff Engineer, Collections Architect
Sun Microsystems, Inc.
![Page 6: Principles of Software Constructioncharlie/courses/15-214/2016-fall/slides/15... · The Design of the Collections API – Parts 1 & 2 Josh Bloch Charlie Garrod. 15-214 2 Administrivia](https://reader036.vdocuments.us/reader036/viewer/2022071004/5fc1a817b7d7216e3f6d9ed7/html5/thumbnails/6.jpg)
615-214
WhatisaCollection?
• Object that groups elements• MainUses
– Datastorage and retrieval– Datatransmission
• Familiar Examples– java.util.Vector– java.util.Hashtable– array 6
![Page 7: Principles of Software Constructioncharlie/courses/15-214/2016-fall/slides/15... · The Design of the Collections API – Parts 1 & 2 Josh Bloch Charlie Garrod. 15-214 2 Administrivia](https://reader036.vdocuments.us/reader036/viewer/2022071004/5fc1a817b7d7216e3f6d9ed7/html5/thumbnails/7.jpg)
715-214
WhatisaCollectionsFramework?
• UnifiedArchitecture– Interfaces- implementation-independence– Implementations- reusabledatastructures– Algorithms- reusablefunctionality
• Best-knownexamples– C++StandardTemplateLibrary(STL)– Smalltalkcollections
7
![Page 8: Principles of Software Constructioncharlie/courses/15-214/2016-fall/slides/15... · The Design of the Collections API – Parts 1 & 2 Josh Bloch Charlie Garrod. 15-214 2 Administrivia](https://reader036.vdocuments.us/reader036/viewer/2022071004/5fc1a817b7d7216e3f6d9ed7/html5/thumbnails/8.jpg)
815-214
Benefits
• Reducesprogrammingeffort• Increasesprogramspeedandquality• InteroperabilityamongunrelatedAPIs• ReducesefforttolearnnewAPIs• ReducesefforttodesignnewAPIs• Fosterssoftwarereuse
8
![Page 9: Principles of Software Constructioncharlie/courses/15-214/2016-fall/slides/15... · The Design of the Collections API – Parts 1 & 2 Josh Bloch Charlie Garrod. 15-214 2 Administrivia](https://reader036.vdocuments.us/reader036/viewer/2022071004/5fc1a817b7d7216e3f6d9ed7/html5/thumbnails/9.jpg)
915-214
DesignGoals
• Smallandsimple• Reasonablypowerful• Easilyextensible• Compatiblewithpreexistingcollections• Mustfeelfamiliar
9
![Page 10: Principles of Software Constructioncharlie/courses/15-214/2016-fall/slides/15... · The Design of the Collections API – Parts 1 & 2 Josh Bloch Charlie Garrod. 15-214 2 Administrivia](https://reader036.vdocuments.us/reader036/viewer/2022071004/5fc1a817b7d7216e3f6d9ed7/html5/thumbnails/10.jpg)
1015-214
ArchitectureOverview
• CoreCollectionInterfaces• General-PurposeImplementations• WrapperImplementations• AbstractImplementations• Algorithms
![Page 11: Principles of Software Constructioncharlie/courses/15-214/2016-fall/slides/15... · The Design of the Collections API – Parts 1 & 2 Josh Bloch Charlie Garrod. 15-214 2 Administrivia](https://reader036.vdocuments.us/reader036/viewer/2022071004/5fc1a817b7d7216e3f6d9ed7/html5/thumbnails/11.jpg)
1115-214
CoreCollectionInterfaces
11
![Page 12: Principles of Software Constructioncharlie/courses/15-214/2016-fall/slides/15... · The Design of the Collections API – Parts 1 & 2 Josh Bloch Charlie Garrod. 15-214 2 Administrivia](https://reader036.vdocuments.us/reader036/viewer/2022071004/5fc1a817b7d7216e3f6d9ed7/html5/thumbnails/12.jpg)
1215-214
Collection Interfacepublic interface Collection<E> {
int size();boolean isEmpty();boolean contains(Object element);boolean add(E element); // Optionalboolean remove(Object element); // OptionalIterator<E> iterator();
Object[] toArray();T[] toArray(T a[]);
// Bulk Operationsboolean containsAll(Collection<?> c);boolean addAll(Collection<? Extends E> c); // Optionalboolean removeAll(Collection<?> c); // Optionalboolean retainAll(Collection<?> c); // Optionalvoid clear(); // Optional
}
12
![Page 13: Principles of Software Constructioncharlie/courses/15-214/2016-fall/slides/15... · The Design of the Collections API – Parts 1 & 2 Josh Bloch Charlie Garrod. 15-214 2 Administrivia](https://reader036.vdocuments.us/reader036/viewer/2022071004/5fc1a817b7d7216e3f6d9ed7/html5/thumbnails/13.jpg)
1315-214
Iterator Interface
• ReplacementforEnumerationinterface– Addsremovemethod– Improvesmethodnames
public interface Iterator<E> {boolean hasNext();E next();void remove(); // Optional
}
![Page 14: Principles of Software Constructioncharlie/courses/15-214/2016-fall/slides/15... · The Design of the Collections API – Parts 1 & 2 Josh Bloch Charlie Garrod. 15-214 2 Administrivia](https://reader036.vdocuments.us/reader036/viewer/2022071004/5fc1a817b7d7216e3f6d9ed7/html5/thumbnails/14.jpg)
1415-214
Collection ExampleReusable algorithm to eliminate nullspublic static boolean removeNulls(Collection<?> c) {
for (Iterator<?> i = c.iterator(); i.hasNext(); ) {if (i.next() == null)
i.remove();}
}
14
![Page 15: Principles of Software Constructioncharlie/courses/15-214/2016-fall/slides/15... · The Design of the Collections API – Parts 1 & 2 Josh Bloch Charlie Garrod. 15-214 2 Administrivia](https://reader036.vdocuments.us/reader036/viewer/2022071004/5fc1a817b7d7216e3f6d9ed7/html5/thumbnails/15.jpg)
1515-214
Set Interface
• AddsnomethodstoCollection!• Addsstipulation:noduplicateelements• MandatesequalsandhashCode calculation
public interface Set<E> extends Collection<E> {}
15
![Page 16: Principles of Software Constructioncharlie/courses/15-214/2016-fall/slides/15... · The Design of the Collections API – Parts 1 & 2 Josh Bloch Charlie Garrod. 15-214 2 Administrivia](https://reader036.vdocuments.us/reader036/viewer/2022071004/5fc1a817b7d7216e3f6d9ed7/html5/thumbnails/16.jpg)
1615-214
Set IdiomsSet<Type> s1, s2;
boolean isSubset = s1.containsAll(s2);
Set<Type> union = new HashSet<>(s1);union = union.addAll(s2);
Set<Type> intersection = new HashSet<>(s1);intersection.retainAll(s2);
Set<Type> difference = new HashSet<>(s1);difference.removeAll(s2);
Collection<Type> c;Collection<Type> noDups = new HashSet<>(c);
![Page 17: Principles of Software Constructioncharlie/courses/15-214/2016-fall/slides/15... · The Design of the Collections API – Parts 1 & 2 Josh Bloch Charlie Garrod. 15-214 2 Administrivia](https://reader036.vdocuments.us/reader036/viewer/2022071004/5fc1a817b7d7216e3f6d9ed7/html5/thumbnails/17.jpg)
1715-214
List InterfaceAsequence of objectspublic interface List<E> extends Collection<E> {
E get(int index);E set(int index, E element); // Optionalvoid add(int index, E element); // OptionalObject remove(int index); // Optionalboolean addAll(int index, Collection<? extends E> c);
// Optional int indexOf(Object o);int lastIndexOf(Object o);
List<E> subList(int from, int to);
ListIterator<E> listIterator();ListIterator<E> listIterator(int index);
}
17
![Page 18: Principles of Software Constructioncharlie/courses/15-214/2016-fall/slides/15... · The Design of the Collections API – Parts 1 & 2 Josh Bloch Charlie Garrod. 15-214 2 Administrivia](https://reader036.vdocuments.us/reader036/viewer/2022071004/5fc1a817b7d7216e3f6d9ed7/html5/thumbnails/18.jpg)
1815-214
List ExampleReusable algorithms to swap and randomizepublic static <E> void swap(List<E> a, int i, int j) {
E tmp = a.get(i);a.set(i, a.get(j));a.set(j, tmp);
}
private static Random r = new Random();
public static void shuffle(List<?> a) {for (int i = a.size(); i > 1; i--)
swap(a, i - 1, r.nextInt(i));}
18
![Page 19: Principles of Software Constructioncharlie/courses/15-214/2016-fall/slides/15... · The Design of the Collections API – Parts 1 & 2 Josh Bloch Charlie Garrod. 15-214 2 Administrivia](https://reader036.vdocuments.us/reader036/viewer/2022071004/5fc1a817b7d7216e3f6d9ed7/html5/thumbnails/19.jpg)
1915-214
ListIdiomsList<Type> a, b;
// Concatenate two listsa.addAll(b);
// Range-removea.subList(from, to).clear();
// Range-extractList<Type> partView = a.subList(from, to);List<Type> part = new ArrayList<>(partView);partView.clear();
19
![Page 20: Principles of Software Constructioncharlie/courses/15-214/2016-fall/slides/15... · The Design of the Collections API – Parts 1 & 2 Josh Bloch Charlie Garrod. 15-214 2 Administrivia](https://reader036.vdocuments.us/reader036/viewer/2022071004/5fc1a817b7d7216e3f6d9ed7/html5/thumbnails/20.jpg)
2015-214
Map InterfaceAkey-value mappingpublic interface Map<K,V> {
int size();boolean isEmpty();boolean containsKey(Object key);boolean containsValue(Object value);Object get(Object key);Object put(K key, V value); // OptionalObject remove(Object key); // Optionalvoid putAll(Map<? Extends K, ? Extends V> t); // Opt.void clear(); // Optional// Collection Viewspublic Set<K> keySet();public Collection<V> values();public Set<Map.Entry<K,V>> entrySet();
}
20
![Page 21: Principles of Software Constructioncharlie/courses/15-214/2016-fall/slides/15... · The Design of the Collections API – Parts 1 & 2 Josh Bloch Charlie Garrod. 15-214 2 Administrivia](https://reader036.vdocuments.us/reader036/viewer/2022071004/5fc1a817b7d7216e3f6d9ed7/html5/thumbnails/21.jpg)
2115-214
MapIdioms// Iterate over all keys in Map mMap<Key, Val> m;for (iterator<Key> i = m.keySet().iterator(); i.hasNext(); )
System.out.println(i.next());
// As of Java 5 (2004)for (Key k : m.keySet())
System.out.println(i.next());
// "Map algebra" Map<Key, Val> a, b;boolean isSubMap = a.entrySet().containsAll(b.entrySet());Set<Key> commonKeys =
new HashSet<>(a.keySet()).retainAll(b.keySet); [sic!]//Remove keys from a that have mappings in ba.keySet().removeAll(b.keySet());
![Page 22: Principles of Software Constructioncharlie/courses/15-214/2016-fall/slides/15... · The Design of the Collections API – Parts 1 & 2 Josh Bloch Charlie Garrod. 15-214 2 Administrivia](https://reader036.vdocuments.us/reader036/viewer/2022071004/5fc1a817b7d7216e3f6d9ed7/html5/thumbnails/22.jpg)
2215-214
GeneralPurposeImplementationsConsistentNamingandBehavior
22
![Page 23: Principles of Software Constructioncharlie/courses/15-214/2016-fall/slides/15... · The Design of the Collections API – Parts 1 & 2 Josh Bloch Charlie Garrod. 15-214 2 Administrivia](https://reader036.vdocuments.us/reader036/viewer/2022071004/5fc1a817b7d7216e3f6d9ed7/html5/thumbnails/23.jpg)
2315-214
ChoosinganImplementation• Set
– HashSet -- O(1)access,noorderguarantee– TreeSet -- O(logn)access,sorted
• Map – HashMap -- (SeeHashSet)– TreeMap -- (SeeTreeSet)
• List – ArrayList -- O(1)randomaccess,O(n)insert/remove– LinkedList -- O(n)randomaccess,O(1)insert/remove;
• Useforqueuesanddeques (nolongeragoodidea!) 23
![Page 24: Principles of Software Constructioncharlie/courses/15-214/2016-fall/slides/15... · The Design of the Collections API – Parts 1 & 2 Josh Bloch Charlie Garrod. 15-214 2 Administrivia](https://reader036.vdocuments.us/reader036/viewer/2022071004/5fc1a817b7d7216e3f6d9ed7/html5/thumbnails/24.jpg)
2415-214
ImplementationBehaviorUnlike Vector and Hashtable…
• Fail-fastiterator• Nullelements,keys,valuespermitted• Not thread-safe
24
![Page 25: Principles of Software Constructioncharlie/courses/15-214/2016-fall/slides/15... · The Design of the Collections API – Parts 1 & 2 Josh Bloch Charlie Garrod. 15-214 2 Administrivia](https://reader036.vdocuments.us/reader036/viewer/2022071004/5fc1a817b7d7216e3f6d9ed7/html5/thumbnails/25.jpg)
2515-214
SynchronizationWrappersAnew approach to thread safety• Anonymousimplementations,onepercoreinterface• Staticfactoriestakecollectionofappropriatetype• Thread-safetyassuredifallaccessthroughwrapper• Mustmanuallysynchronizeiteration• Itwasnewthen;it’soldnow!
– Synchwrappersarelargelyobsolete– Madeobsoletebyconcurrentcollections
25
![Page 26: Principles of Software Constructioncharlie/courses/15-214/2016-fall/slides/15... · The Design of the Collections API – Parts 1 & 2 Josh Bloch Charlie Garrod. 15-214 2 Administrivia](https://reader036.vdocuments.us/reader036/viewer/2022071004/5fc1a817b7d7216e3f6d9ed7/html5/thumbnails/26.jpg)
2615-214
SynchronizationWrapperExampleSet<String> s = Collections.synchronizedSet(new HashSet<>());
...s.add("wombat"); // Thread-safe
...synchronized(s) {
Iterator<String> i = s.iterator(); // In synch block!while (i.hasNext())
System.out.println(i.next());}
// In Java 5 (post-2004)synchronized(s) {
for (String t : s)System.out.println(i.next());
}26
![Page 27: Principles of Software Constructioncharlie/courses/15-214/2016-fall/slides/15... · The Design of the Collections API – Parts 1 & 2 Josh Bloch Charlie Garrod. 15-214 2 Administrivia](https://reader036.vdocuments.us/reader036/viewer/2022071004/5fc1a817b7d7216e3f6d9ed7/html5/thumbnails/27.jpg)
2715-214
UnmodifiableWrappers
• Analogoustosynchronizationwrappers– Anonymousimplementations– Staticfactorymethods– Oneforeachcoreinterface
• Provideread-onlyaccess
27
![Page 28: Principles of Software Constructioncharlie/courses/15-214/2016-fall/slides/15... · The Design of the Collections API – Parts 1 & 2 Josh Bloch Charlie Garrod. 15-214 2 Administrivia](https://reader036.vdocuments.us/reader036/viewer/2022071004/5fc1a817b7d7216e3f6d9ed7/html5/thumbnails/28.jpg)
2815-214
ConvenienceImplementations
• Arrays.asList(E[] a) – Allowsarraytobe"viewed"asList– BridgetoCollection-basedAPIs
• EMPTY_SET,EMPTY_LIST,EMPTY_MAP– immutableconstants
• singleton(E o)– immutablesetwithspecifiedobject
• nCopies(E o)– immutablelistwithncopiesofobject
28
![Page 29: Principles of Software Constructioncharlie/courses/15-214/2016-fall/slides/15... · The Design of the Collections API – Parts 1 & 2 Josh Bloch Charlie Garrod. 15-214 2 Administrivia](https://reader036.vdocuments.us/reader036/viewer/2022071004/5fc1a817b7d7216e3f6d9ed7/html5/thumbnails/29.jpg)
2915-214
29
CustomImplementationIdeas
• Persistent• Highlyconcurrent• High-performance,special-purpose• Space-efficientrepresentations• Fancydatastructures• Convenienceclasses
![Page 30: Principles of Software Constructioncharlie/courses/15-214/2016-fall/slides/15... · The Design of the Collections API – Parts 1 & 2 Josh Bloch Charlie Garrod. 15-214 2 Administrivia](https://reader036.vdocuments.us/reader036/viewer/2022071004/5fc1a817b7d7216e3f6d9ed7/html5/thumbnails/30.jpg)
3015-214
CustomImplementationExampleIt’seasywith our abstract implementations// List adapter for primitive int arraypublic static List intArrayList(int[] a) {
return new AbstractList() {public Integer get(int i) {
return new Integer(a[i]);}
public int size() { return a.length; }
public Object set(int i, Integer e) {int oldVal = a[i];a[i] = e.intValue();return new Integer(oldVal);
}};
} 30
![Page 31: Principles of Software Constructioncharlie/courses/15-214/2016-fall/slides/15... · The Design of the Collections API – Parts 1 & 2 Josh Bloch Charlie Garrod. 15-214 2 Administrivia](https://reader036.vdocuments.us/reader036/viewer/2022071004/5fc1a817b7d7216e3f6d9ed7/html5/thumbnails/31.jpg)
3115-214
31
ReusableAlgorithms
static <T extends Comparable<? super T>> void sort(List<T> list);
static int binarySearch(List list, Object key);
static <T extends Comparable<? super T>> T min(Collection<T> coll);
static <T extends Comparable<? super T>> T max(Collection<T> coll);
static <E> void fill(List<E> list, E e);
static <E> void copy(List<E> dest, List<? Extends E> src);
static void reverse(List<?> list);
static void shuffle(List<?> list);
![Page 32: Principles of Software Constructioncharlie/courses/15-214/2016-fall/slides/15... · The Design of the Collections API – Parts 1 & 2 Josh Bloch Charlie Garrod. 15-214 2 Administrivia](https://reader036.vdocuments.us/reader036/viewer/2022071004/5fc1a817b7d7216e3f6d9ed7/html5/thumbnails/32.jpg)
3215-214
AlgorithmExample1Sorting lists of comparable elementsList<String> strings; // Elements type: String
...Collections.sort(strings); // Alphabetical order
LinkedList<Date> dates; // Elements type: Date...
Collections.sort(dates); // Chronological order
// Comparable interface (Infrastructure)public interface Comparable<E extends Comparable<E>> {
int compareTo(Object o);} 32
![Page 33: Principles of Software Constructioncharlie/courses/15-214/2016-fall/slides/15... · The Design of the Collections API – Parts 1 & 2 Josh Bloch Charlie Garrod. 15-214 2 Administrivia](https://reader036.vdocuments.us/reader036/viewer/2022071004/5fc1a817b7d7216e3f6d9ed7/html5/thumbnails/33.jpg)
3315-214
ComparatorInterfaceInfrastructure
• Specifiesorderamongobjects– Overridesnaturalorderoncomparables– Providesorderonnon-comparables
public interface Comparator<T> {public int compare(T o1, T o2);
}
33
![Page 34: Principles of Software Constructioncharlie/courses/15-214/2016-fall/slides/15... · The Design of the Collections API – Parts 1 & 2 Josh Bloch Charlie Garrod. 15-214 2 Administrivia](https://reader036.vdocuments.us/reader036/viewer/2022071004/5fc1a817b7d7216e3f6d9ed7/html5/thumbnails/34.jpg)
3415-214
AlgorithmExample2Sorting with acomparatorList<String> strings; // Element type: String
Collections.sort(strings, Collections.ReverseOrder());
// Case-independent alphabetical orderstatic Comparator<String> cia = new Comparator<>() {
public int compare(String c1, String c2) {return c1.toLowerCase().compareTo(c2.toLowerCase());
}};
Collections.sort(strings, cia); 34
![Page 35: Principles of Software Constructioncharlie/courses/15-214/2016-fall/slides/15... · The Design of the Collections API – Parts 1 & 2 Josh Bloch Charlie Garrod. 15-214 2 Administrivia](https://reader036.vdocuments.us/reader036/viewer/2022071004/5fc1a817b7d7216e3f6d9ed7/html5/thumbnails/35.jpg)
3515-214
CompatibilityOldand new collections interoperate freely
• UpwardCompatibility– Vector<E> implements List<E> – Hashtable<K,V> implements Map<K,V> – Arrays.asList(myArray)
• BackwardCompatibility– myCollection.toArray() – new Vector<>(myCollection) – new Hashtable<>(myMap)
35
![Page 36: Principles of Software Constructioncharlie/courses/15-214/2016-fall/slides/15... · The Design of the Collections API – Parts 1 & 2 Josh Bloch Charlie Garrod. 15-214 2 Administrivia](https://reader036.vdocuments.us/reader036/viewer/2022071004/5fc1a817b7d7216e3f6d9ed7/html5/thumbnails/36.jpg)
3615-214
APIDesignGuidelines
• Avoidadhoccollections– Inputparametertype:
• Anycollectioninterface(Collection,Map best)• Arraymaysometimesbepreferable
– Outputvaluetype:• Anycollectioninterfaceorclass• Array
• Provideadaptersforyourlegacycollections36
![Page 37: Principles of Software Constructioncharlie/courses/15-214/2016-fall/slides/15... · The Design of the Collections API – Parts 1 & 2 Josh Bloch Charlie Garrod. 15-214 2 Administrivia](https://reader036.vdocuments.us/reader036/viewer/2022071004/5fc1a817b7d7216e3f6d9ed7/html5/thumbnails/37.jpg)
3715-214
Sermon
• Programmers:– Usenewimplementationsandalgorithms– Writereusablealgorithms– Implementcustomcollections
• APIDesigners:– Takecollectioninterfaceobjectsasinput– Furnishcollectionsasoutput 37
![Page 38: Principles of Software Constructioncharlie/courses/15-214/2016-fall/slides/15... · The Design of the Collections API – Parts 1 & 2 Josh Bloch Charlie Garrod. 15-214 2 Administrivia](https://reader036.vdocuments.us/reader036/viewer/2022071004/5fc1a817b7d7216e3f6d9ed7/html5/thumbnails/38.jpg)
3815-214
38
ForMoreInformation
http://java.sun.com/products/jdk/1.2/docs/ guide/collections/index.html
![Page 39: Principles of Software Constructioncharlie/courses/15-214/2016-fall/slides/15... · The Design of the Collections API – Parts 1 & 2 Josh Bloch Charlie Garrod. 15-214 2 Administrivia](https://reader036.vdocuments.us/reader036/viewer/2022071004/5fc1a817b7d7216e3f6d9ed7/html5/thumbnails/39.jpg)
3915-214
Takeaways
• Collectionshaven’tchangedthatmuchsince‘98• APIhasgrown,butessentialcharacterunchanged
– WitharguableexceptionofJava8streams(2014)
![Page 40: Principles of Software Constructioncharlie/courses/15-214/2016-fall/slides/15... · The Design of the Collections API – Parts 1 & 2 Josh Bloch Charlie Garrod. 15-214 2 Administrivia](https://reader036.vdocuments.us/reader036/viewer/2022071004/5fc1a817b7d7216e3f6d9ed7/html5/thumbnails/40.jpg)
4015-214
Part2:Outline
I. TheinitialreleaseofthecollectionsAPIII. DesignofthefirstreleaseIII. EvolutionIV. CodeexampleV. Critique
![Page 41: Principles of Software Constructioncharlie/courses/15-214/2016-fall/slides/15... · The Design of the Collections API – Parts 1 & 2 Josh Bloch Charlie Garrod. 15-214 2 Administrivia](https://reader036.vdocuments.us/reader036/viewer/2022071004/5fc1a817b7d7216e3f6d9ed7/html5/thumbnails/41.jpg)
4115-214
Collectioninterfacesfirstrelease,1998
![Page 42: Principles of Software Constructioncharlie/courses/15-214/2016-fall/slides/15... · The Design of the Collections API – Parts 1 & 2 Josh Bloch Charlie Garrod. 15-214 2 Administrivia](https://reader036.vdocuments.us/reader036/viewer/2022071004/5fc1a817b7d7216e3f6d9ed7/html5/thumbnails/42.jpg)
4215-214
General-purposeimplementationsfirstrelease,1998
![Page 43: Principles of Software Constructioncharlie/courses/15-214/2016-fall/slides/15... · The Design of the Collections API – Parts 1 & 2 Josh Bloch Charlie Garrod. 15-214 2 Administrivia](https://reader036.vdocuments.us/reader036/viewer/2022071004/5fc1a817b7d7216e3f6d9ed7/html5/thumbnails/43.jpg)
4315-214
Otherimplementationsfirstrelease,1998
• Convenienceimplementations– Arrays.asList(Object[] a) – EMPTY_SET, EMPTY_LIST, EMPTY_MAP– singleton(Object o)– nCopies(Object o)
• Decoratorimplementations– Unmodifiable{Collection,Set,List,Map,SortedMap}– Synchronized{Collection,Set,List,Map,SortedMap}
• SpecialPurposeimplementation– WeakHashMap
![Page 44: Principles of Software Constructioncharlie/courses/15-214/2016-fall/slides/15... · The Design of the Collections API – Parts 1 & 2 Josh Bloch Charlie Garrod. 15-214 2 Administrivia](https://reader036.vdocuments.us/reader036/viewer/2022071004/5fc1a817b7d7216e3f6d9ed7/html5/thumbnails/44.jpg)
4415-214
Reusablealgorithmsfirstrelease,1998
• static void sort(List[]);
• static int binarySearch(List list, Object key);
• static object min(List[]);
• static object max(List[]);
• static void fill(List list, Object o);
• static void copy(List dest, List src);
• static void reverse(List list);
• static void shuffle(List list);
![Page 45: Principles of Software Constructioncharlie/courses/15-214/2016-fall/slides/15... · The Design of the Collections API – Parts 1 & 2 Josh Bloch Charlie Garrod. 15-214 2 Administrivia](https://reader036.vdocuments.us/reader036/viewer/2022071004/5fc1a817b7d7216e3f6d9ed7/html5/thumbnails/45.jpg)
4515-214
Andthat’salltherewastoit!
![Page 46: Principles of Software Constructioncharlie/courses/15-214/2016-fall/slides/15... · The Design of the Collections API – Parts 1 & 2 Josh Bloch Charlie Garrod. 15-214 2 Administrivia](https://reader036.vdocuments.us/reader036/viewer/2022071004/5fc1a817b7d7216e3f6d9ed7/html5/thumbnails/46.jpg)
4615-214
OK,Itoldalittlewhitelie:Arrayutilities, firstrelease,1998• static int binarySearch(type[] a, type key)
• static int binarySearch(Object[] a, Object key, Comparator c)
• static boolean equals(type[] a, type[] a2)
• static void fill(type[] a, type val)
• static void fill(type[] a, int fromIndex, int toIndex, type val)
• static void sort(type[] a)
• static void sort(type[] a, int fromIndex, int toIndex)
• static void sort(type[] a, Comparator c)
• static void sort(type[] a, int fromIdx, int toidx, Comparator c)
![Page 47: Principles of Software Constructioncharlie/courses/15-214/2016-fall/slides/15... · The Design of the Collections API – Parts 1 & 2 Josh Bloch Charlie Garrod. 15-214 2 Administrivia](https://reader036.vdocuments.us/reader036/viewer/2022071004/5fc1a817b7d7216e3f6d9ed7/html5/thumbnails/47.jpg)
4715-214
Documentationmatters
Reuseissomethingthatisfareasiertosaythantodo.Doingitrequiresbothgooddesignandverygooddocumentation.Evenwhenweseegooddesign,whichisstillinfrequently,wewon'tseethecomponentsreusedwithoutgooddocumentation.
- D.L.Parnas,SoftwareAging.Proceedingsofthe16thInternationalConferenceonSoftwareEngineering,1994
![Page 48: Principles of Software Constructioncharlie/courses/15-214/2016-fall/slides/15... · The Design of the Collections API – Parts 1 & 2 Josh Bloch Charlie Garrod. 15-214 2 Administrivia](https://reader036.vdocuments.us/reader036/viewer/2022071004/5fc1a817b7d7216e3f6d9ed7/html5/thumbnails/48.jpg)
4815-214
OfcourseyouneedgoodJavaDocButitisnotsufficientforasubstantialAPI
![Page 49: Principles of Software Constructioncharlie/courses/15-214/2016-fall/slides/15... · The Design of the Collections API – Parts 1 & 2 Josh Bloch Charlie Garrod. 15-214 2 Administrivia](https://reader036.vdocuments.us/reader036/viewer/2022071004/5fc1a817b7d7216e3f6d9ed7/html5/thumbnails/49.jpg)
4915-214
Asingleplacetogofordocumentation
![Page 50: Principles of Software Constructioncharlie/courses/15-214/2016-fall/slides/15... · The Design of the Collections API – Parts 1 & 2 Josh Bloch Charlie Garrod. 15-214 2 Administrivia](https://reader036.vdocuments.us/reader036/viewer/2022071004/5fc1a817b7d7216e3f6d9ed7/html5/thumbnails/50.jpg)
5015-214
OverviewsprovideunderstandingAplacetogowhenfirstlearninganAPI
![Page 51: Principles of Software Constructioncharlie/courses/15-214/2016-fall/slides/15... · The Design of the Collections API – Parts 1 & 2 Josh Bloch Charlie Garrod. 15-214 2 Administrivia](https://reader036.vdocuments.us/reader036/viewer/2022071004/5fc1a817b7d7216e3f6d9ed7/html5/thumbnails/51.jpg)
5115-214
AnnotatedoutlinesprovideaccessThey’reawesomeandunderutilized
![Page 52: Principles of Software Constructioncharlie/courses/15-214/2016-fall/slides/15... · The Design of the Collections API – Parts 1 & 2 Josh Bloch Charlie Garrod. 15-214 2 Administrivia](https://reader036.vdocuments.us/reader036/viewer/2022071004/5fc1a817b7d7216e3f6d9ed7/html5/thumbnails/52.jpg)
5215-214
Adesignrationalesavesyouhassleandprovidesatestamenttohistory
![Page 53: Principles of Software Constructioncharlie/courses/15-214/2016-fall/slides/15... · The Design of the Collections API – Parts 1 & 2 Josh Bloch Charlie Garrod. 15-214 2 Administrivia](https://reader036.vdocuments.us/reader036/viewer/2022071004/5fc1a817b7d7216e3f6d9ed7/html5/thumbnails/53.jpg)
5315-214
Outline
I. TheinitialreleaseofthecollectionsAPIII. DesignofthefirstreleaseIII. EvolutionIV. CodeexampleV. Critique
![Page 54: Principles of Software Constructioncharlie/courses/15-214/2016-fall/slides/15... · The Design of the Collections API – Parts 1 & 2 Josh Bloch Charlie Garrod. 15-214 2 Administrivia](https://reader036.vdocuments.us/reader036/viewer/2022071004/5fc1a817b7d7216e3f6d9ed7/html5/thumbnails/54.jpg)
5415-214
Awonderfulsourceofusecases“Goodartistscopy,greatartistssteal.”– PabloPicasso
![Page 55: Principles of Software Constructioncharlie/courses/15-214/2016-fall/slides/15... · The Design of the Collections API – Parts 1 & 2 Josh Bloch Charlie Garrod. 15-214 2 Administrivia](https://reader036.vdocuments.us/reader036/viewer/2022071004/5fc1a817b7d7216e3f6d9ed7/html5/thumbnails/55.jpg)
5515-214
Youmustmaintainanissueslist
• Centralizesallopenandcloseddesignissues• Listprosandconsforeachpossibledecision• Essentialforefficientprogress• Formsthebasisofadesignrationale
![Page 56: Principles of Software Constructioncharlie/courses/15-214/2016-fall/slides/15... · The Design of the Collections API – Parts 1 & 2 Josh Bloch Charlie Garrod. 15-214 2 Administrivia](https://reader036.vdocuments.us/reader036/viewer/2022071004/5fc1a817b7d7216e3f6d9ed7/html5/thumbnails/56.jpg)
5615-214
ThefirstdraftofAPIwasnotsonice
• MapwascalledTable• NoHashMap,onlyHashtable• Noalgorithms(Collections,Arrays)• Containedsomeunbelievablegarbage
![Page 57: Principles of Software Constructioncharlie/courses/15-214/2016-fall/slides/15... · The Design of the Collections API – Parts 1 & 2 Josh Bloch Charlie Garrod. 15-214 2 Administrivia](https://reader036.vdocuments.us/reader036/viewer/2022071004/5fc1a817b7d7216e3f6d9ed7/html5/thumbnails/57.jpg)
5715-214
/*** This interface must be implemented by Collections and Tables that are* <i>views</i> on some backing collection. (It is necessary to* implement this interface only if the backing collection is not* <i>encapsulated</i> by this Collection or Table; that is, if the* backing collection might conceivably be be accessed in some way other* than through this Collection or Table.) This allows users* to detect potential <i>aliasing</i> between collections.* <p>* If a user attempts to modify one collection* object while iterating over another, and they are in fact views on* the same backing object, the iteration may behave erratically.* However, these problems can be prevented by recognizing the* situation, and "defensively copying" the Collection over which* iteration is to take place, prior to the iteration.*/
public interface Alias {/*** Returns the identityHashCode of the object "ultimately backing" this* collection, or zero if the backing object is undefined or unknown.* The purpose of this method is to allow the programmer to determine* when the possiblity of <i>aliasing</i> exists between two collections* (in other words, modifying one collection could affect the other). This
* is critical if the programmer wants to iterate over one collection and* modify another; if the two collections are aliases, the effects of* the iteration are undefined, and it could loop forever. To avoid* this behavior, the careful programmer must "defensively copy" the* collection prior to iterating over it whenver the possibility of* aliasing exists.* <p>* If this collection is a view on an Object that does not impelement* Alias, this method must return the IdentityHashCode of the backing* Object. For example, a List backed by a user-provided array would* return the IdentityHashCode of the array.
* If this collection is a <i>view</i> on another Object that implements* Alias, this method must return the backingObjectId of the backing* Object. (To avoid the cost of recursive calls to this method, the* backingObjectId may be cached at creation time).* <p>* For all collections backed by a particular "external data source" (a* SQL database, for example), this method must return the same value.* The IdentityHashCode of a "proxy" Object created just for this* purpose will do nicely, as will a pseudo-random integer permanently* associated with the external data source.* <p>* For any collection backed by multiple Objects (a "concatenation* view" of two Lists, for instance), this method must return zero.* Similarly, for any <i>view</i> collection for which it cannot be* determined what Object backs the collection, this method must return* zero. It is always safe for a collection to return zero as its* backingObjectId, but doing so when it is not necessary will lead to* inefficiency.* <p>* The possibility of aliasing between two collections exists iff* any of the following conditions are true:<ol>* <li>The two collections are the same Object.* <li>Either collection implements Alias and has a* backingObjectId that is the identityHashCode of* the other collection.* <li>Either collection implements Alias and has a* backingObjectId of zero.* <li>Both collections implement Alias and they have equal* backingObjectId's.</ol>** @see java.lang.System#identityHashCode* @since JDK1.2*/
int backingObjectId();}
AutomaticaliasdetectionAhorribleideathatdiedonthevine
![Page 58: Principles of Software Constructioncharlie/courses/15-214/2016-fall/slides/15... · The Design of the Collections API – Parts 1 & 2 Josh Bloch Charlie Garrod. 15-214 2 Administrivia](https://reader036.vdocuments.us/reader036/viewer/2022071004/5fc1a817b7d7216e3f6d9ed7/html5/thumbnails/58.jpg)
5815-214
Ireceivedalot offeedback
• Initiallyfromasmallcircleofcolleagues– Somevery goodadvice– Somenotsogood
• Thenfromthepublicatlarge:betareleases– Hundredsofmessages– ManyAPIflawswerefixedinthisstage– Iputupwithalotofflaming
![Page 59: Principles of Software Constructioncharlie/courses/15-214/2016-fall/slides/15... · The Design of the Collections API – Parts 1 & 2 Josh Bloch Charlie Garrod. 15-214 2 Administrivia](https://reader036.vdocuments.us/reader036/viewer/2022071004/5fc1a817b7d7216e3f6d9ed7/html5/thumbnails/59.jpg)
5915-214
Reviewfromavery seniorengineerAPI vote notes=====================================================================Array yes But remove binarySearch* and toListBasicCollection no I don't expect lots of collection classesBasicList no see List belowCollection yes But cut toArrayComparator noDoublyLinkedList no (without generics this isn't worth it)HashSet noLinkedList no (without generics this isn't worth it)List no I'd like to say yes, but it's just way
bigger than I was expectingRemovalEnumeration noTable yes BUT IT NEEDS A DIFFERENT NAMETreeSet no
I'm generally not keen on the toArray methods because they add complexity
Simiarly, I don't think that the table Entry subclass or the variousviews mechanisms carry their weight.
![Page 60: Principles of Software Constructioncharlie/courses/15-214/2016-fall/slides/15... · The Design of the Collections API – Parts 1 & 2 Josh Bloch Charlie Garrod. 15-214 2 Administrivia](https://reader036.vdocuments.us/reader036/viewer/2022071004/5fc1a817b7d7216e3f6d9ed7/html5/thumbnails/60.jpg)
6015-214
III.EvolutionofJavacollectionsRelease,Year ChangesJDK1.0, 1996 JavaReleased:Vector,Hashtable,EnumerationJDK1.1,1996 (NoAPIchanges)J2SE1.2,1998 CollectionsframeworkaddedJ2SE1.3, 2000 (NoAPIchanges)J2SE1.4, 2002 LinkedHash{Map,Set},IdentityHashSet, 6newalgorithmsJ2SE5.0, 2004 Generics,for-each,enums: generified everything,Iterable
Queue,Enum{Set,Map},concurrentcollections
Java6,2006 Deque,Navigable{Set,Map},newSetFromMap,asLifoQueue
Java7, 2011 NoAPIchanges.Improvedsorts&defensivehashingJava8, 2014 Lambdas(+streamsandinternaliterators)
![Page 61: Principles of Software Constructioncharlie/courses/15-214/2016-fall/slides/15... · The Design of the Collections API – Parts 1 & 2 Josh Bloch Charlie Garrod. 15-214 2 Administrivia](https://reader036.vdocuments.us/reader036/viewer/2022071004/5fc1a817b7d7216e3f6d9ed7/html5/thumbnails/61.jpg)
6115-214
IV.Example– Howtofindanagrams
• Alphabetizethecharactersineachword– cat→act,dog→dgo,mouse→emosu– Resultingstringiscalledalphagram
• Anagramssharethesamealphagram!– stop→opst,post→opst,tops→opst,opts→opst
• Sogothroughwordlistmaking“multimap”fromalphagramtoword!
![Page 62: Principles of Software Constructioncharlie/courses/15-214/2016-fall/slides/15... · The Design of the Collections API – Parts 1 & 2 Josh Bloch Charlie Garrod. 15-214 2 Administrivia](https://reader036.vdocuments.us/reader036/viewer/2022071004/5fc1a817b7d7216e3f6d9ed7/html5/thumbnails/62.jpg)
6215-214
HowtofindanagramsinJava(1)public static void main(String[] args) throws IOException {
// Read words from file and put into a simulated multimapMap<String, List<String>> groups = new HashMap<>();try (Scanner s = new Scanner(new File(args[0]))) {
while (s.hasNext()) {String word = s.next();String alpha = alphabetize(word);List<String> group = groups.get(alpha);if (group == null)
groups.put(alpha, group = new ArrayList<>());group.add(word);
}}
![Page 63: Principles of Software Constructioncharlie/courses/15-214/2016-fall/slides/15... · The Design of the Collections API – Parts 1 & 2 Josh Bloch Charlie Garrod. 15-214 2 Administrivia](https://reader036.vdocuments.us/reader036/viewer/2022071004/5fc1a817b7d7216e3f6d9ed7/html5/thumbnails/63.jpg)
6315-214
HowtofindanagramsinJava(2)// Print all anagram groups above size thresholdint minGroupSize = Integer.parseInt(args[1]);for (List<String> group : groups.values())
if (group.size() >= minGroupSize)System.out.println(group.size() + ": " + group);
}
// Returns the alphagram for a stringprivate static String alphabetize(String s) {
char[] a = s.toCharArray();Arrays.sort(a);return new String(a);
}
![Page 64: Principles of Software Constructioncharlie/courses/15-214/2016-fall/slides/15... · The Design of the Collections API – Parts 1 & 2 Josh Bloch Charlie Garrod. 15-214 2 Administrivia](https://reader036.vdocuments.us/reader036/viewer/2022071004/5fc1a817b7d7216e3f6d9ed7/html5/thumbnails/64.jpg)
6415-214
Demo– Anagrams
![Page 65: Principles of Software Constructioncharlie/courses/15-214/2016-fall/slides/15... · The Design of the Collections API – Parts 1 & 2 Josh Bloch Charlie Garrod. 15-214 2 Administrivia](https://reader036.vdocuments.us/reader036/viewer/2022071004/5fc1a817b7d7216e3f6d9ed7/html5/thumbnails/65.jpg)
6515-214
TwoslidesinJavavs.achapterinSTLJava’sverbosityissomewhatexaggerated
![Page 66: Principles of Software Constructioncharlie/courses/15-214/2016-fall/slides/15... · The Design of the Collections API – Parts 1 & 2 Josh Bloch Charlie Garrod. 15-214 2 Administrivia](https://reader036.vdocuments.us/reader036/viewer/2022071004/5fc1a817b7d7216e3f6d9ed7/html5/thumbnails/66.jpg)
6615-214
V.CritiqueSomethingsIwishI’ddonedifferently• Algorithmsshouldreturncollection,notvoidorboolean
– Turnsuglymultiliners intoniceone-linersprivate static String alphabetize(String s) {
return new String(Arrays.sort(s.toCharArray()));}
• Collectionshouldhaveget(), remove()– Queue andDeque eventuallydidthis
• Sorted{Set,Map}shouldhavepropernavigation– Navigable{Set,Map}arewarts
![Page 67: Principles of Software Constructioncharlie/courses/15-214/2016-fall/slides/15... · The Design of the Collections API – Parts 1 & 2 Josh Bloch Charlie Garrod. 15-214 2 Administrivia](https://reader036.vdocuments.us/reader036/viewer/2022071004/5fc1a817b7d7216e3f6d9ed7/html5/thumbnails/67.jpg)
6715-214
Conclusion
• Ittakesalotofworktomakesomethingthatappearsobvious– Coherent,unifiedvision– Willingnesstolistentoothers– Flexibilitytoacceptchange– Tenacitytoresistchange– Gooddocumentation!
• It’sworththeeffort!– Asolidfoundationcanlasttwo+decades