【发布时间】:2016-08-23 02:09:09
【问题描述】:
这个问题是在一次采访中被问到的。问题是为什么String 和其他包装类实现Comparable 而不是Comparator 接口。我试图解释Comparator基本上是提供自定义排序,Comparable是默认自然排序顺序。同样从设计原理的角度来看,Comparable 是紧耦合的,Comparator 是松耦合的。但是,我无法解释为什么String 实现了Comparable 而不是Comparator。
【问题讨论】:
-
其他半重复:“When should a class be Comparable and/or Comparator?”和“What is the difference between compare() and compareTo()?”。不幸的是,没有规范的 dup-target 问题......只有少数适度赞成的相互重复。
标签: java string comparator comparable