Java Collections.sort - 帮我删除未选中的警告
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1822836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Collections.sort - help me remove the unchecked warning
提问by emulcahy
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")?
在 Java 1.5 下,除了 'new BeanComparator("questionId")' 生成未经检查的警告之外,上述工作正常。我不喜欢警告。有什么方法可以为 BeanComparator 提供类型,还是必须使用@SuppressWarnings("unchecked")?
回答by Stephen C
Options are:
选项是:
- Change
BeanComparatorto 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. - Fork and modify
BeanComparatoras above, giving it a different FQN. - Wrap the existing
BeanComparatorwith a class that implementsComparator<Question>. - Change the type of
questionstoList<?>. - Add a suppress warnings annotation.
- 更改
BeanComparator以执行Comparator<Question>。鉴于它是一个众所周知的外部库类,这在这里不是一个真正的选择。人们不会让你这样做的。 - Fork and modify
BeanComparator如上,给它一个不同的FQN。 - 包装现有
BeanComparator一个类实现Comparator<Question>。 - 将 的类型更改
questions为List<?>。 - 添加抑制警告注释。
回答by Hank Gay
Since BeanComparatorisn't generic, you'll just have to suppress.
由于BeanComparator不是通用的,你只需要压制。
UPDATE: Actually, if it bothers you enough, you could fork the codebase to make it generic, since it's Open Source.
更新:实际上,如果它足够困扰您,您可以分叉代码库以使其通用,因为它是开源的。
回答by adriclad
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" :
除非在 Apache Commons Beanutils 中添加一个新的泛型类,否则我发现最好的方法是将 BeanComparator 包装在我的“bean 工具箱”中的新方法中:
/**
* Wrapping of Apache communs BeanComparator. Create a comparator which compares two beans by the specified bean
* property. Property expression can use Apache's nested, indexed, combinated, mapped syntax. @see <a
* href="http://commons.apache.org/beanutils/api/org/apache/commons/beanutils/BeanComparator.html">Apache's Bean
* Comparator</a> for more details.
* @param <T> generic type
* @param propertyExpression propertyExpression
* @return the comparator
*/
@SuppressWarnings("unchecked")
public static <T> Comparator<T> createPropertyComparator(final String propertyExpression) {
return new BeanComparator(propertyExpression);
}
回答by dionm
Create a generic wrapper class:
创建一个通用的包装类:
public class GenericBeanComparator<T> implements Comparator<T> {
private final BeanComparator myBeanComparator;
public GenericBeanComparator(String property) {
myBeanComparator = new BeanComparator(property);
}
public int compare(T o1, T o2) {
return myBeanComparator.compare(o1, o2);
}
}
Use it like this:
像这样使用它:
List<Question> questions = new ArrayList<Question>();
questions.addAll(getAllQuestions()); //returns a set of Questions
Collections.sort(questions, new GenericBeanComparator<Question>("questionId"));
回答by Lluis Martinez
BeanComparator is a very small class. Grab the source code and modify it like this:
BeanComparator 是一个非常小的类。抓取源代码并像这样修改它:
public class BeanComparator<E> implements Comparator<E>, Serializable {
public class BeanComparator<E> implements Comparator<E>, Serializable {
And modify your invocation like this:
并像这样修改您的调用:
Collections.sort(yourCollection, new BeanComparator<yourBeanClass>(yourProperty));
Collections.sort(yourCollection, new BeanComparator<yourBeanClass>(yourProperty));
And voilà warnings disappeared.
瞧,警告消失了。
回答by Sindri Traustason
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.
是的,您应该使用@SuppressWarnings("unchecked")。在这种情况下,没有理由认为不使用泛型的比较器会导致问题。
回答by Fortyrunner
You could always switch to using Google Collections.
您可以随时切换到使用 Google Collections。
They support Generics.
他们支持泛型。
回答by Yishai
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.
消除警告的唯一方法是更改 BeanComparator 的代码,但即使可以,除非您将其设置为理解您的特定类型的特定包装器,否则该概念将无法正常工作。该类通过反射对任何对象进行操作,这些对象可能有也可能没有方法。它本质上不是类型安全的。
The simplest way around the warning is to implement your own comparator:
解决警告的最简单方法是实现您自己的比较器:
public class QuestionComparator extends Comparator<Question> {
private BeanComparator peer = new BeanComparator("questionId");
public int compare(Question o1, Question o2) {
return peer.compare(o1, o2);
}
}
You could also implement equals if it matters, and call the BeanComparator equals method like this:
如果重要,您也可以实现 equals,并像这样调用 BeanComparator equals 方法:
public boolean equals(Object o) {
//boiler plate code here to ensure o is an instance of Question and not null
return ((QuestionComparator) o).peer.equals(peer);
}

