Ver Mensaje Individual
  #11 (permalink)  
Antiguo 10/01/2007, 16:34
Avatar de stock
stock
 
Fecha de Ingreso: junio-2004
Ubicación: Monterrey NL
Mensajes: 2.390
Antigüedad: 19 años, 10 meses
Puntos: 53
Re: Que es un HashMap y si alguien sabe un manual o donde puedo informarme

Generic Types

Generic types have been widely anticipated by the Java Community and are now part of J2SE 5.0. One of the first places to see generic types in action is the Collections API. The Collections API provides common functionality like LinkedLists, ArrayLists and HashMaps that can be used by more than one Java type. The next example uses the 1.4.2 libraries and the default javac compile mode.

Código PHP:
ArrayList list = new ArrayList();
  list.
add(0, new Integer(42)); 
  
int total = ((Integer)list.get(0)).intValue(); 
The cast to Integer on the last line is an example of the typecasting issues that generic types aim to prevent. The issue is that the 1.4.2 Collection API uses the Object class to store the Collection objects, which means that it cannot pick up type mismatches at compile time. The first notification of a problem is a ClassCastException at runtime.

The same example with the generified Collections library is written as follows:
Código PHP:
ArrayList<Integer> list =  new ArrayList<Integer>();
   list.
add(0, new Integer(42));
   
int total = list.get(0).intValue(); 
The user of a generified API has to simply declare the type used at compile type using the <> notation. No casts are needed and in this example trying to add a String object to an Integer typed collection would be caught at compile time.

Generic types therefore enable an API designer to provide common functionality that can be used with multiple data types and which also can be checked for type safety at compile time.

Designing your own Generic APIs is a little more complex that simply using them. To get started look at the java.util.Collection source and also the API guide.


Have funnnnnn