• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Java collections framework
 

Java collections framework

on

  • 1,307 views

Introduction to the Java Collections Framework. ...

Introduction to the Java Collections Framework.

Topics:
<a>
<b>
<c>

Teaching material for the course of "Tecniche di Programmazione" at Politecnico di Torino in year 2012/2013. More information: http://bit.ly/tecn-progr

Statistics

Views

Total Views
1,307
Views on SlideShare
1,307
Embed Views
0

Actions

Likes
0
Downloads
36
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

CC Attribution-NonCommercial-ShareAlike LicenseCC Attribution-NonCommercial-ShareAlike LicenseCC Attribution-NonCommercial-ShareAlike License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Java collections framework Java collections framework Presentation Transcript

    • Java collections frameworkCommonly reusable collection data structures
    • Java Collections Framework (JCF)A.A. 2012/2013Tecniche di programmazione2Collectionan object that represents a group of objectsCollection FrameworkA unified architecture for representing and manipulatingcollectionsSuch collections are manipulated independent of the details oftheir representation“JCF” vs.“ADT”
    • A little bit of history…A.A. 2012/2013Tecniche di programmazione3JDK < 1.2Standard practice: Vector and HashtableCompatibility with C++ StandardTemplate Library (STL)Doug Lea’s Collections packageObjectSpace Generic Collection Library (JGL)JDK ≥1.2Sun drops compatibility with C++ STLJoshua Bloch’s JCF(now Chief Java Architect @ Google)
    • A little bit of history…A.A. 2012/2013Tecniche di programmazione4Java 5Introduction of <generics>Clean, safe definition of the Collection InterfaceTrees, linked lists, stacks, hash tables, and other classes areimplementations of CollectionArrays do not implement the Collection interfaceVector redefined to implement Collection
    • A little bit of history…A.A. 2012/2013Tecniche di programmazione5Doug Lea later developed a concurrency package
    • JCF’s Main ElementsA.A. 2012/2013Tecniche di programmazione6InfrastructureInterfaces that provide essential support for the collectioninterfacesGeneral-purpose ImplementationsPrimary implementations (basic and bulk) of the collectioninterfaces
    • AlgorithmsA.A. 2012/2013Tecniche di programmazione7AlgorithmsStatic methods that perform useful functions on collections,such as sorting a list
    • ICF’s Utility ImplementationsA.A. 2012/2013Tecniche di programmazione8Legacy ImplementationsThe collection classes from earlier releases,Vector andHashtable, have been retrofitted to implement the collectioninterfacesConvenience ImplementationsHigh-performance "mini-implementations" of the collectioninterfacesWrapper ImplementationsAdd functionality, such as synchronization, to otherimplementations
    • Abstract ImplementationsA.A. 2012/2013Tecniche di programmazione9Partial implementations (skeletons) of the collectioninterfaces to facilitate custom implementations
    • InfrastructureA.A. 2012/2013Tecniche di programmazione10These interfaces form the basis of the frameworkSome types of collections allow duplicate elements, others do notSome types of collections are ordered, others are unorderedThe Java platform doesn’t provide any direct implementationsof the Collection interface, but provides implementations ofmore specific sub-interfaces, such as Set and List and Maps
    • Collection interfaceA.A. 2012/2013Tecniche di programmazione11A Collection represents a group of objects knownas its elementsThe Collection interface is the least commondenominator that all collections implement.It is Usedto pass collections aroundto manipulate them when maximum generality is desireCollection extends Iterable
    • A note on iteratorsA.A. 2012/2013Tecniche di programmazione12An Iterator is an object that enables you to traversethrough a collection (and to remove elements from thecollection 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(); //optional}
    • Main InterfacesA.A. 2012/2013Tecniche di programmazione13ListA more flexible version of an arrayQueue & Priority QueueThe order of arrival does matter, or the urgencySetNo order, no duplicate elements
    • Map interfaceA.A. 2012/2013Tecniche di programmazione14A Map is an object that maps keys to valuesA map cannot contain duplicate keys: each key can mapto at most one valueMap does not extend Iterable, but it is possible toget an iterator through entrySet()Notez bien: Maps do not extend fromjava.util.Collection, but they’re still considered tobe part of the “collections framework”
    • Collection Family TreeA.A. 2012/2013Tecniche di programmazione15
    • Collection interfaceA.A. 2012/2013Tecniche di programmazione16public interface Collection<E> extends Iterable<E> {int size();boolean isEmpty();boolean contains(Object element);boolean add(E element); //optionalboolean remove(Object element); //optionalIterator<E> iterator();boolean containsAll(Collection<?> c);boolean addAll(Collection<? extends E> c); //optionalboolean removeAll(Collection<?> c); //optionalboolean retainAll(Collection<?> c); //optionalvoid clear(); //optionalObject[] toArray();<T>T[] toArray(T[] a);}
    • Collection interfaceA.A. 2012/2013Tecniche di programmazione17public interface Collection<E> extends Iterable<E> {int size();boolean isEmpty();boolean contains(Object element);boolean add(E element); //optionalboolean remove(Object element); //optionalIterator<E> iterator();boolean containsAll(Collection<?> c);boolean addAll(Collection<? extends E> c); //optionalboolean removeAll(Collection<?> c); //optionalboolean retainAll(Collection<?> c); //optionalvoid clear(); //optionalObject[] toArray();<T>T[] toArray(T[] a);}Basic Operationsgenerics
    • Collection interfaceA.A. 2012/2013Tecniche di programmazione18public interface Collection<E> extends Iterable<E> {int size();boolean isEmpty();boolean contains(Object element);boolean add(E element); //optionalboolean remove(Object element); //optionalIterator<E> iterator();boolean containsAll(Collection<?> c);boolean addAll(Collection<? extends E> c); //optionalboolean removeAll(Collection<?> c); //optionalboolean retainAll(Collection<?> c); //optionalvoid clear(); //optionalObject[] toArray();<T>T[] toArray(T[] a);}Bulk Operationswildcardeither extendsor implements
    • Collection interfaceA.A. 2012/2013Tecniche di programmazione19public interface Collection<E> extends Iterable<E> {int size();boolean isEmpty();boolean contains(Object element);boolean add(E element); //optionalboolean remove(Object element); //optionalIterator<E> iterator();boolean containsAll(Collection<?> c);boolean addAll(Collection<? extends E> c); //optionalboolean removeAll(Collection<?> c); //optionalboolean retainAll(Collection<?> c); //optionalvoid clear(); //optionalObject[] toArray();<T>T[] toArray(T[] a);}Array Operations
    • Licenza d’usoA.A. 2012/2013Tecniche di programmazione21Queste diapositive sono distribuite con licenza Creative Commons“Attribuzione - Non commerciale - Condividi allo stesso modo (CCBY-NC-SA)”Sei libero:di riprodurre, distribuire, comunicare al pubblico, esporre in pubblico,rappresentare, eseguire e recitare questoperadi modificare questoperaAlle seguenti condizioni:Attribuzione — Devi attribuire la paternità dellopera agli autorioriginali e in modo tale da non suggerire che essi avallino te o il modo incui tu usi lopera.Non commerciale — Non puoi usare questopera per finicommerciali.Condividi allo stesso modo — Se alteri o trasformi questopera, o sela usi per crearne unaltra, puoi distribuire lopera risultante solo con unalicenza identica o equivalente a questa.http://creativecommons.org/licenses/by-nc-sa/3.0/