有人知道 Java 比较器库吗?
我正在寻找一个包含许多有用的比较器实现的 foss 库,其中有许多准备就绪的小无聊比较器。
Apache commons 比较器
- 反向
- null 首先/null 最后
- 链
- 自然
- 转换器
还有许多其他有用的可重用可能性,但不可用。
- 空白 忽略
- 空白标准化
- 数字感知字符串 - 例如“apple 10”> “苹果2”。
@SPF 我已经包含了一些伪代码,这些代码显示了如何在不创建任何临时字符串等的情况下一次性标准化和比较。虽然它未经测试并且可能不起作用,但进行快速比较并不需要太多时间。
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
}
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 commons comparators
- reverse
- null first/null last
- chain
- natural
- transformer
There are so many other useful reusable possibilities that arent available.
- whitespace ignoring
- whitespace normalizing
- number aware strings - eg "apple 10" > "apple 2".
@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
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您不会得到很多现成的比较器,但是 Guava 有
订购
类,它既扩展了比较器功能,又添加了一些有用的默认实现作为工厂方法,而且: Guava和 Apache Commons / Lang (那里:我说过)将帮助您使用 < 实现自定义比较器或比较器a href="http://commons.apache.org/lang/api-2.5/org/apache/commons/lang/builder/CompareToBuilder.html" rel="nofollow noreferrer">
CompareToBuilder
< /a> 和ComparisonChain分别是。恐怕没有比这更好的了。
关于这些要求:
在比较器中执行这些操作并不明智,因为这意味着未修改的数据仍保留在集合中,并且
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 methodAnd also: both Guava and Apache Commons / Lang (there: I said it) will help you implement custom Comparators or Comparables using
CompareToBuilder
andComparisonChain
, respectively. It doesn't get much better than that, I'm afraid.And about these requirements:
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.
从 3.0.2 开始,Commons Lang 将在 org.apache.commons.lang3.compare 包中拥有集合比较器的副本。欢迎提出更多建议。
Commons Lang from 3.0.2 onwards will have a copy of the Collections comparators in the org.apache.commons.lang3.compare package. Feel free to propose more.