Java - Collections

Hola a todos!

Las colecciones en java son de bastante uso, las mas cotidianas, para programación estructurada, son los arreglos y las matrices, pero hay mas... expliquémonos bien.

Tenemos:
  • Arrays
  • Iterator
  • Containers
    • Collections
    • Set
    • List
    • Map
 ARRAYS

En el arreglo se puede saber el tamaño y el tipo de datos que contiene.
Un ejemplo de la creacion de arreglos es:

CÓDIGO CLASE: Car
class Car{};
  Car[] cars1; // null
  Car[] cars2 = new Car[10]; // null
    for (int i = 0; i < cars2.length; i++)
      cars2[i] = new Car();
       Car[] cars3 = {new Car(), new Car(), new Car(), new Car()};
       cars1 = {new Car(), new Car(), new Car()};


ITERATOR

En el iterator, se puede saber si tiene siguiente, cual es el siguiente, y eliminar elementos dentro de si.
Un ejemplo de este es:

CODIGO CLASE: Iterator

public class Iterator{
  public static void main (String[] args){
   ArrayList cars = new ArrayList();
    for (int i = 0; i < 12; i++)
     cars.add (new Car());
     Iterator it = cats.iterator();
      while (it.hasNext())
        System.out.println ((Car)it.next());
   }
}

INTERFACE ITERATOR

Interface Iterator {
boolean hasNext();
Object next(); // note "one-way" traffic
void remove();
}

CONTAINERS

Set: no permite duplicados
  • HashSet: Usa tablas tipo hash, no es considera orden
  • TreeSet: Usa estructura tree y garantiza el orden de los elementos.

List: permite duplicados y es en orden garantizado
  • ArrayList: Arreglo que usa métodos set y get
  • LinkedList: Basado en dobles listas ligadas.
Map:  Objeto que asigna claves a valores
  • HashMap: Basado en tablas de tipo Hash, no garantiza orden
  • TreeMap: Implementación tipo Tree, garantiza el orden

INTERFACE COLLECTIONS

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[]);
}

INTERFACE LIST

public interface List extends Collection {
// Positional Access
Object get(int index);
Object set(int index, Object element); // Optional
void add(int index, Object element); // Optional
Object remove(int index); // Optional
abstract boolean addAll(int index, Collection c);
// Optional
// Search
int indexOf(Object o);
int lastIndexOf(Object o);
// Iteration
ListIterator listIterator();
ListIterator listIterator(int index);
// Range-view
List subList(int from, int to);
} 

INTERFACE MAP
public interface Map {
// Basic Operations
Object put(Object key, Object value);
Object get(Object key);
Object remove(Object key);
boolean containsKey(Object key);
boolean containsValue(Object value);
int size();
boolean isEmpty();
// Bulk Operations
void putAll(Map t);
void clear();
// Collection Views
public Set keySet();
public Collection values();
public Set entrySet();
// Interface for entrySet elements
public interface Entry {
Object getKey();
Object getValue();
Object setValue(Object value);
}
} 
Esta entrada esta en referencia del siguiente link: Collections_In_Java

Espero sus comentarios, sus dudas o quejas...jejeje... igualmente siempre saben que estoy atento a uds.

No hay comentarios:

Publicar un comentario