Java Collections.sort - 帮助我删除未经检查的警告

发布于 2024-08-13 04:42:44 字数 418 浏览 5 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

无悔心 2024-08-20 04:42:44

选项有:

  • 更改 BeanComparator 以实现 Comparator。这不是一个真正的选择,因为它是一个众所周知的外部库类。人们不会让你这样做。
  • 按照上面的方法分叉并修改 BeanComparator,给它一个不同的 FQN。
  • 使用实现 Comparator 的类包装现有的 BeanComparator
  • 问题类型更改为List
  • 添加抑制警告注释。

Options are:

  • Change BeanComparator to implement Comparator<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 BeanComparator as above, giving it a different FQN.
  • Wrap the existing BeanComparator with a class that implements Comparator<Question>.
  • Change the type of questions to List<?>.
  • Add a suppress warnings annotation.
征﹌骨岁月お 2024-08-20 04:42:44

< 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.

我只土不豪 2024-08-20 04:42:44

除非在 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);
}

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" :

/**
 * 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);
}
南…巷孤猫 2024-08-20 04:42:44

创建一个通用包装类:

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);
  }
}

像这样使用它:

List<Question> questions = new ArrayList<Question>();
questions.addAll(getAllQuestions()); //returns a set of Questions
Collections.sort(questions, new GenericBeanComparator<Question>("questionId"));

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"));
柠栀 2024-08-20 04:42:44

是的,你应该使用@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.

古镇旧梦 2024-08-20 04:42:44

您随时可以改用 Google Collections。

他们支持泛型。

You could always switch to using Google Collections.

They support Generics.

最佳男配角 2024-08-20 04:42:44

消除警告的唯一方法是更改​​ BeanComparator 的代码,但即使可以,除非您将其制作为能够理解您的特定类型的特定包装器,否则该概念也不会很好地工作。该类通过反射对任何对象进行操作,该对象可能具有或不具有该方法。它本质上不是类型安全的。

解决警告的最简单方法是实现您自己的比较器:

 public class QuestionComparator extends Comparator<Question> {
      private BeanComparator peer = new BeanComparator("questionId");

      public int compare(Question o1, Question o2) {
             return peer.compare(o1, o2);
      }
 }

如果重要的话,您还可以实现 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);
   }

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:

 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:

   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);
   }
灯下孤影 2024-08-20 04:42:44

BeanComparator 是一个非常小的类。获取源代码并进行如下修改:


公共类 BeanComparator实现比较器,可序列化{

并像这样修改您的调用:


Collections.sort(yourCollection, new BeanComparator(yourProperty));

瞧,警告消失了。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文