Tải bản đầy đủ (.pdf) (11 trang)

Collections Resources

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (85.2 KB, 11 trang )

Appendix B: Collections Resources
This appendix is meant to provide a useful collection of resources on the Collections Framework and related
libraries discussed in this book. While I'd like to say it is exhaustive, I'm sure I missed something worthwhile
given the nature of the Web. For an online version of this list, see
/>Collections Implementations
/>The Collections Framework from Sun.♦

/>The Generic Collection Library for Java (JGL) from ObjectSpace.♦

/>Doug Lea's util.concurrent library.♦

/>Doug Lea's (outdated) collections package.♦

http://tilde−hoschek.home.cern.ch/~hoschek/colt/index.htm
The Colt Distribution.♦

l−labs.com/who/wadler/pizza/gj/
GJ: Generic Java.♦

/>PolyJ: Java with Parameterized Types.♦

/>The C++ Standard Template Library.♦

/>The Data Structures Library in Java.♦

/>The Java Algorithm Library (JAL).♦

/>alphaWorks Collection Bean Suit.♦

/>Java Collections Clearinghouse.♦


272
Collections FAQs and Tutorials
/>Collections Framework design FAQ.♦

/>Collections FAQ from jGuru.com.♦

l−labs.com/who/wadler/pizza/gj/FAQ/
GJ: Generic Java FAQ.♦

/>PolyJ Design FAQ.♦

/>Introduction to Collections Framework tutorial from the Java Developer Connection.♦

/>The Java Tutorial trail on the Collections Framework.♦

/>"The battle of the container frameworks: which should you use?" A Java−World article
comparing JGL and the Collections Framework.


/>"Get started with the Java Collections Framework." Another JavaWorld article to get you
started.


/>JDSL Tutorials.♦

/>Richard Baldwin's Java Programming Tutorials, includes collections lessons.♦

Mailing Lists and Newsgroups
/>Advanced−java email list for technical discussions about full−blown Java applications.♦


news:comp.lang.java.help and news:comp.lang.java.programmer
The main Usenet news groups do not have a discussion group specific to the Collections
Framework. Your best bet for finding help are these two general−purpose groups.


Collections FAQs and Tutorials
273
Collections−Related Web Sites and Information
/>Java Specification Request (JSR) 14: For adding generic types to the Java Programming
Language.


/>Bug parade for adding parameterized types.♦

/>Partial Collections Framework library for use with Java 1.1.♦

/>Collections chapter from my Mastering Java 2 book (Sybex Inc., 1998) at the Java Developer
Connection.


Collections−Related Web Sites and Information
274
Appendix C: Generic Types
Overview
Generic types, parameterized types, or templates are essentially different names for the same thing. (Though
some will argue that generic types are not templates.) They represent the ability to work with collections of
objects in a type−safe manner where, instead of working with a collection of Object instances, you work with
a collection of a specific type. They are not presently a part of the Java programming language. At the time
Java was introduced back in 1995, James Gosling, the father of Java, was strongly interested in keeping things
simple and getting things right. The concept of templates was not simple and sufficient resources couldn't be

spent on getting things designed properly, thus templates were left out of the initial release of Java.
Turn the clocks ahead a few years and there is strong desire from the user community to have generic types
added into Java. Looking at the "Top 25 Requests for Enhancements" (RFEs) in the Java Bug Database at
you'll see template support as the second
most requested feature below support for porting Java to FreeBSD and just above support for assertions.
Because adding generic types would require language−level changes and can't be done just by adding on
another class library, the process of adding support has been going through the Java Community Process
(JCP) as JSR #14.
While I haven't personally been involved with the JCP for this Java Specification Request (JSR), rumor has it
that support for generic types will be added to some upcoming release of the Java 2 platform. In what specific
form this will be available is completely unknown. What follows in the rest of this appendix is a review of
two publicly available implementations that were used as the basis of the JSR submission: Generic Java (GJ)
and PolyJ. I hope that this appendix will get you primed for the changes and enable you to use them more
easily once they are available.
Note In an earlier lifetime, the GJ implementation was known as Pizza.
Generic Type Basics
The basic concept of working with generic types is that when you define a class, you specify where the
generic type fits in. For instance, the following defines a Vector of some type T, the type parameter:
Vector<T>
When the class is later used, the actual type is specified, either in a variable declaration:
Vector<String> stringVector;
. . .or in calling its constructor:
new Vector<String>()
Now, whenever you add elements to the collection or get items out, the only datatype that works is that which
was passed to the template, in this case, String. If, when using the object, you pass in an incompatible
datatype, you'll get a compile−type error, instead of waiting for a runtime exception when you try to cast to an
improper type.
275
While you can create a type−safe container without generic types, you cannot create one that is a subclass of
Vector as the rules of overridden methods don't let you return a more specific subtype. In addition, while you

can create methods that only accept the type you want, you cannot hide the methods that accept an Object, so
essentially you haven't changed or hidden anything.
With generic types, you can create type−safe specific containers easily by simply replacing the type
parameter. For instance, in addition to vectors of type String, you can also have Button, Integer, or classes that
you knew nothing about when you defined your custom, template−friendly class but the person using your
class defined instead:
Vector<Button>
Vector<Integer>
Vector<MyClass>
That's really all there is to templates. Behind the scenes, it is a little more involved that that and the exact
syntax that will be introduced (assuming the JSR is implemented) is still to be determined. Templates
essentially add the ability to define generic constructs that accept arbitrary types without using inheritance or
composition.
Getting generic types to work without changing the Java Virtual Machine (JVM) and with nongeneric
libraries requires a bit of thought, as do issues like support for primitive types and dealing with run−time type
data (for instance, with casting and instanceof). Besides just the syntax, these are the issues that the working
group is actually working on.
Generic Java
Generic Java (GJ) is one such implementation for adding parameterized−type support into Java. It relies on a
preprocessor to work and provides a custom set of parameterized core library classes that need to be properly
installed into the bootclasspath before the system libraries in order to run. GJ was designed by Gilad Bracha
of Sun Microsystems, Martin Odersky of the University of South Australia, David Stoutamire of Sun
Microsystems, and Philip Wadler of Bell Labs, Lucent Technologies. The following demonstrates how to
create and use classes with GJ.
Defining Generic Classes and Interfaces
Classes and interfaces designed to support templates are defined by specifying the type parameter with the
class definition. GJ uses angle brackets to surround the type parameter, as in <A>. For example, the following
demonstrates the creation of a LinkedList class that implements a Collection interface, which supports an
Iterator for iterating through the elements:
interface Collection<A> {

public void add(A x);
public Iterator<A> iterator();
}
interface Iterator<A> {
public A next();
public boolean hasNext();
}
class NoSuchElementException extends RuntimeException {}
Generic Java
276

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×