Java Collections.sort - 帮助我删除未经检查的警告
List<Question> questions = new ArrayList<Question>();
questions.addAll(getAllQuestions()); //returns a set of Questions
Collections.sort(questions, new BeanComparator("questionId")); //org.apache.commons.beanutils.BeanComparator
在 Java 1.5 下,除了 'new BeanComparator("questionId")' 生成未经检查的警告之外,上述工作正常。我不喜欢警告。有没有办法为 BeanComparator 提供类型,或者我必须使用 @SuppressWarnings("unchecked") ?
List<Question> questions = new ArrayList<Question>();
questions.addAll(getAllQuestions()); //returns a set of Questions
Collections.sort(questions, new BeanComparator("questionId")); //org.apache.commons.beanutils.BeanComparator
Under Java 1.5, the above works fine except that the 'new BeanComparator("questionId")' generates an unchecked warning. I do not like warnings. Is there a way I can provide the BeanComparator a type, or do I have to use @SuppressWarnings("unchecked")
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
选项有:
BeanComparator
以实现Comparator
。这不是一个真正的选择,因为它是一个众所周知的外部库类。人们不会让你这样做。Comparator
的类包装现有的BeanComparator
。问题
类型更改为List
。Options are:
BeanComparator
to implementComparator<Question>
. This is not a real option here, given it is a well-known external library class. People ain't going to let you do it.BeanComparator
as above, giving it a different FQN.BeanComparator
with a class that implementsComparator<Question>
.questions
toList<?>
.自 < code>BeanComparator 不是通用的,您只需抑制即可。
更新:实际上,如果它足够困扰您,您可以分叉代码库以使其通用,因为它是开源的。
Since
BeanComparator
isn't generic, you'll just have to suppress.UPDATE: Actually, if it bothers you enough, you could fork the codebase to make it generic, since it's Open Source.
除非在 Apache Commons Beanutils 中添加新的泛型类,否则我发现最好的方法是将 BeanComparator 包装在我的“bean 工具箱”中的新方法中:
Unless adding a new generic class at Apache Commons Beanutils, the best I found was to wrap BeanComparator in a new method in my "bean toolbox" :
创建一个通用包装类:
像这样使用它:
Create a generic wrapper class:
Use it like this:
是的,你应该使用@SuppressWarnings(“unchecked”)。在这种情况下,没有理由认为不使用泛型的比较器会导致问题。
Yes, you are supposed to use @SuppressWarnings("unchecked"). There is no reason to think the comparator not using generics can cause a problem in that case.
您随时可以改用 Google Collections。
他们支持泛型。
You could always switch to using Google Collections.
They support Generics.
消除警告的唯一方法是更改 BeanComparator 的代码,但即使可以,除非您将其制作为能够理解您的特定类型的特定包装器,否则该概念也不会很好地工作。该类通过反射对任何对象进行操作,该对象可能具有或不具有该方法。它本质上不是类型安全的。
解决警告的最简单方法是实现您自己的比较器:
如果重要的话,您还可以实现 equals,并像这样调用 BeanComparator equals 方法:
The only way to remove the warning would be to change the code of BeanComparator, but even if you could, unless you made it a specific wrapper that understands your particular type, the concept wouldn't work well. The class operates on any object by reflection which may or may not have the method. It is inherently not typesafe.
The simplest way around the warning is to implement your own comparator:
You could also implement equals if it matters, and call the BeanComparator equals method like this:
BeanComparator 是一个非常小的类。获取源代码并进行如下修改:
公共类 BeanComparator
并像这样修改您的调用:
Collections.sort(yourCollection, new BeanComparator
瞧,警告消失了。
BeanComparator is a very small class. Grab the source code and modify it like this:
public class BeanComparator<E> implements Comparator<E>, Serializable {
And modify your invocation like this:
Collections.sort(yourCollection, new BeanComparator<yourBeanClass>(yourProperty));
And voilà warnings disappeared.