Multiple Type Parameters


If you look at the API documentation for java.util.List and java.util.ArrayList, you will see that they are declared as List<E> and ArrayList<E>, respectively. The <E> is known as the type parameter list. The List interface and ArrayList class declarations each contain a single type parameter in the type parameter list. Therefore you must supply a single bind type when using List or ArrayList in your code.[1]

[1] You are not required to supply any bind types; however, this is not recommended. See the section in this lesson entitled Raw Types.

The class HashMap represents a collection of key-value pairs. A key can be of one type and the value could be another. For example, you might store an appointment book in a HashMap, where the key is a Date and the value is a String description of an event occurring on that date.

 final String event = "today"; final Date date = new Date(); Map<Date, String> events = new HashMap<Date, String>(); events.put(date, event); String retrievedEvent = events.get(date); assertEquals(event, retrievedEvent); 

The Map interface and HashMap class are declared as Map<K,V> and HashMap<K,V>, respectively. You must supply two bind types when using theseone for the key (K) and one for the value (V).



Agile Java. Crafting Code with Test-Driven Development
Agile Javaв„ў: Crafting Code with Test-Driven Development
ISBN: 0131482394
EAN: 2147483647
Year: 2003
Pages: 391
Authors: Jeff Langr

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net