java基础(6)-集合类2

浏览: 986

泛型:是一种把类型明确的工作推迟到创建对象或者调用方法的时候才去明确的特殊的类型,参数化类型,把类型当做参数一样的传递

好处:
1)把运行时期的问题提前到了编译器期间
2)避免了强制类型转换
3)优化了程序设计,解决了黄色警告线

例1:用ArrayList存储字符串元素,并遍历,用泛型改进

import java.util.ArrayList;
import java.util.Iterator;
public class ArrayListDemo1{
public static void main(String[] args){
ArrayList<String> a1=new ArrayList<String>();
a1.add("hadoop");
a1.add("spark");
a1.add("storm");

Iterator<String> it = a1.iterator();
while(it.hasNext()){
String s = it.next();
System.out.println(s);
}

System.out.println("----------------for--------------------");
for(int i=0;i String s = a1.get(i);
System.out.println(s);
}
}
}

--------------------------------------------------------
输出结果
hadoop
spark
storm
---------------for----------------------------
hadoop
spark
storm
---------------------------------------------

例2:用ArrayList存储自定义对象,并遍历,用泛型改进

import java.util.ArrayList;
import java.util.Iterator;
public class ArrayListDemo2{
public static void main(String[] args){
ArrayList a1 = new ArrayList();

Student s1 = new Student("wu",24);
Student s2 = new Student("sun",25);
Student s3 = new Student("xu",26);

a1.add(s1);
a1.add(s2);
a1.add(s3);

Iterator it=a1.iterator();
while(it.hasNext()){
Student s = it.next();
System.out.println(s);
}

System.out.println("---------for----------------------");
for(int i=0;i System.out.println(a1.get(i));
}
}
}

class Student{
private int age;
private String name;

//构造方法
public Student(){
super();
}

public Student(String name,int age){
this.name = name;
this.age = age;
}

public void setName(String name){
this.name = name;
}

public String getName(){
return name;
}

public void age(int age){
this.age = age;
}

public int age(){
return age;
}

public String toString(){
return "Student [name="+name+",age="+age+"]";
}
}

--------------------------------------------
输出结果
Student [name=wu,age=24]
Student [name=sun,age=25]
Student [name=xu,age=26]
---------for----------------------
Student [name=wu,age=24]
Student [name=sun,age=25]
Student [name=xu,age=26]


  • 泛型类

把泛型定义在类上
格式:public class 类名<泛型类型1,...>
注意:泛型类型必须是引用类型

public class ObjectToolDemo{
public static void main(String[] args){
/*
ObjectTool ot = new ObjectTool();
ot.setObj(new String("wujiadong"));
String s =(String) ot.getObj();
System.out.println(s);

ot.setObj(new Integer(30));
Integer i=(Integer) ot.getObj();
System.out.println(i);
*/


ObjectTool<String> ot1 = new ObjectTool<String>();
ot1.setObj(new String("wujiadong"));
String s = ot1.getObj();
System.out.println(s);

ObjectTool ot2 = new ObjectTool();
ot2.setObj(new Integer(2));
Integer i = ot2.getObj();
System.out.println(i);

}
}

/*
*泛型类:把泛型定义在类上
*/

class ObjectTool {
private T obj;

public T getObj(){
return obj;
}

public void setObj(T obj){
this.obj = obj;
}
}

-------------------------------------------
输出结果
wujiadong
2
----------------------------------------------
  • 泛型方法

把泛型定义在方法上
格式:public <泛型类型> 返回类型 方法名(泛型类型){}

前示例:

public class ObjectToolDemo1{
public static void main(String[] args){
/*
ObjectTool ot = new ObjectTool();
ot.show("wujiadong");
ot.show(10);
ot.show(true);
*/

ObjectTool<String> ot1 = new ObjectTool<String>();
ot1.show("wujiadong");

ObjectTool ot2 = new ObjectTool();
ot2.show(20);

ObjectTool<Boolean> ot3 = new ObjectTool<Boolean>();
//大写的Boolean,小写的boolean是基本数据类型,而这里只能是引用数据类型
ot3.show(true);

}
}

class ObjectTool{
/*
public void show(String s){
System.out.println(s);
}

public void show(Integer i){
System.out.println(i);
}

public void show(Boolean b){
System.out.println(b);
}
*/


public void show(T t){
System.out.println(t);
}
}

------------------------------------------

输出结果
wujiadong
20
true
-----------------------------------------

泛型方法示例

public class ObjectToolDemo1{
public static void main(String[] args){

ObjectTool ot = new ObjectTool();
ot.show("wujiadong");
ot.show(10);
ot.show(true);


/*
ObjectTool ot1 = new ObjectTool();
ot1.show("wujiadong");

ObjectTool ot2 = new ObjectTool();
ot2.show(20);

ObjectTool ot3 = new ObjectTool();
//大写的Boolean,小写的boolean是基本数据类型,而这里只能是引用数据类型
ot3.show(true);
*/

}
}

class ObjectTool{
/*
public void show(String s){
System.out.println(s);
}

public void show(Integer i){
System.out.println(i);
}

public void show(Boolean b){
System.out.println(b);
}
*/



public void show(T t){
System.out.println(t);
}
}

--------------------------------------
输出结果
wujiadong
10
true
---------------------------------------
  • 泛型接口

把泛型定义在接口上
格式:public interface 接口名<泛型类型1,...>

//接口
interface Inter<T>{
public abstract void show(T t);//接口中抽象方法无方法体

}

//实现接口
//实现类实现接口分两种情况:
//第一:已经知道是什么类型
//第二:不知奥是什么类型
class InterImp1 implements Inter<String>{
public void show(String t){
System.out.println(t);
}

}

public class InterDemo{
public static void main(String[] args){
//InterImp1 i = new InterImp1();
Inter i = new InterImp1();
i.show("wujiadong");

}
}
-------------------------------------------
输出结果
wujiadong
-------------------------------------------

第二种情况(常用)

//接口
interface Inter<T>{
public abstract void show(T t);//接口中抽象方法无方法体

}

//实现接口
//实现类实现接口分两种情况:
//第二:不知奥是什么类型
class InterImp1<T> implements Inter<T>{
public void show(T t){
System.out.println(t);
}

}

public class InterDemo{
public static void main(String[] args){
//InterImp1 i = new InterImp1();
Inter i = new InterImp1();
i.show("wujiadong");

Inter i1 = new InterImp1();
i1.show(10);

Inter i2 = new InterImp1();
i2.show(true);
}
}


----------------------------------------------
输出结果
wujiadong
10
true

泛型高级(通配符)

1)泛型通配符

  • 任意类型,如果没有明确,那么就是Object以及任意的java类了

    2)?extend E

  • 向下限定,E及其子类

3)?super E

  • 向上限定,E及其父类

举例说明

import java.util.Collection;
import java.util.ArrayList;
class Animal{

}

class Dog extends Animal{

}

class Cat extends Animal{

}

public class GenericDemo{
public static void main(String[] args){
//泛型如果明确的写的时候,前后必须一致
// Collection c = new ArrayList();错误的写法

//?表示任意的类型都是可以的
Collection c1 = new ArrayList<Object>();
Collection c2 = new ArrayList<Animal>();
Collection c3 = new ArrayList<Dog>();
Collection c4 = new ArrayList<Cat>();

//?extends E 向下限定,E及其子类
//Collection c5 = new ArrayList();报错
Collectionextends Animal> c5 = new ArrayList<Animal>();
Collectionextends Animal> c6 = new ArrayList<Dog>();
Collectionextends Animal> c7 = new ArrayList<Cat>();

//?super E 向上限定,E及其父类
Collectionsuper Animal> c8 = new ArrayList<Object>();
Collectionsuper Animal> c9 = new ArrayList<Animal>();
//Collection c10 = new ArrayList();
//Collection c11 = new ArrayList();
}
}
推荐 0
本文由 邬家栋 创作,采用 知识共享署名-相同方式共享 3.0 中国大陆许可协议 进行许可。
转载、引用前需联系作者,并署名作者且注明文章出处。
本站文章版权归原作者及原出处所有 。内容为作者个人观点, 并不代表本站赞同其观点和对其真实性负责。本站是一个个人学习交流的平台,并不用于任何商业目的,如果有任何问题,请及时联系我们,我们将根据著作权人的要求,立即更正或者删除有关内容。本站拥有对此声明的最终解释权。

0 个评论

要回复文章请先登录注册