【DataStructure】The description of generic collections
来源:互联网 发布:征途电子狗的地图软件 编辑:程序博客网 时间:2024/05/02 01:44
In this blog, generic collections will be talked about in details. In the past bacause of shortage of generic argument, less importance has been attached to the this module. Just now after reading the chapter about this knowledge, I gradually realized that they are so useful in the pactice, especially desgin the system achitect. Now make a summary about generic collections.
First, use the type argument
package com.albertshao.ds.generic;public class EnumArgument {enum Week {Mon, Tus, Wed, Thu, Fri, Sta, Sun}public static void main(String args[]){Pair<Integer, Week> pairs = new Pair<Integer, Week>(2, Week.Mon);System.out.println(pairs);}}class Pair<S, T> {private S first;private T second;public Pair(S first, T second){this.first = first;this.second = second;}public S getFirst() {return first;}public void setFirst(S first) {this.first = first;}public T getSecond() {return second;}public void setSecond(T second) {this.second = second;}public String toString(){return "("+first + "," + second+")";}}/** * output:(2,Mon) */Second, use the generic methods
In addition to generic types, type parameters can also be used to define generic methods, identified by the generic parameter specifier<T> placed in front of the return type.
package com.albertshao.ds.generic;public class TestPrint {public static void main(String[] args) {String[] weeks = new String[] { "Mon", "Tus", "Wed" };print(weeks);}static <E> void print(E[] a) {for (E e : a) {System.out.println(e);}}}/* * output: * Mon * Tus * Wed * */
Third , use generic wildcards
The symbol ?can be used as a wildcard, in place of a generic variable. It stands for “unknown type,” and is called the wildcard type.
package com.albertshao.ds.generic;import java.util.Arrays;import java.util.Collection;import java.util.List;public class TestWildCards {public static void main(String[] args) {List<String> strList = Arrays.asList("Fri", "Sat", "Sun");print(strList);}static void print(Collection<?> c) {for (Object o : c) {System.out.printf("%s ", o);}System.out.println();}}// output: Fri Sat Sun
- 【DataStructure】The description of generic collections
- 【DataStructure】The description of Java Collections Framework
- 【DataStructure】The description and usage of Stack
- 【DataStructure】Description and usage of queue
- 【DataStructure】Description and Introduction of Tree
- linux kernel中的generic datastructure
- 【DataStructure】Charming usage of Set in the java
- Full Description of the compilation of Scilab
- 【RefactoringCode】The description of the refactoring book
- Resolving the updating problem of image-generic.
- A Description of the Guest Account
- A Brief description of the FTP protocol
- A brief description of the FTP protocol
- The Basical Description Of CWDM OADM
- 【MongoDB】The description of index(一)
- 【MongoDB】The description of procedure in MongoDB
- Detailed description of the 24 Autocord models
- System.Collections.Generic
- openeim 需要提前安排好下次迭代开发的故事
- BOJ 397. Prime Judge
- LightOJ 1231 1232
- ORADIM工具介绍
- 黑马程序员----JavaBean、内省 和 注释
- 【DataStructure】The description of generic collections
- Treap树结构
- 尝试运行项目时出错:无法启动程序
- 潇雨菲怔怔睁着眼前之人
- “你居然在这街上闲逛,
- “大人,请离近一看!”
- “三王爷,要不一起吧!
- “小姐,快喊李大哥!”
- 书香屋 更新时间: