1 / 10

Collections

Collections. A collection is an Object which contains other Objects. There are three fundamental kinds of collection :. Sets - no duplicates. Lists - duplicates allowed. Maps - key / value pairs. The Collection interface mandates the following methods:.

morse
Download Presentation

Collections

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Collections A collection is an Object which contains other Objects. There are three fundamental kinds of collection: Sets - no duplicates Lists - duplicates allowed Maps - key / value pairs

  2. The Collection interfacemandates the following methods: boolean add( Object toAdd) // Adds toAdd to the collection boolean addAll( Collection allOf) // Adds all of allOf to the collection void clear() // Removes everything, size() becomes 0. boolean contains( Object thisOne) // True if thisOne is in the collection boolean containsAll( Collection allOf) // True if all of allOf are in the collection boolean equals( Object toThis) // !!!! Well tricky!! int hashCode() // A collection of collections! boolean isEmpty() // True if size() is zero. Iterator iterator() // see slide on iterators. boolean remove( Object toRemove) // removes toRemove, if present! boolean removeAll( Collection allOf) // removes allOf, if present! boolean retainAll( Collection these) // removes all apart from these! int size() // the number of elements in the collection. Object[] toArray() // returns an array representation of the collection! Object[] toArray( Object[] anArray)// returns an array represention, in anArray.

  3. Aninterface ... … is a promise to supply certain methods. ...all Set and List Collection classes implement the Collection interface ... … so all instances of these classes will support all of the methods on the previous slide .... … so a developer need only learn the meaning of the methods on the previous slide .... … or a different kind of list or set can be usedafter an artifact has been developed ....

  4. The Set and List hierarchy Object AbstractCollection implements Collection AbstractList AbstractSet Abstract SequentialList ArrayList Vector HashSet TreeSet LinkedList Stack

  5. The Set and List hierarchy - notes Elements in a LinkedList cannot be accessed directly - but only by traversing the links from the start. An ArrayList can be though of as a more efficient Vector. A Stack is a FIFO (cf LIFO) collection. A HashSet makes guarantees about sequence. A TreeSet stores elements in a defined sequence.

  6. The iterator interface boolean hasNext() // true if the iteration has more elements. Object next() // Returns the next element in the iteration. This gives a general-purpose iteration across a collection as ... RadioSlot aSlot = null; Iterator iter = theList.iterator(); while ( iter.hasNext()) { aSlot = (RadioSlot) iter. next(); // Do something with aSlot! } // End while. This should be favored over the for loop iteration given in the waypoint 4 notes as it does not rely upon the provision of various methods.

  7. Collections are important! Most (almost all?) artefacts have to deal with n instances not 1! The choice of which class of collection can become vital! The choice of which class can be changed after development! The choice of which class can be decided at run-time! Java collections are non-generic (every element is an Object!)! Special purpose collections should implement the Collection interface. There is an argument that every class should have an associated interface (clear separation of what from how!). The Collection classes are an important and accessible example of good software development practice.

  8. The Vector documentation 1 general description The Vector class implements a growable array of objects. Like an array, it contains components that can be accessed using an integer index. However, the size of a Vector can grow or shrink as needed to accommodate adding and removing items after the Vector has been created. Each vector tries to optimize storage management by maintaining a capacity and a capacityIncrement. The capacity is always at least as large as the vector size; it is usually larger because as components are added to the vector, the vector's storage increases in chunks the size of capacityIncrement. An application can increase the capacity of a vector before inserting a large number of components; this reduces the amount of incremental reallocation. As of JDK1.2, this class has been retrofitted to implement List, so that it becomes a part of Java's collection framework. Unlike the new collection implementations, Vector is synchronized. The Iterators returned by Vector's iterator and listIterator methods are fail-fast: if the Vector is structurally modified at any time after the Iterator is created, in any way except through the Iterator's own remove or add methods, the Iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the Iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future. The Enumerations returned by Vector's elements method are not fail-fast. This documentation is available with the Java SDK.

  9. The Vector documentation 2 specific methods public void addElement(Object obj) Adds the specified component to the end of this vector, increasing its size by one. The capacity of this vector is increased if its size becomes greater than its capacity. This method is identical in functionality to the add(Object) method (which is part of the List interface). Parameters: obj - the component to be added. See Also: add(Object), List see http://java.sun.com/products/jdk/1.2/docs/api/index.html

  10. The Vector implementation publicsynchronizedvoid addElement( Object obj) { modCount++; ensureCapacityHelper( elementCount + 1); elementData[ elementCount++] = obj; } The source code for the standard java classes is distributed as part of the SDK. This indicates that the Java libraries are written in Java!

More Related