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.
分享到:
相关推荐
sjava *.class<br><br> (or jad -o -d test -s java *.class, which has the same effect)<br><br>This command decompiles all .class files in the current directory <br>and places all output files with ...
Efficient string matching with wildcards and length constraints
-pp <pfx>- prefix for method parms with numerical names (default: _prm) -r - restore package directory structrure -s <ext> - output file extension (by default '.jad') -stat - display the total ...
REPORT LOCKFORALL <path> <password> [R] REPORT LOCKFOROTHERS <path> <password> [R] REPORT UNLOCK <path> <password> [R] REPORT LIST <path> [R] For the path you can use wildcards or directories. Add ...
提出了一个更普遍的问题, 带任意长度通配符的模式匹配问题(Pattern matching with <br> arbitrary-length wildcards, PMAW), 这里模式中不仅可以有多个通配符约束, 而且每个通配符的约束可以是两个整数, 也可 <br> ...
http: 用法 Fix-TS.ps1 <Path> [-filter <filter>] [-fix] [-all] [-rename <rename>] [-source <timestamp>] path - path to a folder with files to check/fix -filter - array of wildcards to filter files, e.g...
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 ...
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
1. 介绍 2. 定义简单的泛型 3. 泛型和子类继承 4. 通配符(Wildcards) 4.1. 有限制的通配符(Bounded Wildcards) 5. 泛型方法 ...9. More fun with * 9.1. 通配符匹配(wildcard capture) 10. 泛型化老代码
(大一计算机新生作业)带通配符的字符串匹配 输入两个字符串(不包含空格)用空格分开,前一个字符串中含有通配符‘*’和‘?’,其中‘*’可代替任意长度的字符串,'?'可代替任一字符,若前者与后者匹配,输出...
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...
9. More fun with *:通配符匹配(wildcard capture)可以用来实现泛型的类型安全。 10. 泛型化老代码:可以将老代码泛型化,以提高代码的类型安全和可读性。 11.致谢:感谢 Java 1.5 泛型指南中文版的作者和翻译...
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 ...
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...
different assembler, use -E<filename>, 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. ...
different assembler, use -E<filename>, 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. ...
BLOB compression: ZLib, BZip, PPM Search operators: <,>,=,<>,<=,>=, like, not like, is null, is not null, and, or, not, (). Features and Benefits---------------------Compactness. - Short compiled ...
Option -s <ext> 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 [options] < Original > 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 "....