简体   繁体   English

泛型 - (列表的元素实例)的法律替代 <? extends Comparable> )

[英]Generics - Legal alternative for (elements instanceof List<? extends Comparable>)

I have this method which unique parameter (List elements) sets elements to a ListModel, but I need to make a validation to see if the generic type implements comparable and since such thing like: 我有这个方法,其中唯一的参数(List elements)将元素设置为ListModel,但我需要进行验证以查看泛型类型是否实现了可比性,因为这样的事情:

if (elements instanceof List<? extends Comparable>)

is illegal, I don't know how to do the proper validation. 是非法的,我不知道如何做正确的验证。

Update 更新

I already have done this validation using: 我已经使用以下方法完成了此验证:

(elements.size() > 0 && elements.get(0) instanceof Comparable)

but I want to know if is there a cleaner solution for that, using for example reflection? 但我想知道是否有更清洁的解决方案,例如使用反射?

Thanks in advance. 提前致谢。

The generic type of a list is erased at runtime . 列表的泛型类型在运行时擦除 For this to work you need to either require the parameter in the method signature or test each element individually. 要使其工作,您需要在方法签名中要求参数或单独测试每个元素。

public void doSomething(List<? extends Comparable> elements) {}

OR 要么

for (Object o : elements) {
    if (o instanceof Comparable) {}
}

If you have control over the code, the former is preferred and cleaner; 如果您可以控制代码,前者是首选且更清洁; the later can be wrapped in a utility method call if needed. 如果需要,可以将后者包装在实用程序方法调用中。

That's not possible. 那是不可能的。 instanceof is a runtime operator whereas generic information is lost at runtime (type-erasure). instanceof是运行时运算符,而通用信息在运行时丢失(类型擦除)。

I'm not a Generics guru, but to my understanding the reason you can't do that is that at runtime, there's no distinction between an ArrayList and, say, an ArrayList<String> . 我不是泛型大师,但根据我的理解,你不能这样做的原因是在运行时, ArrayListArrayList<String>之间没有区别。 So it's too late to perform the test you want. 因此,执行您想要的测试为时已晚。

Why not just declare your method to accept a List<? extends Comparable> 为什么不声明你的方法接受List<? extends Comparable> List<? extends Comparable> instead of a List ? List<? extends Comparable>而不是List

In particular, the way you phrased your question makes it sound like you expect the list to always contain homogeneous elements, but a plain old List doesn't give you any sort of assurance like that. 特别是,你提出问题的方式使得听起来好像你希望列表总是包含同类元素,但是一个普通的旧List不会给你任何类似的保证。

To your update: simply making sure that one element implements Comparable is not enough (what if the other ones don't?) And making sure that all of the elements implement Comparable is also not enough to validate (what if they are of different classes that implement Comparable but cannot compare with each other?). 对你的更新:简单地确保一个元素实现Comparable是不够的(如果其他元素不实现怎么办?)并确保所有元素实现Comparable也不足以验证(如果它们属于不同的类,该怎么办?实现可比较但无法相互比较?)。

But the bigger question is, why bother validating at runtime anyway? 但更大的问题是,为什么还要在运行时验证? What benefit does that give compared to simply trying to use it and then seeing that it fails (ie throws an Exception, which maybe you can catch)? 与仅仅尝试使用它然后看到它失败(即抛出异常,也许你可以捕获)相比,它给了什么好处?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 泛型之间的区别 <T extends Number & Comparable<T> &gt;和T扩展可比 <? extends Number> - Difference between generics <T extends Number & Comparable<T>> and T extends Comparable<? extends Number> Java Generics E扩展了Comparable <E> 留下警告 - Java Generics E extends Comparable<E> leaves warning 带通配符的泛型(地图 <? extends A, List<? extends B> &gt;) - Generics with Wildcards (Map<? extends A, List<? extends B>>) 可比VS. <? extends Comparable> - Comparable VS <? extends Comparable> Java泛型-为什么不允许我做可比 <? extends T> 而不是可比 <? super T> ? - Generics in Java - Why I am not allowed to do Comparable<? extends T> instead of Comparable<? super T>? Java泛型:Comparable类型的compareTo(capture#2-of?扩展E)方法不适用于自变量(Comparable) - Java Generics :The method compareTo(capture#2-of ? extends E) in the type Comparable is not applicable for the arguments (Comparable) 可比和通用 - Comparable and generics 行类HanoiStack中的Java泛型编译错误 <T extends Comparable<T> &gt;扩展堆栈 <T> - Java Generics compile error in line class HanoiStack<T extends Comparable<T>> extends Stack<T> Java泛型-“ extends”关键字-&gt;不支持添加元素 - java generics - “extends” keyword -> not supporting add elements 泛型:列表<? extends Animal>与列表相同<Animal> ? - Generics : List<? extends Animal> is same as List<Animal>?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM