29、Java高级特性——集合:集合框架、Collections类、Comparable接口、重写compareTo方法、泛型

目录

一、Java集合框架

二、Collections类 

1、Collections类中的常用静态方法

2、代码演示

三、Comparable接口

1、Comparable接口的作用

2、comparTo方法

3、重写comparTo方法 

四、泛型 

1、泛型的作用

2、泛型的定义

3、泛型在集合中的应用 

4、代码修改 

4.1  属性类

4.2  ArrayList集合

4.3  LinkedList集合

4.4  HashSet集合

4.5  HashMap集合

4.6  Collections类的方法

4.7  重写Comparable接口中的compareTo()方法

5、深入泛型 

5.1  定义泛型类

5.2  定义泛型接口

5.3  定义泛型方法 

5.4  多个参数的泛型类

5.5  从泛型类派生子类

五、集合框架和泛型总结 


一、Java集合框架

Java集合框架提供了一套性能优良、使用方便的接口和类,它们都位于java.util包中;

Java集合类主要由Collection接口和Map接口派生而来,其中Collection接口有两个常用的子接口,即List接口和Set接口,所以通常说Java集合框架由三大类接口组成(List接口、Set接口、Map接口)

Collections类是包含在Collection上的多态算法。

二、Collections类 

--->Java集合框架将针对不同数据结构算法的实现都保存在工具类中

--->Collections类定义了一系列用于操作集合的静态方法

--->Collections类完全由在 collection 上进行操作或返回 collection 的静态方法组成。它包含在 collection 上操作的多态算法,即“包装器”,包装器返回由指定 collection 支持的新 collection,以及少数其他内容。

--->Collections类是Java提供的一个集合操作工具类。它包含了大量的静态方法,用于实现对集合元素的排序、查找和替换等操作。

--->注意Collections和Collection的区别,Collections是操作类,Collection是集合接口

1、Collections类中的常用静态方法

静态方法可以通过类名直接调用。

--->使用binarySearch()方法可以查找集合中的元素,但是在使用binarySearch()方法之前需要使用sort()方法对集合进行排序,否则不能保证查找结果的正确性。 查找集合中不存在的元素,会返回一个负值

2、代码演示

import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;

public class CollectionsDemo01 {
	public static void main(String[] args) {
		//创建个LinkedList集合
		LinkedList list = new LinkedList();
		//向集合中存入数据
		list.add("def");
		list.add("opqrst");
		list.add("jkl");
		list.add("abc");
		list.add("ghi");
		list.add("mn");
		
		//遍历集合list
		//迭代器遍历
		Iterator it = list.iterator();
		while(it.hasNext()){
			Object object = it.next();
			String cd = (String)object;
			System.out.println(cd);
		}
		System.out.println("************************************************************");
		
		//将集合中的额元素进行排序
		Collections.sort(list);
		//增强for循环遍历集合
		for(Object object2:list){
			String cd2 = (String)object2;
			System.out.println(cd2);
		}
		System.out.println("************************************************************");
		
		//查找集合中的元素
		int index1 = Collections.binarySearch(list, "def");
		System.out.println("你要查找的集合元素的下标为:"+index1);
		int index2 = Collections.binarySearch(list, "ooo");
		System.out.println("你要查找的集合元素的下标为:"+index2);
		System.out.println("************************************************************");
		
		//查找集合中的最小值
		String minStr = Collections.min(list);
		System.out.println("集合中的最小元素为:"+minStr);
		System.out.println("************************************************************");
		
		//查找集合中的最大元素值
		String maxStr = Collections.max(list);
		System.out.println("集合中最大元素值为:"+maxStr);
		System.out.println("************************************************************");
		
		//将集合中的所有元素替换为ikun
		Collections.fill(list, "ikun");
		//遍历集合
		for(Object object3:list){
			String str3 = (String)object3;
			System.out.println(str3);
		}
		
		
	}

}

三、Comparable<T>接口

先看一个案例

创建Student类

public class Student {
	private String name;
	private char sex;
	private int age;
	
	//无参构造
	public Student(){
		
	}

	//有参构造
	public Student(String name, char sex, int age) {
		super();
		this.name = name;
		this.sex = sex;
		this.age = age;
	}

	//get/set方法
	public String getName() {
		return name;
	}

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

	public char getSex() {
		return sex;
	}

	public void setSex(char sex) {
		this.sex = sex;
	}

	public int getAge() {
		return age;
	}

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

	//toString方法
	@Override
	public String toString() {
		return "Student [name=" + name + ", sex=" + sex + ", age=" + age + "]";
	}
	
}

创建测试类CollectionsStudent

这里报错了,代码是正确的,我们上面一个案例就没有报错。因为上一个案例,排序依据是字符串,而这个案例集合中存储的是对象,没有设置排序依据,我们可以通过Comparable接口中的方法设置排序依据。

1、Comparable<T>接口的作用

这个接口的能力就是进行自然排序

此接口强行对实现它的每个类的对象进行整体排序。这种排序被称为类的自然排序,类的 compareTo 方法被称为它的自然比较方法

--->大部分类都实现了Comparable<T>接口。

2、compareTo方法

int compareTo():

比较此对象与指定对象的顺序。如果该对象小于、等于或大于指定对象,则分别返回负整数、零或正整数。

参数:

o - 要比较的对象。

返回:

负整数、零或正整数,根据此对象是小于、等于还是大于指定对象。

3、重写compareTo方法 

首先要实现Comparable接口,然后在重写CompareTo方法,排序的规则在重写的CompareTo方法体内编辑

public class Student implements Comparable<Student>{
	private String name;
	private char sex;
	private int age;
	
	//无参构造
	public Student(){
		
	}

	//有参构造
	public Student(String name, char sex, int age) {
		super();
		this.name = name;
		this.sex = sex;
		this.age = age;
	}

	//get/set方法
	public String getName() {
		return name;
	}

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

	public char getSex() {
		return sex;
	}

	public void setSex(char sex) {
		this.sex = sex;
	}

	public int getAge() {
		return age;
	}

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

	//toString方法
	@Override
	public String toString() {
		return "Student [name=" + name + ", sex=" + sex + ", age=" + age + "]";
	}

	@Override
	public int compareTo(Student stu) {
		if(this.getAge()>stu.getAge()){
			return 1;
		}else if(this.getAge()==stu.getAge()){
			return 0;
		}else if(this.getAge()<stu.getAge()){
			return -1;
		}
		return 0;
	}
	
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;

public class CollectionsStudent {
	public static void main(String[] args) {
		//创建五个学生对象
		Student stu1 = new Student("张三",'男',42);
		Student stu2 = new Student("李四",'男',18);
		Student stu3 = new Student("Lisa",'女',20);
		Student stu4 = new Student("Helen",'男',25);
		Student stu5 = new Student("王二",'男',50);
		
		//创建ArrayList集合,将学生对象添加到集合中
		ArrayList list = new ArrayList();
		list.add(stu1);
		list.add(stu2);
		list.add(stu3);
		list.add(stu4);
		list.add(stu5);
		
		//迭代器遍历集合
		Iterator it = list.iterator();
		while(it.hasNext()){
			Object object1 = it.next();
			Student student1 = (Student)object1;
			System.out.println(student1);
		}
		System.out.println("*********************************************");
		
		//对集合进行排序
		Collections.sort(list);
		//遍历数组
		for(Object object2:list){
			Student student2 = (Student)object2;
			System.out.println(student2);
		}
	}

}

四、泛型 

泛型这个型是类型的型。

我们已经学习到这个地步,一般我们用集合来存储的集合元素是对象,而对象的类型是我们的属性类。所以本质上集合和数组一样,存储的都是同一类型的元素。

在不使用泛型时,集合中的元素会自动转型为Object类,我们在操作集合时,要进行繁琐的强制类型转换,而强制类型转换是非常容易发生类型转换异常的,为了解决这个问题,也为了我们少费点脑子,也为了减少我们脑细胞的死亡,就有了泛型这个东西,使用了泛型以后,就不需要强制类型转换了。

1、泛型的作用

(1)泛型是JDK1.5的新特性,泛型的本质是参数化类型,也就是说所操作的数据类型被指定为一个参数,使代码可以应用于多种类型。

(2)Java语言引入泛型的好处是安全简单,且所有强制转换都是自动和隐式进行的,提高了代码的重用率。

好处就是我们再也不用进行类型转换了,前面我们写的代码进行了很多次的类型转换,搞不好还出错,以后不会了,那些在集合代码中写强制类型转换的日子一去不复返了。至于为什么一开始不用泛型:因为要先了解各种集合,他们可以这么写,再说一次讲的太多会懵圈的,大家还是先把前面梳理顺畅了吧,不要像一口吃个大胖子,会找不到对象的。,要一顿饭一顿饭的吃,消化完再吃 

2、泛型的定义

(1)将对象的类型作为参数,指定到其他类或者方法上,从而保证类型转换的安全性和稳定性。这就是泛型。泛型的本质就是参数化类型。

(2)语法格式:

类1或者接口<类型实参> 对象 = new 类2<类型实参>();

--->“类2”可以是“类1”本身,可以是“类1”的子类,还可以是接口的实现类;

--->“类2”的类型实参必须和“类1”中的类型实参相同.

例如:我们前面常用的Student类:

ArrayList<Student> list = new ArrayList<Student>();

创建一个ArrayList类的集合,集合中存储的元素必须是Student类。

定义了泛型以后,集合就只能存储泛型规定的类 

3、泛型在集合中的应用 

(1)List和ArrayList的泛型形式是List<E>和ArrayList<E>,ArrayList<E>与ArrayList类的常用方法基本一样;

(2)Map与HashMap也有它们的泛型形式,即Map<K,V>和HashMap<K,V>,因为它们的每一个元素都包含两个部分,及key和value,所以,在应用泛型时,要同时指定key的类型和value的类型,K表示key的类型,V表示value的类型。HashMap<K,V>操作数据的方法与HashMap基本一样;

(3)泛型使集合的使用更方便,也提升了安全:

--->存储数据时进行严格类型检查,确保只有合适类型的对象才能存储在集合中。

--->从集合中检索对象时,减少了强制类型转换。

主要是会用就行,定义啥的把一些简单明了的话变成了一堆谁也看不懂的文字,大家只要能仿照我下面的代码,会用泛型就行。

4、代码修改 

带有强制类型转换的代码都可以修改

4.1  属性类

public class Student implements Comparable<Student>{
	private String name;
	private char sex;
	private int age;
	
	//无参构造
	public Student(){
		
	}

	//有参构造
	public Student(String name, char sex, int age) {
		super();
		this.name = name;
		this.sex = sex;
		this.age = age;
	}

	//get/set方法
	public String getName() {
		return name;
	}

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

	public char getSex() {
		return sex;
	}

	public void setSex(char sex) {
		this.sex = sex;
	}

	public int getAge() {
		return age;
	}

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

	//toString方法
	@Override
	public String toString() {
		return "Student [name=" + name + ", sex=" + sex + ", age=" + age + "]";
	}

	@Override
	public int compareTo(Student stu) {
		if(this.getAge()>stu.getAge()){
			return 1;
		}else if(this.getAge()==stu.getAge()){
			return 0;
		}else if(this.getAge()<stu.getAge()){
			return -1;
		}
		return 0;
	}
	
}

4.2  ArrayList集合

import java.util.ArrayList;
import java.util.Iterator;

public class ArraysListDemo01 {
	public static void main(String[] args) {
		//创建两个NewsTitle类对象
		NewsTitle nt01 = new NewsTitle(1001,"我的很大吴亦凡!","张三");
		NewsTitle nt02 = new NewsTitle(1002,"一次六千李云迪!","李四");
		
		//使用ArrayList类创建一个集合容器
		ArrayList<NewsTitle> al = new ArrayList<NewsTitle>();
		
		//使用ArrayList类中的add()方法,将数据存入到集合中
		al.add(nt01);
		al.add(nt02);
		al.add(nt01);//ArraysList是一个有序的不唯一的集合(里面的元素可以重复)
		System.out.println(al);
		System.out.println("*****************************************************************************");
		
		//将集合存储到集合中的指定位置add(int index,Object object)
		NewsTitle nt03 = new NewsTitle(1003,"死不承认李易峰!","王二");
		al.add(1, nt03);
		al.add(nt03);
		System.out.println(al);
		System.out.println("*****************************************************************************");
		
		
		//通过集合对象名al调用方法,对集合元素进行操作
		//获取元素个数
		int size = al.size();
		System.out.println("集合al的元素个数为:"+size+"个。");
		System.out.println("*****************************************************************************");
		
		//获取集合中指定下标的元素
		//获取下标为0的集合元素
		NewsTitle object01 = al.get(0);
		System.out.println(object01);//当你直接输出对象名的时候输出的是地址值,重写了toString方法,输出的是toString方法的内容
		System.out.println("*****************************************************************************");
		
		//遍历集合中的元素
		for(int i = 0;i < al.size();i++){
			NewsTitle forNt = al.get(i);
			System.out.println(forNt);
		}
		System.out.println("*****************************************************************************");
		
		
		//判断集合中是否包含某个元素
		boolean result = al.contains(nt02);
		System.out.println("集合中是否包含nt02这个元素?"+result);
		System.out.println("*****************************************************************************");
		
		//删除集合中的元素:ramove(元素名)/remove(元素下标)
		boolean result01 = al.remove(nt02);
		System.out.println("元素nt02是否删除成功:"+result01);
		
		NewsTitle result02 = al.remove(0);
		System.out.println("删除的元素:"+result02);
		System.out.println("删除后的集合:"+al);
		System.out.println("*****************************************************************************");
		
		//普通for循环遍历集合
		for(int i = 0;i < al.size();i++){
			NewsTitle object = al.get(i);
			System.out.println(object);
		}
		System.out.println("*****************************************************************************");
		
		//使用增强for循环遍历数组
		//没有使用泛型的时候,集合里面所有的元素都会向上转型为Object类
		for(NewsTitle object:al){
			System.out.println(object);
		}
		System.out.println("*****************************************************************************");
		
		//删除元素后使用迭代器遍历集合:iterator()
		Iterator<NewsTitle> it = al.iterator();
		//从it容器中取出元素
		while(it.hasNext()){
			NewsTitle newst = it.next();
			System.out.println(newst);
		}
		System.out.println("*****************************************************************************");
		
		//判断集合是否为空:isEmpty
		boolean result00 = al.isEmpty();
		System.out.println("集合al是否为空:"+result00);
		System.out.println("*****************************************************************************");
		
		//将集合转换为数组:toArray
		Object[] objec = al.toArray();
		//遍历数组
		for(int i = 0;i < objec.length;i++){
			System.out.println(objec[i]);
		}
		System.out.println("*****************************************************************************");
		
		System.out.println("集合的方法还能用:"+al.get(0));
		System.out.println("*****************************************************************************");
		
		//清空集合:clear()
		al.clear();
		System.out.println(al.size());
		System.out.println("集合是否为空:"+al.isEmpty());
	}
	
}

4.3  LinkedList集合

import java.util.LinkedList;
import java.util.List;

public class LinkedListDemo01 {
	public static void main(String[] args) {
		//创建三个NewsTitle类对象
		NewsTitle nt01 = new NewsTitle(1001,"我的很大吴亦凡!","张三");
		NewsTitle nt02 = new NewsTitle(1002,"一次六千李云迪!","李四");
		NewsTitle nt03 = new NewsTitle(1003,"死不承认李易峰!","王二");
		
		//创建集合容器
		List<NewsTitle> list = new LinkedList<NewsTitle>();
		
		//添加数据
		list.add(nt01);
		list.add(nt02);
		list.add(nt03);
		
		//将nt03存储到集合第一个位置
		//接口的引用,无法使用实现类中特有的方法,需要向下转型
		//list.addFirst(nt03);
		LinkedList<NewsTitle> linkedList = (LinkedList<NewsTitle>)list;
		linkedList.addFirst(nt03);
		
		//将nt01添加到集合尾部
		linkedList.addLast(nt01);
		
		//遍历集合
		for(NewsTitle nt:list){
			//类型转换
			System.out.println(nt);
		}
		System.out.println("*******************************************************************");
		
		//返回列表中的第一个元素和最后一个元素
		NewsTitle news01 = linkedList.getFirst();
		System.out.println("返回首个元素:"+news01);
		
		NewsTitle news02 = linkedList.getLast();
		System.out.println("返回末尾元素:"+news02);
		System.out.println("*******************************************************************");
		
		//删除并返回集合中的第一个元素和最后一个元素:removeFirst()/removeLast()
		NewsTitle obj03 = linkedList.removeFirst();
		System.out.println("删除并返回集合中的第一个元素:"+obj03);
		
		System.out.println("删除并返回集合中的最后一个元素:"+linkedList.removeLast());
		System.out.println("*******************************************************************");
		
		for(int i = 0;i < list.size();i++){
			NewsTitle newst = list.get(i);
			System.out.println(newst);
			
		}
		
	}

}

4.4  HashSet集合

import java.util.HashSet;
import java.util.Iterator;

public class HashSetDemo01 {
	public static void main(String[] args) {
		//创建三个NewsTitle类对象
		NewsTitle nt01 = new NewsTitle(1001,"我的很大吴亦凡!","张三");
		NewsTitle nt02 = new NewsTitle(1002,"一次六千李云迪!","李四");
		NewsTitle nt03 = new NewsTitle(1003,"死不承认李易峰!","王二");
		
		//创建集合容器
		HashSet<NewsTitle> hs = new HashSet<NewsTitle>();
		
		//添加数据:add()
		hs.add(nt01);
		hs.add(nt02);
		hs.add(nt03);
		
		//集合中元素个数:size()
		int size = hs.size();
		System.out.println("Set集合中元素的数量为:"+size);
		System.out.println("***************************************************************");
		
		//增强for循环遍历数组
		for(NewsTitle newsTitle:hs){
			System.out.println(newsTitle);
		}
		System.out.println("***************************************************************");
		
		//迭代器遍历数组
		//将集合元素取出来放到迭代器容器中
		Iterator<NewsTitle> it = hs.iterator();
		//使用hashNext()方法判断迭代器是否有元素
		while(it.hasNext()){
			//使用next()方法取出元素
			NewsTitle nt = it.next();
			System.out.println(nt);
		}
	}

}

4.5  HashMap集合

public class Student implements Comparable<Student>{
	private String name;
	private char sex;
	private int age;
	
	//无参构造
	public Student(){
		
	}

	//有参构造
	public Student(String name, char sex, int age) {
		super();
		this.name = name;
		this.sex = sex;
		this.age = age;
	}

	//get/set方法
	public String getName() {
		return name;
	}

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

	public char getSex() {
		return sex;
	}

	public void setSex(char sex) {
		this.sex = sex;
	}

	public int getAge() {
		return age;
	}

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

	//toString方法
	@Override
	public String toString() {
		return "Student [name=" + name + ", sex=" + sex + ", age=" + age + "]";
	}

	@Override
	public int compareTo(Student stu) {
		if(this.getAge()>stu.getAge()){
			return 1;
		}else if(this.getAge()==stu.getAge()){
			return 0;
		}else if(this.getAge()<stu.getAge()){
			return -1;
		}
		return 0;
	}
	
}
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class HashMapDemo01 {
	public static void main(String[] args) {
		//准备三个Student类对象
		Student stu01 = new Student("张三",'男',20);
		Student stu02 = new Student("李四",'男',30);
		Student stu03 = new Student("Lisa",'女',18);
		
		//创建Map集合容器(多态)
		Map<String,Student> map = new HashMap<String,Student>();
		
		//添加元素
		map.put("zs", stu01);
		map.put("ls", stu02);
		map.put("lisa", stu03);
		
		//获取集合中的元素个数
		int size = map.size();
		System.out.println("Map集合长度:"+size);
		System.out.println("***********************************************************");
		
		//根据键(key)获取键对应的值:get(Object key)
		Student student01 = map.get("zs");
		System.out.println(student01);
		System.out.println("***********************************************************");
		
		//根据键删除键值对
		Student student02 = map.remove("ls");
		System.out.println("你删除的元素为:"+student02);
		System.out.println("Map集合长度:"+map.size());
		System.out.println("***********************************************************");
		
		//判断集合中是否包含键:containsKey(Object key)
		boolean result01 = map.containsKey("zs");
		System.out.println("集合中包含键“zs”?"+result01);
		System.out.println("集合中包含键“mz”?"+map.containsKey("mz"));
		
		//判断集合中是否包含值:containsValue(Object value)
		System.out.println("集合中包含值stu01?"+map.containsValue(stu01));
		System.out.println("***********************************************************");
		
		//获取集合中的所有键:keySet()
		Set<String> keys = map.keySet();
		for(String key:keys){
			//我们设置的键是String类型
			System.out.println(key);
		}
		System.out.println("***********************************************************");
		
		//获取集合中的所有值:values()
		Collection<Student> value = map.values();
		Iterator<Student> it = value.iterator();
		while(it.hasNext()){
			Student stud = it.next();
			System.out.println(stud);
		}
		System.out.println("***********************************************************");
		
		//通过键遍历Map集合
		//通过键的集合,然后通过键来找值
		Set<String> keyset = map.keySet();
		
		//增强for循环
		for(String strKey:keyset){
			Student student1 = map.get(strKey);
			System.out.println(strKey+"对应的学生:"+student1);
		}
		System.out.println("***********************************************************");
		
		//迭代器遍历
		Iterator<String> it01 = keyset.iterator();
		while(it01.hasNext()){
			String strkey = it01.next();
			Student student2 = map.get(strkey);
			System.out.println(strkey+"对应的学生:"+student2);
		}
		System.out.println("***********************************************************");
		
		//Map.Entry接口遍历集合
		Set<Entry<String, Student>> keyValues = map.entrySet();
		for(Map.Entry<String,Student> me:keyValues){
			//获取键
			String stringkey = me.getKey();
			//获取值
			Student student3 = me.getValue();
			System.out.println(stringkey+"对应的学生:"+student3);
		}
	}

}

4.6  Collections类的方法

import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;

public class CollectionsDemo01 {
	public static void main(String[] args) {
		//创建个LinkedList集合
		LinkedList<String> list = new LinkedList<String>();
		//向集合中存入数据
		list.add("def");
		list.add("opqrst");
		list.add("jkl");
		list.add("abc");
		list.add("ghi");
		list.add("mn");
		
		//遍历集合list
		//迭代器遍历
		Iterator<String> it = list.iterator();
		while(it.hasNext()){
			String cd = it.next();
			System.out.println(cd);
		}
		System.out.println("************************************************************");
		
		//将集合中的元素进行排序
		Collections.sort(list);
		//增强for循环遍历集合
		for(String cd2:list){
			System.out.println(cd2);
		}
		System.out.println("************************************************************");
		
		//查找集合中的元素
		int index1 = Collections.binarySearch(list, "def");
		System.out.println("你要查找的集合元素的下标为:"+index1);
		int index2 = Collections.binarySearch(list, "ooo");
		System.out.println("你要查找的集合元素的下标为:"+index2);
		System.out.println("************************************************************");
		
		//查找集合中的最小值
		String minStr = Collections.min(list);
		System.out.println("集合中的最小元素为:"+minStr);
		System.out.println("************************************************************");
		
		//查找集合中的最大元素值
		String maxStr = Collections.max(list);
		System.out.println("集合中最大元素值为:"+maxStr);
		System.out.println("************************************************************");
		
		//将集合中的所有元素替换为ikun
		Collections.fill(list, "ikun");
		//遍历集合
		for(String str3:list){
			System.out.println(str3);
		}
		
		
	}

}

4.7  重写Comparable<T>接口中的compareTo()方法

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;

public class CollectionsStudent {
	public static void main(String[] args) {
		//创建五个学生对象
		Student stu1 = new Student("张三",'男',42);
		Student stu2 = new Student("李四",'男',18);
		Student stu3 = new Student("Lisa",'女',20);
		Student stu4 = new Student("Helen",'男',25);
		Student stu5 = new Student("王二",'男',50);
		
		//创建ArrayList集合,将学生对象添加到集合中
		ArrayList<Student> list = new ArrayList<Student>();
		list.add(stu1);
		list.add(stu2);
		list.add(stu3);
		list.add(stu4);
		list.add(stu5);
		
		//迭代器遍历集合
		Iterator<Student> it = list.iterator();
		while(it.hasNext()){
			Student student1 = it.next();
			System.out.println(student1);
		}
		System.out.println("*********************************************");
		
		//对集合进行排序
		Collections.sort(list);
		//遍历数组
		for(Student student2:list){
			System.out.println(student2);
		}
	}

}

5、深入泛型 

随便了解一下就行

(1)在集合中使用泛型只是泛型多种应用的一种,在接口、类、方法等方面也有着泛型的广泛应用。

(2)定义泛型接口或类的过程,与定义一个接口或者类相似。

5.1  定义泛型类

概念:泛型类简单地说就是具有一个或者多个类型参数的类。

语法:
                                    访问修饰符  class  类名<TypeList>

--->TypeList表示类型参数列表,每个类型变量之间以逗号分隔。

5.2  定义泛型接口

概念:泛型接口就是拥有一个或者多个类型参数的接口。泛型接口的定义方式与定义泛型类类似。

语法格式:

访问修饰符 interface 接口名<TypeList> 

泛型类实现泛型接口:
访问修饰符 class 类名<TypeList> implements 接口名<TypeList>

5.3  定义泛型方法 

 概述:一些方法常常需要对某一类型数据进行处理,若处理的数据乐行不确定,则可以通过泛型方法的方式来定义,达到简化代码、提高代码重用性的目的。

 泛型方法实际上就是带有类型参数的方法。

 定义泛型方法与方法所在的类、或者接口是否是泛型类或者泛型接口没有直接的关系,也就是说无论是泛型类还是非泛型类,如果需要就可以定义泛型方法。

语法格式:

访问修饰符 <类型参数> 返回值 方法名(类型参数列表)

--->TypeList表示由逗号分隔的一个或多个类型参数列表。

类型变量放置在访问修饰符与返回值之间

5.4  多个参数的泛型类

(1)泛型类的类型参数可以有一个或者多个。

(2) 比如HashMap<K,V>就有两个类型参数,一个指定key的类型,一个指定value的类型。

5.5  从泛型类派生子类

(1)面向对象的特征同样适用于泛型类,所以泛型类也可以继承。

(2)继承了反省类的子类,必须也是泛型类。

 (3)继承泛型类的语法格式:class 子类<T> extends 父类<T>{}

五、集合框架和泛型总结 

1、集合弥补了数组的缺陷,它比数组更灵活实用,而且不同的集合适用于不同场合;

2、Java集合框架共有3大类接口,即Map接口、List接口和Set接口;

3、List集合可以存储一组重复的元素;Set集合中的额元素不能重复;Map集合存储的是一组兼职对象的元素,键不可以重复,值可以重复;

4、ArrayList和数组采用相同的存储方式,它的特点是长度可变且可以存储任何类型的数据,它的优点在于遍历元素和随机访问元素的效率较高;

5、LinkedList类采用链表存储方式,优点在于插入、删除元素时效率较高;

6、Iterator为集合而生,专门实现集合的遍历,它隐藏了各种集合实现类的内部细节,提供了遍历集合的统一编程接口;

7、 HashMap类是最常用的Map实现类,它的特点是存储键值对数据,优点是查询指定元素效率高;

8、泛型的本质是参数化类型,也就是说所操作的数据类型被指定为一个参数,使代码可以应用于多种数据类型;

9、使用泛型集合在创建集合对象时指定集合中元素的类型,从集合中取出元素时无需进行强制类型转换;

10、在集合中使用泛型只是泛型多种应用的一种,在接口、类、方法等方面也有着泛型的广泛应用;

11、如果数据类型不确定,可以通过泛型方法的方式,达到简化代码、提高代码重用性的目的

这篇博文竟然都快达到两万字了,不过学习的内容并不多,定义和代码一大堆,没必要去背定义,知道怎么用就行了; 代码都是以前学过的,大部分都是重复的,把以前的代码拿过来使用泛型;不过大家不要偷懒,不要在原有的代码上修改,重新建个包,哪怕复制粘贴也行,万一哪天想不起来了还能看看。今天的学习到此结束,我是一米八、有腹肌、低于半小时你报警的Loveletter,觉得内容不错的小伙伴可以点点关注,我们下篇博文见,再见!

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值