/  7
 
8/18/20091
Object Oriented Programming
Colecciones
Java Collections Framework
2
 
General featuresGenerics and parameterizationOverriding equals() and hashCode()EnumerationsDefining generic abstract typesor-eac conro srucureAutoboxing
The Java Collection API
 
8/18/20092
Collections Framework
Collection
 –Generic container, most of the framework implements this
List
 –stores multiple items in an explicit order (repeated elements allowed) –ArrayList, LinkedList, etc.
Set
 –stores unique items in no particular order –HashSet, SortedSet, etc.
Map
 –stores unordered key-value pairs (like a dictionary; keys are unique) –HashMap, Hastable, TreeMap, etc.Buena práctica: –No exponer tipos de datos usados innecesariamente• E.g., declare Map, instancie un HashMap()
General-Purpose Implementationclasses
The primary implementations of the collection interfaces.HashSet: Hash table implementation of theSetinterface. TreeSet: Red-black tree implementation of theSortedSetinterface. ArrayList: Resizable-array implementation of theListinterface.
 –(Essentially anunsynchronizedVector.) The best all-aroundimplementation of the List interface.
LinkedList: Doubly-linked list implementation of theListinterface.
 –May provide better performance than the ArrayList implementation ifelements are frequently inserted or deleted within the list. –Useful for queues and double-ended queues (deques).
HashMap: Hash table implementation of the Map interface.
 –(Essentially anunsynchronizedHashtable that supports null keys andvalues.) The best all-around implementation of the Map interface.
TreeMap: Red-black tree implementation of the SortedMap interface.
Collection Operation
Collections typically provide these operations:
 –add(Object o) –add an object to the collection –remove(Object o) remove the object –clear() –remove all objects from collection –size() returns a count of obects in collection
 
 –isEmpty() –returns true if collection is empty –iterator() –traverse contents of collection –contains(Object element); // use equals() for comparison
Someoperationsare optionalSomeoperationsare slower/faster
Generic Types
Antes de Java 5, se podía recorrer una lista comosigue:
public String concat(ArrayList list) {int i;StringBuffer buff = new StringBuffer();for (i = 0; i< list.size(); i++) {String element = (String) lista.get(i);buff.append(element);}return buff.toString();}
Qué problema hay? ……. Casting?
 
8/18/20093
Generic Types
A partir de Java 5
public String concat(List<String> list) {StringBuffer buff = new StringBuffer();for (String element : list) {buff.append(element);}return buff.toString();}
Es código más corto y garantiza que el tipo de datos de la lista esString, además no requiere casting
Arraylist, Generics
Crear un ArrayList usando Generics:
ArrayList<Integer> listA = new ArrayList<Integer>();
Añadir elementos al ArrayList
listAaddnewInteeri
 
.listA.add(new Integer(i+1));OlistA.add(1); //Gracias a Autoboxing
Arraylist in a program
Buscar
por un Object y retornar su posición
int locationIndex = listA.indexOf("Jeff");
Arraylist in a program
Check to see if the List is empty
:
System.out.println("Is List A empty?+ stA.sEmpty());

Share & Embed

More from this user

Add a Comment

Characters: ...