Home arrow Java arrow Page 2 - Generics of Java 1.5 Tiger
JAVA

Generics of Java 1.5 Tiger


The generics feature of Tiger brings greater type safety to Java, allowing developers to do many things they could not do before. Generics bear on a number of other features specific to Tiger. This article introduces you to generics, and what they can do. It is excerpted from chapter two of Java 1.5 Tiger: A Developer's Notebook, written by Brett McLaughlin and David Flanagan (O'Reilly, 2004; ISBN: 0596007388).

Author Info:
By: O'Reilly Media
Rating: 4 stars4 stars4 stars4 stars4 stars / 25
May 26, 2005
TABLE OF CONTENTS:
  1. · Generics of Java 1.5 Tiger
  2. · Using Type-Safe Maps
  3. · Iterating Over Parameterized Types
  4. · Accepting Parameterized Types as Arguments
  5. · Returning Parameterized Types
  6. · Checking for Lint
  7. · Generics and Type Conversions
  8. · Using Type Wildcards
  9. · Writing Generic Types
  10. · Restricting Type Parameters

print this article
SEARCH DEVARTICLES

Generics of Java 1.5 Tiger - Using Type-Safe Maps
(Page 2 of 10 )

As cool as generics make the List class, it wouldnít be much good if that was the only collection that could be parameterized. All of the various collection classes are now generic types, and accept type parameters. Since most of these behave like List, Iíll spare you the boring prose of covering each one. It is worth looking at Map, though, as it takes two type parameters, instead of just one. You use it just as you use List, but with two types at declaration and initialization.

How do I do that?

java.util.Map has a key type (which can be any type) and a value type (which can be any type). While itís common to use a numeric or String key, thatís not built into the language, and you canít depend on itóat least, not until Tiger came along:

  Map<Integer, Integer> squares = new HashMap<Integer, Integer>();
  for (int i=0; i<100; i++) {
    squares.put(i, i*i);
  }
  for (int i=0; i<10; i++) {
    int n = i*3;
    out.println("The square of " + n + " is " + squares.get(n));
  }

This is a simple example of where a new Map is declared, and both its key and value types are defined as Integer. This ensures that you donít have to do any casting, either in putting values into the Map or pulling them out. Pretty easy stuff, isnít it? Of course, you could use any of the following lines of code as well:

  // Key and value are Strings
  Map<String, String> strings = new HashMap<String, String>();
  // Key is a String, value is an Object
  Map<String, Object> map = new HashMap<String, Object>();
  // Key is a Long, value is a String
  Map<Long, String> args = new HashMap<Long, String>();

What just happened?

As briefly mentioned in ďUsing Type-Safe Lists,Ē autoboxing helps when you want to stuff primitives into a collection. In this case, even though the Map is defined to take Integers, itís the int counter i that is used to create values. Without getting into the details covered in Chapter 4, Java autoboxes the int value of i into an Integer, behind the scenes, meeting the requirements of the squares Map.


blog comments powered by Disqus
JAVA ARTICLES

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- Deploying Multiple Java Applets as One
- Deploying Java Applets
- Understanding Deployment Frameworks
- Database Programming in Java Using JDBC
- Extension Interfaces and SAX
- Entities, Handlers and SAX
- Advanced SAX
- Conversions and Java Print Streams
- Formatters and Java Print Streams
- Java Print Streams
- Wildcards, Arrays, and Generics in Java
- Wildcards and Generic Methods in Java
- Finishing the Project: Java Web Development ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials