Oreilly java generics and collections oct 2006 ebook bbl




















This comprehensive guide shows you how to master the most importantchanges to Java since it was first released. Generics and the greatlyexpanded collection libraries have tremendously increased the power ofJava 5 and Java 6.

But they have also confused many developers whohaven't known how to take advantage of these new features. Java Generics and Collections covers everything from the mostbasic uses of generics to the strangest corner cases.

It teaches youeverything you need to know about the collections libraries, so you'llalways know which collection is appropriate for any given task, andhow to use it. Here is a method to compute the dot product of two vectors, represented as lists of doubles, both of the same length. Two iterators, uIt and vIt , advance across the lists u and v in lock step.

The loop condition checks only the first iterator, but the assertions confirm that we could have used the second iterator instead, since we previously tested both lists to confirm that they have the same length. Here is a method that accepts an array of any type and converts it to a list:. A method which declares a type variable in this way is called a generic method. The scope of the type variable T is local to the method itself; it may appear in the method signature and the method body, but not outside the method.

In the first line, boxing converts 1, 2, 3 from int to Integer. Packing the arguments into an array is cumbersome. The vararg feature permits a special, more convenient syntax for the case in which the last argument of a method is an array.

To use this feature, we replace T[] with T… in the method declaration:. This is just shorthand for what we wrote above. At run time, the arguments are packed into an array which is passed to the method, just as previously. Any number of arguments may precede a last vararg argument. Here is a method that accepts a list and adds all the additional arguments to the end of the list:.

Whenever a vararg is declared, one may either pass a list of arguments to be implicitly packed into an array, or explicitly pass the array directly. Thus, the preceding method may be invoked as follows:. We will see later that when we attempt to create an array containing a generic type, we will always receive an unchecked warning.

Since varargs always create an array, they should be used only when the argument does not have a generic type see Array Creation and Varargs. In the preceding examples, the type parameter to the generic method is inferred, but it may also be given explicitly, as in the following examples:. Explicit parameters are usually not required, but they are helpful in the examples given here. It infers that the argument to toList is an empty array of an arbitrary generic type rather than an empty array of integers, and this triggers the unchecked warning described earlier.

The Eclipse compiler uses a different inference algorithm, and compiles the same line correctly without the explicit parameter. In the second example, without the type parameter there is too much information for the type inference algorithm to infer the correct type. You might think that Object is the only type that an integer and a string have in common, but in fact they also both implement the interfaces Serializable and Comparable.

The type inference algorithm cannot choose which of these three is the correct type. In general, the following rule of thumb suffices: in a call to a generic method, if there are one or more arguments that correspond to a type parameter and they all have the same type then the type parameter may be inferred; if there are no arguments that correspond to the type parameter or the arguments belong to different subtypes of the intended type then the type parameter must be given explicitly.

When a type parameter is passed to a generic method invocation, it appears in angle brackets to the left, just as in the method declaration. The Java grammar requires that type parameters may appear only in method invocations that use a dotted form. Even if the method toList is defined in the same class that invokes the code, we cannot shorten it as follows:. Methods Arrays. Both classes are in package java. The Collections Framework version of asList does not return an ArrayList , but instead returns a specialized list class that is backed by a given array.

Also, its version of addAll acts on general collections, not just lists. We clarify our code by liberal use of the assert statement. Each occurrence of assert is followed by a boolean expression that is expected to evaluate to true. If assertions are enabled and the expression evaluates to false , an AssertionError is thrown, including an indication of where the error occurred. Assertions are enabled by invoking the JVM with the -ea or -enableassertions flag.

We only write assertions that we expect to evaluate to true. Since assertions may not be enabled, an assertion should never have side effects upon which any nonassertion code depends. When checking for a condition that might not hold such as confirming that the arguments to a method call are valid , we use a conditional and throw an exception explicitly.

To sum up, we have seen how generics, boxing and unboxing, foreach loops, and varargs work together to make Java code easier to write, having illustrated this through the use of the Collections Framework.

Skip to main content. Start your free trial. Chapter 1. Boxing and Unboxing. He is a co-designer of GJ, work thatbecame the basis for generics in Sun's Java 5. He has most recently served as an architect and mentor at NSB Retail Systems plc, and as the leader of the client development team of a major UK government social service system. By far the best book on thetopic, it provides a crystal clear tutorial that starts with thebasics and ends leaving the reader with a deep understanding of boththe use and design of generics.

Java 5 introduced generics capabilities to the language, and for most developers, simply knowing that collections …. Today, software engineers need to know not only how to program effectively but also how to …. The introduction of functional programming concepts in Java SE 8 was a drastic change for this ….

Distributed systems have become more fine-grained as organizations shift from code-heavy monolithic applications to smaller, self-contained …. Skip to main content. Start your free trial. Buy on Amazon.



0コメント

  • 1000 / 1000