Estructura de Datos : Colecciones ( Collections )


Definiciones


Representaciones de Colecciones

Representacion secuencial

     *--------*     *--------*     *--------*     *--------*     *--------*     *--------*     *--------*     *--------*     *--------*
     |        |     |        |     |        |     |        |     |        |     |        |     |        |     |        |     |        |
     |        |     |        |     |        |     |        |     |        |     |        |     |        |     |        |     |        |
     |        |     |        |     |        |     |        |     |        |     |        |     |        |     |        |     |        |
     |        |     |        |     |        |     |        |     |        |     |        |     |        |     |        |     |        |
     |        |     |        |     |        |     |        |     |        |     |        |     |        |     |        |     |        |
     *--------*     *--------*     *--------*     *--------*     *--------*     *--------*     *--------*     *--------*     *--------*

      Elemento       Elemento       Elemento       Elemento       Elemento       Elemento       Elemento       Elemento       Elemento

Representacion lineal

                 Elemento  1    Elemento  2    Elemento  3    Elemento  2    Elemento  3    Elemento  4    Elemento  5    Elemento  1
                   adicion        adicion        adicion      eliminacion      adicion        adicion        adicion      eliminacion

 *-----------*  *-----------*  *-----------*  *-----------*  *-----------*  *-----------*  *-----------*  *-----------*  *-----------*
 |           |  |Elemento  1|  |Elemento  1|  |Elemento  1|  |Elemento  1|  |Elemento  1|  |Elemento  1|  |Elemento  1|  |Elemento  3|
 *-----------*  *-----------*  *-----------*  *-----------*  *-----------*  *-----------*  *-----------*  *-----------*  *-----------*
 |           |  |           |  |Elemento  2|  |Elemento  2|  |Elemento  3|  |Elemento  3|  |Elemento  3|  |Elemento  3|  |Elemento  3|
 *-----------*  *-----------*  *-----------*  *-----------*  *-----------*  *-----------*  *-----------*  *-----------*  *-----------*
 |           |  |           |  |           |  |Elemento  3|  |           |  |Elemento  3|  |Elemento  3|  |Elemento  3|  |Elemento  4|
 *-----------*  *-----------*  *-----------*  *-----------*  *-----------*  *-----------*  *-----------*  *-----------*  *-----------*
 |           |  |           |  |           |  |           |  |           |  |           |  |Elemento  4|  |Elemento  4|  |Elemento  5|
 *-----------*  *-----------*  *-----------*  *-----------*  *-----------*  *-----------*  *-----------*  *-----------*  *-----------*
 |           |  |           |  |           |  |           |  |           |  |           |  |           |  |Elemento  5|  |           |
 *-----------*  *-----------*  *-----------*  *-----------*  *-----------*  *-----------*  *-----------*  *-----------*  *-----------*
 |           |  |           |  |           |  |           |  |           |  |           |  |           |  |           |  |           |
 *-----------*  *-----------*  *-----------*  *-----------*  *-----------*  *-----------*  *-----------*  *-----------*  *-----------*
 |           |  |           |  |           |  |           |  |           |  |           |  |           |  |           |  |           |
 *-----------*  *-----------*  *-----------*  *-----------*  *-----------*  *-----------*  *-----------*  *-----------*  *-----------*
 |           |  |           |  |           |  |           |  |           |  |           |  |           |  |           |  |           |
 *-----------*  *-----------*  *-----------*  *-----------*  *-----------*  *-----------*  *-----------*  *-----------*  *-----------*

   Tiempo t1      Tiempo t2      Tiempo t3      Tiempo t4      Tiempo t5      Tiempo t6      Tiempo t7      Tiempo t8      Tiempo t9


Iterador ( Iterator )

Es un mecanismo abstracto que permite visitar todos los elementos de cualquier contenedor de elementos.

Representación

                                +--------------+
                                |   Iterador   |
                                |              |
                                |              |
     *--------*     *--------*  |  *--------*  |  *--------*     *--------*     *--------*     *--------*     *--------*     *--------*
     |        |     |        |  |  |        |  |  |        |     |        |     |        |     |        |     |        |     |        |
     |        |     |        |  |  |Elemento|  |  |        |     |        |     |        |     |        |     |        |     |        |
     |        |     |        |  X  |        |  V  |        |     |        |     |        |     |        |     |        |     |        |
     |        |     |        |     |Devuelto|     |        |     |        |     |        |     |        |     |        |     |        |
     |        |     |        |     |        |     |        |     |        |     |        |     |        |     |        |     |        |
     *--------*     *--------*     *--------*     *--------*     *--------*     *--------*     *--------*     *--------*     *--------*

      Elemento       Elemento       Elemento       Elemento       Elemento       Elemento       Elemento       Elemento       Elemento
Cuando se invoca el método next(), el iterador, salta sobre el próximo elemento, y retorna una referencia sobre el elemento saltado. El iterador debe pensarse como dispuesto entre elementos, y no en un elemento.

Interfase

        Object  next()
        boolean hasNext()
        void    remove()

        Si se usa next() sin hasNext() se puede obtener la excepción NoSuchElementException
        Si se usa remove() sin next() se obtiene la excepción IllegalStateException
El método next recupera el siguiente objeto de la Colección. Invocando repetidamente este método se visitan todos los objetos de la Colección, uno a uno, desde el primer elemento hasta el último.

El método hasNext retorna true si existen más objetos en la Colección y retorna false en caso contrario. Siempre, antes de invocar la recuperación del siguiente objeto, debe consultarse si existen más objetos en la Colección.

El método remove elimina de la Colección el último objeto recuperado.

Ejemplos

        Iterator iter = c.iterator();
        while( iter.hasNext() )
        {
                Object obj = iter.next();
                ...
                ( Hago algo con el elemento recuperado )
                ...
        }

        Iterator iter = c.iterator();
        while( iter.next() )                    // Posible NoSuchElementException
        {
                ...
                ...
        }

        Iterator iter = c.iterator();
        iter.next()                             // Posible NoSuchElementException
        iter.remove()
        iter.next()                             // Posible NoSuchElementException
        iter.remove()
        iter.next()                             // Posible NoSuchElementException
        iter.remove()

        Iterator iter = c.iterator();
        iter.next()                             // Posible NoSuchElementException
        iter.remove()
        iter.remove()                           // IllegalStateException
        iter.remove()                           // IllegalStateException


Colección ( Collection )

Los métodos fundamentales de una colección de objetos o elementos son boolean add(Object obj) y el Iterator iterator(). La interfase se completa con otros métodos más, que pueden ser escritos en base a estos dos, siguiendo el concepto de funciones primitivas y secundarias.

Interfase

        int      size()
        boolean  isEmpty()
        boolean  contains(Object obj)
        boolean  containsAll(Collection c)
        boolean  equals(Object other)
        void     add(Object obj)
        boolean  addAll(Collection from)
        boolean  remove(Object obj)
        boolean  removeAll(Collection c)
        void     clear()
        boolean  retainAll(Collection c)
        Object[] toArray()
        Object[] toArray(Object[] a)
        Iterator iterator()
        int      hashCode()
El método size retorna el número de elementos actualmente almacenados en la Colección.

El método isEmpty retorna true si no existen elementos en la Colección.

El método contains retorna true si la Colección contiene un objeto igual a obj.

El método containsAll retorna true si la Colección contiene todos los objeto de la Colección other.

El método equals retorna true si la Colección es esta misma.

El método add adiciona un objeto a la Colección. Las Colecciones siempre aceptan un nuevo elemento y mantienen elementos duplicados.

El método addAll retorna true si la Colección ha cambiado por la adición de todos los objetos de la Colección other.

El método remove retorna true si la Colección ha cambiado por la eliminación del Objeto other.

El método removeAll retorna true si la Colección ha cambiado por la eliminación de todos los objetos de la Colección other.

El método clear elimina todos los objetos de la Colección.

El método retainAll retorna true si la Colección ha cambiado por la eliminación de todos los objetos no iguales de la Colección other.

El método toArray retorna un Arreglo formado por los objetos de la Colección o hace una copia de un Arreglo a otro.

El método iterator devuelve un Iterator de esta Colección.

El método hasCode devuelve un int que es el hash code de la Colección.


Coleccion Abstracta ( AbstractCollection )

Es una implementación parcial de la interfase Collection, para facilitar las implementaciones de la interfase mencionada. La firma de esta clase es public abstract class AbstractCollection extends Object implements Collection.

   *---------------*                           .................
   |               |                           .               .
   |               |                           .               .
   |               |                           .               .
   |    Object     |             ...............  Collection   .
   |               |             .             .               .
   |               |             .             .               .
   |               |             .             .               .
   *---------------*             .             .................
           |                     .
           |                     .
           |                     .
           |                     .
   *---------------*             .
   |               |             .
   |               |..............
   |   Abstract    |
   |               |
   |  Collection   |
   |               |
   |               |
   *---------------*
Los métodos de la clase son : Todos los métodos deberían ser escritos a partir de los dos primitivos, add()y size().


Lecturas complementarias

The Collection Interface

A Collection represents a group of objects, known as its elements. The primary use of the Collection interface is to pass around collections of objects where maximum generality is desired. For example, by convention all general-purpose collection implementations (which typically implement some subinterface of Collection like Set or List) have a constructor that takes a Collection argument. This constructor initializes the new Collection to contain all of the elements in the specified Collection. This constructor allows the caller to create a Collection of a desired implementation type, initially containing all of the elements in any given Collection, whatever its subinterface or implementation type. Suppose you have a Collection, c, which may be a List, a Set, or some other kind of Collection. The following one-liner creates a new ArrayList (an implementation of the List interface), initially containing all of the elements in c:

List l = new ArrayList(c);

The Collection interface is shown below:

public interface Collection
{
    // Basic Operations
    int size();
    boolean isEmpty();
    boolean contains(Object element);
    boolean add(Object element);               // Optional
    boolean remove(Object element);            // Optional
    Iterator iterator();

    // Bulk Operations
    boolean containsAll(Collection c);
    boolean addAll(Collection c);              // Optional
    boolean removeAll(Collection c);           // Optional
    boolean retainAll(Collection c);           // Optional
    void clear();                              // Optional

    // Array Operations
    Object[] toArray();
    Object[] toArray(Object a[]);
}
The interface does about what you'd expect, given that a Collection represents a group of objects. It has methods to tell you how many elements are in the collection (size, isEmpty), to check if a given object is in the collection (contains), to add and remove an element from the collection (add, remove), and to provide an iterator over the collection (iterator).

The add method is defined generally enough so that it makes sense for collections that allow duplicates as well as those that don't. It guarantees that the Collection will contain the specified element after the call completes, and returns true if the Collection changes as a result of the call. Similarly, the remove method is defined to remove a single instance of the specified element from the Collection, assuming the Collection contains the element, and to return true if the Collection was modified as a result.

Iterators

The object returned by the iterator method deserves special mention. It is an Iterator, which is very similar to an Enumeration, but differs in two respects:

The first point is important: There was no safe way to remove elements from a collection while traversing it with an Enumeration. The semantics of this operation were ill-defined, and differed from implementation to implementation.

The Iterator interface is shown below:

public interface Iterator
{
    boolean hasNext();
    Object next();
    void remove();                        // Optional
}
The hasNext method is identical in function to Enumeration.hasMoreElements, and the next method is identical in function to Enumeration.nextElement. The remove method removes from the underlying Collection the last element that was returned by next. The remove method may be called only once per call to next, and throws an exception if this condition is violated. Note that Iterator.remove is the only safe way to modify a collection during iteration; the behavior is unspecified if the underlying collection is modified in any other way while the iteration is in progress.

The following snippet shows you how to use an Iterator to filter a Collection, that is, to traverse the collection, removing every element that does not satisfy some condition:

static void filter(Collection c)
{
    for (Iterator i = c.iterator(); i.hasNext(); ) if (!cond(i.next())) i.remove();
}
Two things should be kept in mind when looking at this simple piece of code:

Bulk Operations

The bulk operations perform some operation on an entire Collection in a single shot. They are shorthands in the sense that each of them can be simulated, perhaps less efficiently, using the operations described above.

The addAll, removeAll, and retainAll methods all return true if the target Collection was modified in the process of executing the operation.

As a simple example of the power of the bulk operations, consider following idiom to remove all instances of a specified element, e from a Collection, c.:

c.removeAll(Collections.singleton(e));
More specifically, suppose that you want to remove all of the null elements from a Collection:
c.removeAll(Collections.singleton(null));
This idiom uses Collections.singleton, which is a static factory method that returns an immutable Set containing only the specified element.

Array Operations

The toArray methods are provided as a bridge between collections and older APIs that expect arrays on input. They allow the contents of a Collection to be translated into an array. The simple form with no arguments creates a new array of Object. The more complex form allows the caller to provide an array or to choose the runtime type of the output array.

For example, suppose c is a Collection The following snippet dumps the contents of c into a newly allocated array of Object whose length is identical to the number of elements in c:

Object[] a = c.toArray();
Suppose c is known to contain only strings. The following snippet dumps the contents of c into a newly allocated array of String whose length is identical to the number of elements in c:
String[] a = (String[]) c.toArray(new String[0]);


Criterios de Diseno

   *-----------------------------------------------------------------------*       *-----------------------------------------------------------------------*
   |                                                                       |       |                                                                       |
   |                                                                       |       |                                                                       |
   |                                                                       |       |                                                                       |
   |      *---------------------------------------------------------*      |       |      *---------------------------------------------------------*      |
   |      |                                                         |      |       |      |                                                         |      |
   |      |                                                         |      |       |      |                                                         |      |
   |      |                                                         |      |       |      |                                                         |      |
   |      |      *-------------------------------------------*      |      |       |      |                                                         |      |
   |      |      |                                           |      |      |       |      |                                                         |      |
   |      |      |                                           |      |      |       |      |                                                         |      |
   |      |      |                                           |      |      |       |      |                                                         |      |
   |      |      |      *-----------------------------*      |      |      |       |      |                                                         |      |
   |      |      |      |                             |      |      |      |       |      |                                                         |      |
   |      |      |      |                             |      |      |      |       |      |                                                         |      |
   |      |      |      |                             |      |      |      |       |      |                                                         |      |
   |      |      |      |      *---------------*      |      |      |      |       |      |                                                         |      |
   |      |      |      |      |               |      |      |      |      |       |      |                                                         |      |
   |      |      |      |      |     Datos     |      |      |      |      |       |      |                  Estructura de Datos                    |      |
   |      |      |      |      |               |      |      |      |      |       |      |                                                         |      |
   |      |      |      |      *---------------*      |      |      |      |       |      |                                                         |      |
   |      |      |      |                             |      |      |      |       |      |                                                         |      |
   |      |      |      |     Funciones Primarias     |      |      |      |       |      |                                                         |      |
   |      |      |      |                             |      |      |      |       |      |                                                         |      |
   |      |      |      *-----------------------------*      |      |      |       |      |                                                         |      |
   |      |      |                                           |      |      |       |      |                                                         |      |
   |      |      |           Funciones Secundarias           |      |      |       |      |                                                         |      |
   |      |      |                                           |      |      |       |      |                                                         |      |
   |      |      *-------------------------------------------*      |      |       |      |                                                         |      |
   |      |                                                         |      |       |      |                                                         |      |
   |      |                  Funciones  Terciarias                  |      |       |      |                                                         |      |
   |      |                                                         |      |       |      |                                                         |      |
   |      *---------------------------------------------------------*      |       |      *---------------------------------------------------------*      |
   |                                                                       |       |                                                                       |
   |                             Aplicaciones                              |       |                             Aplicaciones                              |
   |                                                                       |       |                                                                       |
   *-----------------------------------------------------------------------*       *-----------------------------------------------------------------------*