- 浏览: 1137521 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (411)
- Java Foundation (41)
- AI/机器学习/数据挖掘/模式识别/自然语言处理/信息检索 (2)
- 云计算/NoSQL/数据分析 (11)
- Linux (13)
- Open Source (12)
- J2EE (52)
- Data Structures (4)
- other (10)
- Dev Error (41)
- Ajax/JS/JSP/HTML5 (47)
- Oracle (68)
- FLEX (19)
- Tools (19)
- 设计模式 (4)
- Database (12)
- SQL Server (9)
- 例子程序 (4)
- mysql (2)
- Web Services (4)
- 面试 (8)
- 嵌入式/移动开发 (18)
- 软件工程/UML (15)
- C/C++ (7)
- 架构Architecture/分布式Distributed (1)
最新评论
-
a535114641:
LZ你好, 用了这个方法后子页面里的JS方法就全不能用了呀
页面局部刷新的两种方式:form+iframe 和 ajax -
di1984HIT:
学习了,真不错,做个记号啊
Machine Learning -
赵师傅临死前:
我一台老机器,myeclipse9 + FB3.5 可以正常使 ...
myeclipse 10 安装 flash builder 4.6 -
Wu_Jiang:
触发时间在将来的某个时间 但是第一次触发的时间超出了失效时间, ...
Based on configured schedule, the given trigger will never fire. -
cylove007:
找了好久,顶你
Editable Select 可编辑select
Generics allow you to abstract over types
这里的 types 指的是什么?The Java programming language includes classes and interfaces, both are collectively re-ferred to as types. (见附件 Choosing Efficient Inheritance Patterns for Java Generics)
好文一篇 - Java Generics FAQs:
http://www.angelikalanger.com/GenericsFAQ/JavaGenericsFAQ.html
Java Tutorials - Generics(Updated):
http://docs.oracle.com/javase/tutorial/java/generics/index.html
Java Tutorials - Generics(老的,看看即可):
http://docs.oracle.com/javase/tutorial/extra/generics/intro.html
http://www.oracle.com/technetwork/articles/javase/generics-136597.html
Java 1.5 Generics Tutorial: How Generics in Java works with Example of Collections, Best practices, Gotchas:
http://javarevisited.blogspot.com/2011/09/generics-java-example-tutorial.html
几个概念:
generic type
引用
A generic type is a generic class or interface that is parameterized over types.
type parameter(also called type variable)
引用
A place holder for a type argument.
Generic types have one or more type parameters.
Each type parameter is replaced by a type argument when an instantiation of the generic type.
By convention, type parameter names are single, uppercase letters. This stands in sharp contrast to the variable naming conventions that you already know about, and with good reason: Without this convention, it would be difficult to tell the difference between a type variable and an ordinary class or interface name.
The most commonly used type parameter names are:
E - Element (used extensively by the Java Collections Framework)
K - Key
N - Number
T - Type
V - Value
S,U,V etc. - 2nd, 3rd, 4th types
Generic types have one or more type parameters.
Each type parameter is replaced by a type argument when an instantiation of the generic type.
By convention, type parameter names are single, uppercase letters. This stands in sharp contrast to the variable naming conventions that you already know about, and with good reason: Without this convention, it would be difficult to tell the difference between a type variable and an ordinary class or interface name.
The most commonly used type parameter names are:
E - Element (used extensively by the Java Collections Framework)
K - Key
N - Number
T - Type
V - Value
S,U,V etc. - 2nd, 3rd, 4th types
type argument
引用
A reference type or a wildcard that is used for instantiation of a generic type or a reference type used for instantiation of a generic method. An actual type argument replaces the formal(形式化的) type parameter used in the declaration of the generic type or method.
parameterized type
引用
The instantiation(or invocation) of a generic type with actual type arguments is called a parameterized type
type inference
引用
Type inference is a Java compiler's ability to look at each method invocation and corresponding declaration to determine the type argument (or arguments) that make the invocation applicable. The inference algorithm determines the types of the arguments and, if available, the type that the result is being assigned, or returned. Finally, the inference algorithm tries to find the most specific type that works with all of the arguments.
例子
/** * List & ArrayList 都是 generic type(细说的话,前者是 generic interface, 后者是 generic class) * List & Array 类定义上的 <E> 中的 E 为 type parameter(also called type variable) * Integer 为 type argument * List<Integer> 为 parameterized type * */ List<Integer> list = new ArrayList<Integer>();
Generic Methods
Not only types can be generic, but methods can be generic, too. Static and non-static methods as well as constructors can have type parameters. The syntax for declaration of the formal type parameters is similar to the syntax for generic types. The type parameter section is delimited by angle brackets and appears before the method's return type. Its syntax and meaning is identical to the type parameter list of a generic type.
Wildcards(通配符)
http://docs.oracle.com/javase/tutorial/java/generics/subtyping.html
In generic code, the question mark (?), called the wildcard, represents an unknown type, or a family of types.
There are 3 different flavors of wildcards:
" ? " - the unbounded wildcard. It stands for the family of all types.
" ? extends Type " - a wildcard with an upper bound. It stands for the family of all types that are subtypes of Type , type Type being included.
" ? super Type " - a wildcard with a lower bound. It stands for the family of all types that are supertypes of Type , type Type being included.
PECS (short for Producer extends and Consumer super)
http://stackoverflow.com/questions/2723397/java-generics-what-is-pecs引用
要站在collection的角度理解 producer & consumer:
当需要 get(从 collection 中取) 时,collection 扮演的是 producer 的角色,所以使用 extends;
当需要 put(往 collection 中放) 时,collection 扮演的是 consumer 的角色,所以使用super。
当需要 get(从 collection 中取) 时,collection 扮演的是 producer 的角色,所以使用 extends;
当需要 put(往 collection 中放) 时,collection 扮演的是 consumer 的角色,所以使用super。
Bounded Type Parameter 和 upper bounded wildcard 的区别是什么?
http://stackoverflow.com/questions/1750273/what-is-the-difference-between-bounded-wildcard-and-type-parameters?rq=1
http://stackoverflow.com/questions/3486689/java-bounded-wildcards-or-bounded-type-parameter
泛型与继承:
http://www.ibm.com/developerworks/cn/java/j-lo-gj/
引用
在 Java 语言中,我们可以将某种类型的变量赋值给其父类型所对应的变量,例如,String 是 Object 的子类型,因此,我们可以将 String 类型的变量赋值给 Object 类型的变量,甚至可以将 String [ ] 类型的变量(数组)赋值给 Object [ ] 类型的变量,即 String [ ] 是 Object [ ] 的子类型。
上述情形恐怕已经深深地印在了广大读者的脑中,对于泛型来讲,上述情形有所变化,因此请广大读者务必引起注意。为了说明这种不同,我们还是先来分析一个小例子,代码如下所示:
如果上述赋值是合理的,那么上面代码的第三行的操作将是可行的,因为 lo是 List<Object>,所以向其添加 Integer 类型的元素应该是完全合法的。读到此处,我们已经看到了第二行的这种赋值所潜在的危险,它破坏了泛型所带来的类型安全性。
一般情况下,如果 A 是 B 的子类型,C 是某个泛型的声明,那么 C<A>并不是 C<B>的子类型,我们也不能将 C<A>类型的变量赋值给 C<B>类型的变量。这一点和我们以前接触的父子类型关系有很大的出入,因此请读者务必引起注意。
上述情形恐怕已经深深地印在了广大读者的脑中,对于泛型来讲,上述情形有所变化,因此请广大读者务必引起注意。为了说明这种不同,我们还是先来分析一个小例子,代码如下所示:
List<String> ls = new ArrayList<String>(); List<Object> lo = ls; lo.add(new Integer()); String s = ls.get(0);上述代码的第二行将 List<String>赋值给了 List<Object>,按照以往的经验,这种赋值好像是正确的,因为 List<String>应该是 List<Object>的子类型。这里需要特别注意的是,这种赋值在泛型当中是不允许的! List<String>也不是 List<Object>的子类型。
如果上述赋值是合理的,那么上面代码的第三行的操作将是可行的,因为 lo是 List<Object>,所以向其添加 Integer 类型的元素应该是完全合法的。读到此处,我们已经看到了第二行的这种赋值所潜在的危险,它破坏了泛型所带来的类型安全性。
一般情况下,如果 A 是 B 的子类型,C 是某个泛型的声明,那么 C<A>并不是 C<B>的子类型,我们也不能将 C<A>类型的变量赋值给 C<B>类型的变量。这一点和我们以前接触的父子类型关系有很大的出入,因此请读者务必引起注意。
- generics-tutorial.pdf (70 KB)
- 下载次数: 1
- Choosing_Efficient_Inheritance_Patterns_for_Java_Generics.pdf (158.8 KB)
- 下载次数: 2
发表评论
-
J2SE Evolution
2013-04-11 15:39 1178Java 7 New Features Java SE 7 ... -
未完 Java: IO & NIO(new I/O)
2013-01-11 20:56 2016适用: event and data-driven apps ... -
未完 java设计: naming convention | 命名规范
2012-11-20 16:45 2111应该遵循的规范: 类/接口/属性名,使用名词或形容词 ... -
未完 Java: enum 枚举
2012-11-19 20:29 1789http://stackoverflow.com/que ... -
Java多线程之 concurrent 并发包
2012-11-01 07:47 1986Java Tutorials -> Concur ... -
未完 Java Tips & Tricks & Notes
2012-09-12 10:00 1101Hidden Features of Java: h ... -
未完 Java Socket
2012-09-12 08:42 998Java SocketJava SocketJava Sock ... -
Java For-each Loop & Iterable | 增强型For循环和Iterable接口
2012-09-11 21:50 2033增强型For循环没什么好说的,Just see link ... -
未完 Java Collections | 容器
2012-09-06 11:35 1815Sources: http://docs.oracle.com ... -
Java object Initialization (class Instantiation) | 对象的初始化(即类的实例化)
2012-09-03 09:12 2977类实例即对象 ... -
未完Java class&interfac 's Loading, Linking and Initializing | 类与接口的加载、链接和初始化
2012-08-31 19:01 1646JVM装载一个类的时候,首先检查他有没有父类,如果有父类则装载 ... -
未完 java Static 总结
2012-08-31 18:47 1382static可以用来修饰: 字段 Fields 方法 Meth ... -
未完 JVM Runtime Data Areas & Java Memory Model | 内存分配模型 & Java数据存储
2012-08-31 18:43 1869Java虚拟机内存分配模型 需精读:Chapter 5 of ... -
Java Data Types & Literals | 数据类型 和 字面量
2012-08-30 18:12 3895Java数据类型划分: OR http:// ... -
未完 Variables 变量 (Instance/Class/Local)
2012-08-29 10:59 1672Local/Instance/Class Variables ... -
未完 Regular Expressions | 正则表达式
2011-08-25 11:43 1508Extended Regular Expression ... -
java Date(util.Date/sql.Date/sql.Timestamp/sql.Time) & Oracle DATE Type 时分秒 精度问题
2011-05-17 09:32 3913遇到的问题描述: 数据库为Oracle,其jdbc驱动为ojd ... -
Java byte code (bytecode)
2011-05-04 02:55 3860keys: bytecode, byte code, opco ... -
Java Classloading Mechanism : ClassLoader & ASM & 动态字节码增强
2011-04-21 13:29 2397Setting the class path: http:// ... -
class literal & instance.getClass() & Class.forName(String className)
2011-04-20 12:33 2317常用的几种取得Class类实例的方式: 1 class lit ...
相关推荐
本资料 "[Java泛型和集合].(Java.Generics.and.Collections).Maurice.Naftalin&Philip.Wadler.文字版" 由知名专家Maurice Naftalin和Philip Wadler编著,提供了关于这些主题的深入理解。 **Java泛型** 是自Java...
必要性 在程序日益复杂庞大的,编写泛用性代码的价值愈发变得巨大。...但Java引入泛型的初衷,也许只是为了创造 容器类 也说不定。 站在类库设计者的角度,我们不妨走上一遭。 得益于单根
在"Java-Generics-and-Collections-Example-master"这个压缩包中,可能包含了各种关于Java泛型和集合的实例代码,如创建和操作泛型集合、遍历和搜索元素、使用泛型方法等。通过研究这些示例,开发者可以深入理解这两...
3. 使用泛型:例如 List<Integer> myIntList = new LinkedList()。 泛型的优化: 1. 编译期检查:泛型可以在编译期检查类型的正确性,避免了 ClassCastException。 2. 代码重用:泛型可以将类型参数化,使得代码...
Java Generics and Collections 英文版,详细描述java 泛型技术
在Java编程语言中,泛型(Generics)是一种强大的特性,它允许我们在编写代码时指定容器(如集合)可以存储的数据类型。这提高了代码的安全性和效率,因为编译器可以在编译时检查类型,避免了运行时...
通过阅读"Java Generics and Collections",开发者不仅可以掌握Java泛型和集合的基本使用,还能深入了解它们的高级特性和最佳实践,从而在实际项目中编写出更高质量的代码。这本书对于Java程序员来说是一份宝贵的...
在Java_Generics_And_Collections__2006.chm文件中,可能包含了以下内容: 1. **泛型的基本使用**:介绍如何声明和使用泛型类、泛型接口和泛型方法。 2. **泛型通配符**:如"? extends T"和"? super T",用于表示...
这个压缩包文件"Generics"可能包含了多个示例,用于演示和解释泛型在Java中的应用。 泛型引入于Java 5,它的主要目标是提供编译时的类型检查,避免了类型转换的麻烦,并且能够减少运行时错误。下面我们将详细讨论...
Java中的泛型 JDK1.5 令我们期待很久,可是当他发布的时候却更换版本号为5.0。...而泛型也是一样的,这样写class Java_Generics,V>,这里边的K和V就象方法中的参数str1和str2,也是可变。下面看看例子:
环境:Windows XP Professional、JDK 1.6、Ant 1.7 说明:Java泛型的动机是为解决类型转换在编译时不报错的问题。另外由于“范型编程”(Generic Programming)的推广,于是2004年JDK 5.0引用范型标准。本例子说明...
在Java中,泛型(Generics)引入于J2SE 5.0,目的是提供类型安全的集合,避免了在集合操作中强制类型转换的需要,同时也消除了运行时可能发生的ClassCastException。而集合框架(Collections Framework)是一组接口...
《Java Generics and Collections》是Java开发者必备的参考资料,它深入探讨了Java编程中的泛型(Generics)和集合(Collections)这两个核心概念。在Java编程中,泛型和集合框架是提高代码效率、可读性和类型安全性...
泛型与集合 使用 进行初步翻译. 将利用碎片时间进行整理和校对,完整的时间段适合做其他需要大量思考的事,如果你有兴趣欢迎提交PR。 TODO 数据校对 目录 2.4 获取和放置原则 2.5 数组 2.6 通配符与类型参数 2.7 ...
在Java编程语言中,泛型(Generics)是一种强大的特性,它允许我们在编写代码时指定容器(如集合)可以存储的数据类型。这提高了代码的类型安全性,并在编译时就能发现可能的类型错误,而非运行时。在...
在有关Java核心的系列文章中,我们将继续学习2个新内容,即Generics和Collection,它们是Java中非常流行的对象。 泛型格式化参数化数据类型,以便我们可以将类,接口或方法用于许多不同的数据类型。 集合只是具有...
[Java泛型和集合].(Java.Generics.and.Collections).Maurice.Naftalin&Philip.Wadler.文字版
Java 1.5 引入的泛型(generics)是一项强大的特性,旨在提升代码的类型安全性、可读性和减少类型转换的繁琐。泛型允许开发者在编程时为类、接口以及方法定义一种或多种类型参数,使得这些类型能够根据实际使用的...