我正在追踪一个包含许多有用的Comparator实现的化石库,它有许多可以随意使用的无聊的比较器.
Apache commons比较器
还有许多其他有用的可重用可能性.
@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
}
Run Code Online (Sandbox Code Playgroud)
我不认为你会得到很多现成的比较器,但是Guava有这样的Ordering类,它既扩展了比较器的功能,又添加了一些有用的默认实现作为工厂方法
而且:Guava和Apache Commons/Lang(在那里:我说过)将帮助您分别使用CompareToBuilder和实现自定义Comparators或Comparables ComparisonChain.我担心它没有那么好.
关于这些要求:
还有许多其他有用的可重用可能性.
- 空白无视
- 空格正常化
- 数字感知字符串 - 例如"apple 10">"apple 2".
在比较器中执行任何此操作并不明智,因为这意味着您的未修改数据仍保留在集合中,并且Comparator需要为每次比较进行两次所需的转换.现在想想用几百万个条目对数组进行排序.需要多少个字符串转换?
首先将数据标准化然后对其进行排序总是更明智.
| 归档时间: |
|
| 查看次数: |
1130 次 |
| 最近记录: |