java 基础,
分享于 点击 46424 次 点评:19
java 基础,
java 基础
Comparable & Comparator都是用来实现集合中的排序的 只是Comparable是在集合内部定义的方法实现的排序,Comparator是在集合外部实现的排序,所以,如想实现排序,就需要在集合外定义Comparator接口的方法 或 在集合内实现Comparable接口的方法。 Comparable 是一个对象本身就已经支持自比较所需要实现的接口(如String Integer自己就可以完成比较大小操作) . String的 int compareTo( anotherString) 按字典顺序比较两个字符串。该比较基于字符串中各个字符的 Unicode 值 返回值是 对应位置的 Unicode 值 之差 (< ,负数),(= , 0),(>, 正数) int compareTo(Integer anotherInteger ) 返回值是 (< ,-1),(= , 0),(>, 1) 用Comparator是策略模式(strategy design pattern),就是不改变对象自身,而用一个策略对象(strategy object)来改变它的行为 |
相关文章
- 暂无相关文章
用户点评