User:Jenny Harlow/Design study/Java Collections Framework
From CSSEMediaWiki
< User:Jenny Harlow | Design study(Difference between revisions)
Jenny Harlow (Talk | contribs) (→Overview of the Java Collections Framework) |
Jenny Harlow (Talk | contribs) (→Overview of the Java Collections Framework) |
||
Line 26: | Line 26: | ||
*The [http://download-llnw.oracle.com/javase/6/docs/api/java/util/Collections.html Collections] class contains static methods which relate to collections, including Collection and Map types. | *The [http://download-llnw.oracle.com/javase/6/docs/api/java/util/Collections.html Collections] class contains static methods which relate to collections, including Collection and Map types. | ||
− | **Some Collections methods apply only to particular collection types. In particular, many apply only to List types (sort(...), reverse(...), swap(...), shuffle(...), indexOfSublist( | + | **Some Collections methods apply only to particular collection types. In particular, many apply only to List types (sort(...), reverse(...), swap(...), shuffle(...), indexOfSublist(...), shuffle(...), etc). |
− | **Collections also includes static factory methods providing 'wrappers', ie a view of the specified collection which interposes a layer between the client and methods of the underlying collection. Some wrappers (eg synchronisation wrappers) add functionality, some disable functionality. The <span id="unmodifiable">unmodifiable wrappers</span> are intended to provide a read-only view of a collection so that "attempts to modify the returned collection, whether direct or via its iterator, result in an UnsupportedOperationException." ([http://download-llnw.oracle.com/javase/6/docs/api/ Java SE 6.0 documentation]). Specific wrapper factory methods for List, Set, Map etc are provided for wrapper family. | + | **Collections also includes static factory methods providing 'wrappers', ie a view of the specified collection which interposes a layer between the client and methods of the underlying collection. Some wrappers (eg synchronisation wrappers) add functionality, some - like the unmodifiable wrappers - disable functionality. The <span id="unmodifiable">unmodifiable wrappers</span> are intended to provide a read-only view of a collection so that "attempts to modify the returned collection, whether direct or via its iterator, result in an UnsupportedOperationException." ([http://download-llnw.oracle.com/javase/6/docs/api/ Java SE 6.0 documentation]). Specific wrapper factory methods for List, Set, Map etc are provided for the wrapper family. |
**Some algorithms are 'destructive', and again<blockquote>"the algorithms that modify the collection on which they operate, are specified to throw UnsupportedOperationException if the collection does not support the appropriate mutation primitive(s), such as the set method. These algorithms may, but are not required to, throw this exception if an invocation would have no effect on the collection." ([http://download-llnw.oracle.com/javase/6/docs/api/ Java SE 6.0 documentation]) </blockquote> | **Some algorithms are 'destructive', and again<blockquote>"the algorithms that modify the collection on which they operate, are specified to throw UnsupportedOperationException if the collection does not support the appropriate mutation primitive(s), such as the set method. These algorithms may, but are not required to, throw this exception if an invocation would have no effect on the collection." ([http://download-llnw.oracle.com/javase/6/docs/api/ Java SE 6.0 documentation]) </blockquote> | ||
*The [http://download-llnw.oracle.com/javase/6/docs/api/index.html?java/util/Collections.html RandomAccess] interface is a 'tag' or 'marker' interface retrofitted to ArrayLists to indicate that they provide random access (rather than sequential access) to list elements. | *The [http://download-llnw.oracle.com/javase/6/docs/api/index.html?java/util/Collections.html RandomAccess] interface is a 'tag' or 'marker' interface retrofitted to ArrayLists to indicate that they provide random access (rather than sequential access) to list elements. |
Latest revision as of 19:16, 28 September 2010
Navigation shortcuts: Wiki users:Jenny Harlow:Jenny Harlow Design study
Overview of the Java Collections Framework
This diagram gives an overview of the main Java Collections Framework interfaces and implementations.
Some of the main points which are relevant for this design study are:
- The Collections Framework uses generics.
- List and Set are Collection subtypes (formal type parameter E).
- Map (formal type parameters K and V) is not a Collection subtype and uses the Map.Entry type (a member interface of Map).
- The Collections Framework includes abstract classes, not shown in this diagram, which provide a 'skeletal implementation' of the interfaces to act as the basis for concrete implementations.
- Collection implements Iterable, which is what (transparently to the client) allows us to be able to use the for-each looping construct. Map does not implement Iterable and there is no map iterator in the Framework.
- The Collection type has non-altering methods including size(), isEmpty(), contains(E e). It also has collection-altering (what Java terms 'destructive methods') add(E e), remove(E e) (and addAll(...), removeAll(...) to be able to add or remove a whole Collection), and clear(). Map methods include size(), isEmpty(), containsKey(K k) and "destructive" methods put(K k, V v) (and putAll(...)) and remove(K k). The destructive methods are marked optional in the interface specifications. This essentially means that an implmentation may choose not to support them. Java says:
"The 'destructive' methods contained in this interface, that is, the methods that modify the collection on which they operate, are specified to throw UnsupportedOperationException if this collection does not support the operation. If this is the case, these methods may, but are not required to, throw an UnsupportedOperationException if the invocation would have no effect on the collection."(Java SE 6.0 documentation)
- Similarly, if there are restrictions on the types of elements an implementation allows, then
"attempting an operation on an ineligible element whose completion would not result in the insertion of an ineligible element into the collection may throw an exception or it may succeed, at the option of the implementation. Such exceptions are marked as 'optional' in the specification for this interface."(Java SE 6.0 documentation)
- The Collection interface also includes the iterator() factory method, which returns an Iterator. Iterator methods include remove() as well as next() and hasNext(). remove() is not marked optional but in reality is - see the unmodifiable wrappers described below.
- The Collections class contains static methods which relate to collections, including Collection and Map types.
- Some Collections methods apply only to particular collection types. In particular, many apply only to List types (sort(...), reverse(...), swap(...), shuffle(...), indexOfSublist(...), shuffle(...), etc).
- Collections also includes static factory methods providing 'wrappers', ie a view of the specified collection which interposes a layer between the client and methods of the underlying collection. Some wrappers (eg synchronisation wrappers) add functionality, some - like the unmodifiable wrappers - disable functionality. The unmodifiable wrappers are intended to provide a read-only view of a collection so that "attempts to modify the returned collection, whether direct or via its iterator, result in an UnsupportedOperationException." (Java SE 6.0 documentation). Specific wrapper factory methods for List, Set, Map etc are provided for the wrapper family.
- Some algorithms are 'destructive', and again
"the algorithms that modify the collection on which they operate, are specified to throw UnsupportedOperationException if the collection does not support the appropriate mutation primitive(s), such as the set method. These algorithms may, but are not required to, throw this exception if an invocation would have no effect on the collection." (Java SE 6.0 documentation)
- The RandomAccess interface is a 'tag' or 'marker' interface retrofitted to ArrayLists to indicate that they provide random access (rather than sequential access) to list elements.