有人知道Java比较器库吗? [英] Does anyone know of a Java Comparators library?

查看:61
本文介绍了有人知道Java比较器库吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我追求的是一个foss库,该库包含许多有用的Comparator实现,其中包含许多准备就绪的无聊的比较器。

I am after a foss library that includes many useful Comparator implementations, that is has lots of little boring comparators that are ready to go.

Apache公共比较器

Apache commons comparators


  • 反向

  • 第一个无效/最后一个无效

  • chain

  • natural

  • transformer

  • reverse
  • null first/null last
  • chain
  • natural
  • transformer

还有很多其他有用的可重用的可能性,因此没有可用。

There are so many other useful reusable possibilities that arent available.


  • 空格忽略

  • 空格标准化

  • 可识别数字的字符串-例如 apple 10> apple 2。

@SPF
Ive包含了一些伪代码,这些代码显示了如何在不创建任何临时字符串等的情况下一次性进行规范化和比较。然而,未经测试且可能没有

@SPF Ive included some psuedo code that shows how one can normalize and compare in one pass without creating any temporary strings etc. WHile it is untested and probably doesnt work it wouldnt take much to have a working fast compare.

while {

   while
        get next char from $string1
        if none left then
           $string1 > $string2 return +1;
        get next char from $string1
        increase $index1
        if previous was whitespace and this is whitespace then
           continue;
        end if
   end while

   while
    get next char from $string2
    if none left then
       $string2 > $string1 return +1;
    get next char from $string2
    increase $index2
    if previous was whitespace and this is whitespace then
       continue;
    end if
   end while

   result = $char1 - $char2
   if result != 0 return
}


推荐答案

我认为您不会得到很多现成的比较器,但是番石榴具有 Ordering 类既扩展了Comparator功能,又添加了一些有用的默认值作为工厂方法的实现

I don't think you'll get many readymade comparators, but Guava has the Ordering class which both extends the Comparator functionality and adds some useful default implementations as factory method

以及:番石榴和< a href = http://commons.apache.org/lang rel = nofollow noreferrer> Apache Commons / Lang (我说过)将帮助您使用 CompareToBuilder ComparisonChain 。恐怕没有比这更好的了。

And also: both Guava and Apache Commons / Lang (there: I said it) will help you implement custom Comparators or Comparables using CompareToBuilder and ComparisonChain, respectively. It doesn't get much better than that, I'm afraid.

关于这些要求:


还有很多其他有用的可重用的可能性,因此无法提供。

There are so many other useful reusable possibilities that arent available.


  • 空白忽略

  • 空格标准化

  • 可识别数字的字符串-例如, apple 10 > 苹果2。

在比较器中执行任何上述操作都不明智,因为这意味着您的未修改的数据仍保留在集合中,比较器需要为每次比较两次进行所需的转换。现在考虑对具有数百万个条目的数组进行排序。

It's not smart to do any of this in a comparator, because it means that your unmodified data remains in the collection and the Comparator need to make the required conversion twice for every comparison. Now think of sorting an Array with several million entries. How many String conversions would that require?

首先规范化数据然后对其进行排序始终是明智的选择。

It's always wiser to normalize your data first and then sort it.

这篇关于有人知道Java比较器库吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆