泛型固定下边界 & 泛型固定上边界 ? super E & ? extends E

泛型固定下边界 & 泛型固定上边界 ? super E & ? extends E

TreeSet构造方法

image

TreeMap构造方法

image

   package com.heima.collections;
    import java.util.ArrayList;
    import java.util.Comparator;
    import java.util.TreeSet;
    import com.heima.bean.BaseStudent;
    import com.heima.bean.Student;
    public class Demo2_Genric {
    
    	/**
    	 * 泛型固定下边界
    	 * ? super E  
    	 * 
    	 * 泛型固定上边界
    	 * ? extends E
    	 */
    	public static void main(String[] args) {
    		demo1();
    		//demo2();
    	}

1.泛型固定上边界 ? extends E

  • 多态子类引用了父类方法,这个好理解相当于 Student arr = new BaseStudent( );
    image
    	public static void demo1() {
    		ArrayList<Student> list1 = new ArrayList<>();
    		list1.add(new Student("张三", 23));
    		list1.add(new Student("李四", 24));
    		
    		ArrayList<BaseStudent> list2 = new ArrayList<>();
    		list2.add(new BaseStudent("王五", 25));
    		list2.add(new BaseStudent("赵六", 26));
    		
    		list1.addAll(list2);
    		System.out.println(list1);
    		System.out.println(list2);
    	}
[Student [name=张三, age=23], Student [name=李四, age=24], Student [name=王五, age=25], Student [name=赵六, age=26]]
[Student [name=王五, age=25], Student [name=赵六, age=26]]

2.泛型固定下边界 * ? super E

  • 通过TreeSet & TreeMap 中的 构造 new Comparator 匿名对象 中的 重写compare(Student s1, Student s2) 方法进行比较
    image
  	private static void demo2() {
    		TreeSet<Student> ts1 = new TreeSet<>(new CompareByAge());
    		ts1.add(new Student("张三", 33));
    		ts1.add(new Student("李四", 13));
    		ts1.add(new Student("王五", 23));
    		ts1.add(new Student("赵六", 43));
    		
    		TreeSet<BaseStudent> ts2 = new TreeSet<>(new CompareByAge());
    		ts2.add(new BaseStudent("张三", 33));
    		ts2.add(new BaseStudent("李四", 13));
    		ts2.add(new BaseStudent("王五", 23));
    		ts2.add(new BaseStudent("赵六", 43));
    		ts2.add(new BaseStudent("小五", 23));
    		
    		System.out.println(ts2);
    		System.out.println(ts1);
    	}
    }
    
    class CompareByAge implements Comparator<Student> {
    
    	@Override
    	public int compare(Student s1, Student s2) {
    		int num = s1.getAge() - s2.getAge();
    		return num == 0 ? s1.getName().compareTo(s2.getName()) :  num;
    	}
    	
    }
[Student [name=李四, age=13], Student [name=小五, age=23], Student [name=王五, age=23], Student [name=张三, age=33], Student [name=赵六, age=43]]
[Student [name=李四, age=13], Student [name=王五, age=23], Student [name=张三, age=33], Student [name=赵六, age=43]]

1.父类Student

public class Student implements Comparable<Student> {
	private String name;
	private int age;
	public Student() {                   //Alt +Shift + s + c
		super();
		
	}
	public Student(String name, int age) {//Alt +Shift + s + o
		super();
		this.name = name;
		this.age = age;
	}
	
	public String getName() {              //Alt +Shift + s + + a + r 
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	
	@Override
	public String toString() {           //Alt +Shift + s + s + g 
		return "Student [name=" + name + ", age=" + age + "]";
	}
	
	
	
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + age;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Student other = (Student) obj;
		if (age != other.age)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}
	@Override
	public int compareTo(Student o) {
		int num = this.age - o.age;					//以年龄为主要条件
		return num == 0 ? this.name.compareTo(o.name) : num;
	}
}

2.子类BaseStudent

package com.heima.bean;

public class BaseStudent extends Student {

	public BaseStudent() {
	}

	public BaseStudent(String name, int age) {
		super(name, age);

	}

}

  • 欢迎关注微博:http://weibo.com/jankinWang 获取更多笔记动态图片
  • 欢迎加入QQ群:724100835 讨论交流,下载学习资料
  • 欢迎转载,订阅关注哦!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值