这个代码尽可能地从更复杂的类结构中简化。在实际代码中,我在这里使用了Integer和Double类型的子类型。
我正在尝试使用带有类型参数的Java Generics。如果用户请求Number.class
类型,我们希望将List<Integer>
列表和List<Double>
列表合并到一个列表中。
虽然代码有效,但我无法获得未经检查的强制转换警告(请参阅TODO标记)。警告是:
Type safety: Unchecked cast from List<Integer> to Collection<? extends T>
但是,如果我删除了强制转换,我会收到编译错误:
The method addAll(Collection<? extends T>) in the type List<T> is not applicable for the arguments (List<Integer>).
我的代码:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
public class Generics1 {
static final List<Integer> intList = new ArrayList<Integer>(Arrays.asList(
1, 2, 3, 4));
static final List<Double> dblList = new ArrayList<Double>(Arrays.asList(
1.1, 2.2, 3.3));
public static <T extends Number> List<T> getObjects(Class<T> type) {
List<T> outList = new ArrayList<T>();
if (type == Number.class) {
// user asked for everything
// TODO: unchecked cast warnings here should be fixed
outList.addAll((Collection<? extends T>) intList);
outList.addAll((Collection<? extends T>) dblList);
} else {
// user asked for subtype of number
if (Integer.class.isAssignableFrom(type)) for (Integer i : intList)
if (type.isInstance(i)) {
T obj = type.cast(i);
outList.add(obj);
}
if (Double.class.isAssignableFrom(type)) for (Double d : dblList)
if (type.isInstance(d)) {
T obj = type.cast(d);
outList.add(obj);
}
}
return outList;
}
public static void main(String[] args) {
System.out.println("HI!");
System.out.println("integers: " + getObjects(Integer.class));
System.out.println("doubles: " + getObjects(Double.class));
System.out.println("numbers: " + getObjects(Number.class));
}
}
答案 0 :(得分:0)
您可以将其添加到您的代码中:
@SuppressWarnings("unchecked")
这是另一篇SO帖子,解释了“含义”:What is SuppressWarnings ("unchecked") in Java?
这是另一个处理转换可能有用的链接的问题:How do I fix "The expression of type List needs unchecked conversion...'?
虽然有一些重新编码,但你可能会让警告完全消失而不需要被压制。
答案 1 :(得分:0)
(先前的答案已删除)
以下是使用番石榴的方法:
@SuppressWarnings("unchecked")
public static <T> List<T> filterAndCollapse(final Class<T> type,
Collection<?> a, Collection<?> b) {
List combined = new ArrayList();
Predicate<Object> filter = new Predicate<Object>() {
public boolean apply(Object obj) {
return type.isInstance(obj);
}
};
combined.addAll(Collections2.filter(a, filter));
combined.addAll(Collections2.filter(b, filter));
return combined;
}
// ...
filter(Number.class, intList, dblList);
编辑:完全类型安全的比较方式。
public static <T> List<T> filterAndCollapse(final Class<T> type,
Collection<?> a, Collection<?> b) {
List<T> combined = new ArrayList<T>();
Predicate<Object> filter = new Predicate<Object>() {
public boolean apply(Object obj) {
return type.isInstance(obj);
}
};
Function<Object, T> transform = new Function<Object, T>() {
public T apply(Object obj) {
return type.cast(obj);
}
};
combined.addAll(Collections2.transform(Collections2.filter(a, filter),
transform));
combined.addAll(Collections2.transform(Collections2.filter(b, filter),
transform));
return combined;
}
不幸的是,根据我的知识,没有办法用Guava过滤和转换一步。
答案 2 :(得分:0)
(Class<T> type)
List<T> outList = new ArrayList<T>();
if (type == Number.class) {
// obviously, T==Number here, though the compiler doesn't know that
// so we do the cast. compiler will still warn. since the cast makes
// perfect sense and is obviously correct, we are ok with it.
List<Number> numList = (List<Number>)outList;
numList.addAll( intList);
numList.addAll( dblList);
} else {
更好的解决方案,简单
for list in lists
for item in list
if item instance of type
add item to result