关于Collections.binarySearch(List> list, T key)的疑惑

a1282379904 2017-09-01 10:42:40
加精
为什么该方法设计成这样:
<T> int binarySearch(List<? extends Comparable<? super T>> list, T key) // m1
而不是这样:
<T> int binarySearch(List<? extends Comparable<T>> list, T key) // m2
这两者有什么区别?希望有人能给个例子:怎样的参数能传给方法1而不能传给方法2?

...全文
3379 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
csdn987q 2017-09-27
  • 打赏
  • 举报
回复
谢谢分享 不错、111
xx41064203 2017-09-11
  • 打赏
  • 举报
回复
学习了.谢谢.
csdn987q 2017-09-05
  • 打赏
  • 举报
回复
感谢楼主分享
killxj2shit 2017-09-03
  • 打赏
  • 举报
回复
是的 泛型不能像对象那样多态
ljheee 2017-09-03
  • 打赏
  • 举报
回复
<? super T>的意思是T及T的父类
Freefish1994 2017-09-01
  • 打赏
  • 举报
回复
把你的代码改了一下 <? super T>的意思是T及T的父类 这里真正实现Comparab接口的类型是Phone 而你用<T extends Comparable<? super T>>这个情况下T类实现Comparab接口的类型必须是T这个类, 而你HuaWei类是继承Phone类的,实际实现的接口类型是Phone所以调用方法1时就编译不过了

public class Test {
	public static void main(String args[]) {
		List<Phone> phone = new ArrayList<Phone>();
		phone.add(new Phone(1000));
		phone.add(new Phone(2000));
		List<HuaWei> huaWei = new ArrayList<HuaWei>();
		huaWei.add(new HuaWei(3000));
		huaWei.add(new HuaWei(4000));
		binarySearch1(phone);// 编译通过
		binarySearch2(phone);// 编译通过
		
		/*
		 * Bound mismatch: The generic method binarySearch1(List<T>) of type Test 
		 * is not applicable for the arguments (List<HuaWei>). 
		 * The inferred type HuaWei is not a valid substitute for the bounded parameter <T extends Comparable<T>>
		 */
		binarySearch1(huaWei);// 编译不通过
		
		
		binarySearch2(huaWei);// 编译通过
	}
	
	public static <T extends Comparable<T>> void binarySearch1(List<T> list){
		Collections.sort(list);
	}
	
	public static <T extends Comparable<? super T>> void binarySearch2(List<T> list){
		Collections.sort(list);
	}
}
class Phone implements Comparable<Phone> {
	public int price;
	
	public Phone(int price) {
		super();
		this.price = price;
	}

	public int compareTo(Phone other) {
		return this.price - other.price;
	}
}

class HuaWei extends Phone{

	public HuaWei(int price) {
		super(price);
	}
	
}
a1282379904 2017-09-01
  • 打赏
  • 举报
回复


这样子是可以编译通过的
a1282379904 2017-09-01
  • 打赏
  • 举报
回复
引用 2 楼 qq_27762917 的回复:
把你的代码改了一下 <? super T>的意思是T及T的父类 这里真正实现Comparab接口的类型是Phone 而你用<T extends Comparable<? super T>>这个情况下T类实现Comparab接口的类型必须是T这个类, 而你HuaWei类是继承Phone类的,实际实现的接口类型是Phone所以调用方法1时就编译不过了

public class Test {
	public static void main(String args[]) {
		List<Phone> phone = new ArrayList<Phone>();
		phone.add(new Phone(1000));
		phone.add(new Phone(2000));
		List<HuaWei> huaWei = new ArrayList<HuaWei>();
		huaWei.add(new HuaWei(3000));
		huaWei.add(new HuaWei(4000));
		binarySearch1(phone);// 编译通过
		binarySearch2(phone);// 编译通过
		
		/*
		 * Bound mismatch: The generic method binarySearch1(List<T>) of type Test 
		 * is not applicable for the arguments (List<HuaWei>). 
		 * The inferred type HuaWei is not a valid substitute for the bounded parameter <T extends Comparable<T>>
		 */
		binarySearch1(huaWei);// 编译不通过
		
		
		binarySearch2(huaWei);// 编译通过
	}
	
	public static <T extends Comparable<T>> void binarySearch1(List<T> list){
		Collections.sort(list);
	}
	
	public static <T extends Comparable<? super T>> void binarySearch2(List<T> list){
		Collections.sort(list);
	}
}
class Phone implements Comparable<Phone> {
	public int price;
	
	public Phone(int price) {
		super();
		this.price = price;
	}

	public int compareTo(Phone other) {
		return this.price - other.price;
	}
}

class HuaWei extends Phone{

	public HuaWei(int price) {
		super(price);
	}
	
}
感谢回答,你的回答正是我困惑的地方,为什么一楼图片中24行能编译通过,按照你的回答来想的话24行应该编译不通过才对,因为Child类实际上实现的是Comparable<Parent>,而不是Comparable<Child>
wewsxccxss 2017-09-01
  • 打赏
  • 举报
回复
值得推荐~~~
soton_dolphin 2017-09-01
  • 打赏
  • 举报
回复
值得推荐~~~
hugh_z 2017-09-01
  • 打赏
  • 举报
回复
学习了.谢谢.

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧