`

More Fun with Wildcards and <T> before method return type T

 
阅读更多

In this section, we’ll consider some of the more advanced uses of wildcards. We’ve

seen several examples where bounded wildcards were useful when reading from a data
structure. Now consider the inverse, a write-only data structure.
The interface Sink is a simple example of this sort.

 

interface Sink<T> {
    flush(T t);
}

 

We can imagine using it as demonstrated by the code below. The method writeAll()
is designed to flush all elements of the collection coll to the sink snk, and return the
last element flushed.

 

public static <T> T writeAll(Collection<T> coll, Sink<T> snk) { 
    T last;
    for (T t : coll) { 
        last = t;
        snk.flush(last);
    }
    return last;
}
...
Sink<Object> s;
Collection<String> cs;
String str = writeAll(cs, s); // illegal call

 

As written, the call to writeAll() is illegal, as no valid type argument can be inferred;
neither String nor Object are appropriate types for T, because the Collection element
and the Sink element must be of the same type.
We can fix this by modifying the signature of writeAll() as shown below, using a
wildcard.

 

public static <T> T writeAll(Collection<? extends T>, Sink<T>) {
    ...
}
... 
String str = writeAll(cs, s); // call ok, but wrong return type
 

The call is now legal, but the assignment is erroneous, since the return type inferred
is Object because T matches the element type of s, which is Object.
The solution is to use a form of bounded wildcard we haven’t seen yet: wildcards
with a lower bound. The syntax ? super T denotes an unknown type that is a
supertype of T3. It is the dual of the bounded wildcards we’ve been using, where we
use ? extends T to denote an unknown type that is a subtype of T.

 

public static <T> T writeAll(Collection<T> coll, Sink<? super T> snk) {
    ...
}
...
String str = writeAll(cs, s); // Yes!

 

Using this syntax, the call is legal, and the inferred type is String, as desired.

 

and i can see that the <T> used by the Type or method describes the identical T usage within its domain.

分享到:
评论

相关推荐

    java反编译工具jad 1.5.8g(可以反编译jdk1.5,1.6)

    sjava *.class&lt;br&gt;&lt;br&gt; (or jad -o -d test -s java *.class, which has the same effect)&lt;br&gt;&lt;br&gt;This command decompiles all .class files in the current directory &lt;br&gt;and places all output files with ...

    Efficient string matching with wildcards and length constraints

    Efficient string matching with wildcards and length constraints

    Java反编译软件JAD1

    -pp &lt;pfx&gt;- prefix for method parms with numerical names (default: _prm) -r - restore package directory structrure -s &lt;ext&gt; - output file extension (by default '.jad') -stat - display the total ...

    PLSQL.Developer v11.0.4.1774 主程序+ v11中文包+keygen

    REPORT LOCKFORALL &lt;path&gt; &lt;password&gt; [R] REPORT LOCKFOROTHERS &lt;path&gt; &lt;password&gt; [R] REPORT UNLOCK &lt;path&gt; &lt;password&gt; [R] REPORT LIST &lt;path&gt; [R] For the path you can use wildcards or directories. Add ...

    带任意长度通配符的模式匹配

    提出了一个更普遍的问题, 带任意长度通配符的模式匹配问题(Pattern matching with &lt;br&gt; arbitrary-length wildcards, PMAW), 这里模式中不仅可以有多个通配符约束, 而且每个通配符的约束可以是两个整数, 也可 &lt;br&gt; ...

    powershell-fix-files:用于管理文件名和属性的PowerShell脚本

    http: 用法 Fix-TS.ps1 &lt;Path&gt; [-filter &lt;filter&gt;] [-fix] [-all] [-rename &lt;rename&gt;] [-source &lt;timestamp&gt;] path - path to a folder with files to check/fix -filter - array of wildcards to filter files, e.g...

    VclZip pro v3.10.1

    If you currently work with VCLZip 2.X with TBlobStreams or some other type of streams, you can either define your own TkpBlobStream for instance which inherits from TkpHugeStream, or use the ...

    OutlookAttachView v2.73

    or more attachments and save all of them into the desired folder, as well as you can delete unwanted large attachments that take too much disk space in your mailbox. You can also save the list of ...

    MAIL: mining sequential patterns with wildcards

    MAIL: mining sequential patterns with wildcards

    Java1.5泛型指南中文版

    1. 介绍 2. 定义简单的泛型 3. 泛型和子类继承 4. 通配符(Wildcards) 4.1. 有限制的通配符(Bounded Wildcards) 5. 泛型方法 ...9. More fun with * 9.1. 通配符匹配(wildcard capture) 10. 泛型化老代码

    String matching with wildcards.rar_deerg91_jumpi9a_matched_带通配符的

    (大一计算机新生作业)带通配符的字符串匹配 输入两个字符串(不包含空格)用空格分开,前一个字符串中含有通配符‘*’和‘?’,其中‘*’可代替任意长度的字符串,'?'可代替任一字符,若前者与后者匹配,输出...

    Effective Java 3rd edition(Effective Java第三版英文原版)附第二版

    Item 47: Prefer Collection to Stream as a return type Item 48: Use caution when making streams parallel 8 Methods Item 49: Check parameters for validity Item 50: Make defensive copies when needed Item...

    Java1.5泛型指南中文版(Java1.5Gene....pdf

    9. More fun with *:通配符匹配(wildcard capture)可以用来实现泛型的类型安全。 10. 泛型化老代码:可以将老代码泛型化,以提高代码的类型安全和可读性。 11.致谢:感谢 Java 1.5 泛型指南中文版的作者和翻译...

    A BIT-PARALLEL ALGORITHM FOR SEQUENTIAL PATTERN MATCHING WITH WILDCARDS

    Pattern matching with both gap constraints and the one-off condition is a challenging topic, especially in bioinformatics, information retrieval, and dictionary query. Among the algorithms to solve ...

    Scala for the Impatient 2nd (完整英文第二版 带书签)

    xii Contents14.10 The copy Method and Named Parameters 205 14.11 Infix Notation in case Clauses 206 14.12 Matching Nested Structures 207 14.13 Are Case Classes Evil? 208 14.14 Sealed Classes 209 14.15...

    Turbo C 2.01[DISK]

    different assembler, use -E&lt;filename&gt;, as described in the Reference Guide. o When using THELP on an AT&T 6300, be sure to use the /L25 command-line option, as described in the file THELP.DOC. ...

    Turbo C 2.00[DISK]

    different assembler, use -E&lt;filename&gt;, as described in the Reference Guide. o When using THELP on an AT&T 6300, be sure to use the /L25 command-line option, as described in the file THELP.DOC. ...

    SQLMemTable for Delphi / C++ Builder

    BLOB compression: ZLib, BZip, PPM Search operators: &lt;,&gt;,=,&lt;&gt;,&lt;=,&gt;=, like, not like, is null, is not null, and, or, not, (). Features and Benefits---------------------Compactness. - Short compiled ...

    'FrontEnd Plus' The GUI for the fast JAva Decompiler.

    Option -s &lt;ext&gt; allows to change output file extension: jad -sjava example1.class This command creates file 'example1.java'. Be careful when using options -o and -sjava together, because Jad can ...

    Astyle(格式调整)

    astyle [options] &lt; Original &gt; Beautified When indenting a specific file, the resulting indented file RETAINS the original file-name. The original pre-indented file is renamed, with a suffix of "....

Global site tag (gtag.js) - Google Analytics