JAVA 泛型实现
来源:互联网 发布:真诚网络卖qq号可信吗 编辑:程序博客网 时间:2024/06/06 08:51
最近肝《Effective Java》
看到28 利用有限制通配符提升API的灵活性,看到代码就控制不住自己的手要实现它,几班波折终于。。。
---copy from book 《Effective Java》 s1
public static <T extends Comparable<? super T>> T max(List<? extends T> list) {
Iterator<? extends T> i = list.iterator();
T result = i.next();
while (i.hasNext()) {
T t = i.next();
if (t.compareTo(result) > 0) {
result = t;
}
}
return result;
}
---copy from book 《Effective Java》 e1
这是书上的代码
用来查找最大值,约定了泛型T 实现了Comparable 接口
我就想,妈呀要怎么比呢,如果我有一推double long float int 我该怎么找到最大的那个。(实际生产中几乎不会有这样的问题)
遂想用下面的代码实现他:
public static void main(String[] args) {
Random d = new Random();
List<Number> list = new ArrayList<Number>();
list.add(d.nextDouble());
list.add(d.nextDouble());
list.add(d.nextDouble());
list.add(d.nextDouble());
list.add(d.nextInt());
list.add(d.nextInt());
list.add(d.nextDouble());
list.add(d.nextFloat());
for (Number one : list) {
System.out.println(one.getClass().getName() + "--" + one);
}
Number max = Util.max(list);
System.out.println("max:" + max.getClass().getName() + "--" + max);
}
但是 Util.max 这一行 会报
Bound mismatch: The generic method max(List<? extends T>) of type Util is not applicable for the arguments (List<Number>). The inferred type Number is not a valid substitute for the bounded parameter <T extends Comparable<? super T>>
这个错误,经查Number 类 没实现 Comparable 囧。
那就弄个包装类出来好了。
包装类设计如下
package test.generic;
import java.math.BigDecimal;
public class NumberIn implements Comparable<NumberIn> {
private Number value;
public Number getValue() {
return value;
}
public void setValue(Number value) {
this.value = value;
}
public NumberIn(Number value) {
this.value = value;
}
@Override
public int compareTo(NumberIn o) {
BigDecimal self = null;
BigDecimal target = null;
if (o.getValue().getClass().equals(Double.class)) {
target = new BigDecimal(o.getValue().doubleValue());
} else if (o.getValue().getClass().equals(Integer.class)) {
target = new BigDecimal(o.getValue().intValue());
} else if (o.getValue().getClass().equals(Float.class)) {
target = new BigDecimal(o.getValue().floatValue());
} else if (o.getValue().getClass().equals(Long.class)) {
target = new BigDecimal(o.getValue().longValue());
} else {
try {
throw new Exception("in para class not legal" + o.getValue().getClass().getName());
} catch (Exception e) {
e.printStackTrace();
}
}
if (value.getClass().equals(Double.class)) {
self = new BigDecimal(value.doubleValue());
} else if (value.getClass().equals(Integer.class)) {
self = new BigDecimal(value.intValue());
} else if (value.getClass().equals(Float.class)) {
self = new BigDecimal(value.floatValue());
} else if (value.getClass().equals(Long.class)) {
self = new BigDecimal(value.longValue());
} else {
try {
throw new Exception("in para class not legal" + value.getClass().getName());
} catch (Exception e) {
e.printStackTrace();
}
}
return self.compareTo(target);
}
}
再把main 方法稍加改动
public static void main(String[] args) {
Random d = new Random();
List<NumberIn> list = new ArrayList<NumberIn>();
list.add(new NumberIn(d.nextDouble()));
list.add(new NumberIn(d.nextDouble()));
list.add(new NumberIn(d.nextDouble()));
list.add(new NumberIn(d.nextDouble()));
list.add(new NumberIn(d.nextInt()));
list.add(new NumberIn(d.nextInt()));
list.add(new NumberIn(d.nextDouble()));
list.add(new NumberIn(d.nextFloat()));
for (NumberIn one : list) {
System.out.println(one.getValue().getClass().getName() + "--" + one.getValue());
}
NumberIn max = Util.max(list);
System.out.println("max:" + max.getValue().getClass().getName() + "--" + max.getValue());
}
ok 跑动了 ,❤里有点小激动呢
- JAVA 泛型实现
- java泛型实现原理
- java泛型实现原理
- java泛型实现原理
- Java泛型实现测试
- java泛型实现原理
- java泛型接口怎么实现??
- java 数组泛型的简单实现
- 用Java泛型实现快速排序
- 用Java泛型实现堆排序
- java中利用泛型实现类
- Java泛型实现各个排序
- java 实现泛型List接口
- Java泛型的实现原理
- java泛型的实现原理
- Java泛型实现排序算法
- C++泛型 && Java泛型实现机制
- 使用JAVA泛型实现快速排序
- IO模型介绍以及同步异步阻塞非阻塞的区别
- [c++]容器类
- C基础知识复习(下)
- maven中的Hello World!
- 最大乘积 -- 蓝桥杯
- JAVA 泛型实现
- Attempt to present <UIImagePickerController: 0x1848de00> on <UINavigationController: 0x17e6ed50> wh
- android自定义View的研究——模拟真实按钮例子
- 【转】关于haslayout
- VS2011下生成sqlite3.lib
- android Application类的详细介绍
- mysql 修改已存在的表增加ID属性为auto_increment自动增长
- 修复Eclipse导入web工程变JAVA工程
- java+Jsoup 正则过滤html网页标签【多线程数据采集之二】