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