JAVA 泛型实现

本文介绍了在阅读《Effective Java》时对有限制通配符的泛型方法的实践过程,探讨了如何处理不同类型的Number对象寻找最大值的问题。通过创建实现Comparable接口的包装类NumberIn,成功解决因Number类未实现Comparable接口导致的编译错误,实现了泛型方法Util.max()在各种数值类型间的比较。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近肝《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 跑动了  ,❤里有点小激动呢

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值