Read without ads and support Scribd by becoming a Scribd Premium Reader.
Generics and Collections in Java 5
Maurice Naftalin
naftalin@morninglight.co.uk
Philip Wadler
wadler@inf.ed.ac.uk
This version is a draft. Please send comments to the addresses above.
Copyrightc
\ue0002005 O\u2019Reilly Media, Inc.
2
Contents
I Introduction to Generics
7
1 Gettingstarted
11

1.1 Generics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .12 1.2 Boxing and unboxing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .14 1.3 Foreach. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .16 1.4 Generic methods and varargs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .17 1.5 Assertions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .19 1.6 Summingup. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .19

2 Subtyping and wildcards
21

2.1 Subtyping and The Substitution Principle. . . . . . . . . . . . . . . . . . . . . . . . . .21 2.2 Wildcards with extends. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .22 2.3 Wildcards with super. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .23 2.4 The Get and Put Principle. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .24 2.5 Arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .27 2.6 Wildcards vs. type parameters. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .29 2.7 Wildcardcapture. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .31 2.8 Restrictions on wildcards. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .32

3 Comparison and bounds
35

3.1 Comparable. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .35 3.2 Maximum of a collection. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .37 3.3 A fruity example. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .39 3.4 Comparator. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .42 3.5 Enumeratedtypes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .45 3.6 Multiplebounds. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .46 3.7 Bridges. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .48 3.8 Staticmembers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .49 3.9 Nestedclasses. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .51 3.10 How erasure works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .52

4 Evolution not revolution
57
4.1 Legacy library with legacy client. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .57
4.2 Generic library and generic client. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .58
3
Search History:
Searching...
Result 00 of 00
00 results for result for
  • p.
  • Notes
    Load more