zoukankan      html  css  js  c++  java
  • Effective Java 12 Consider implementing Comparable

    Sort array with sorted collection construction.

       

    public class WordList {

    public static void main(String[] args) {

    Set<String> s = new TreeSet<String>();

    // This will sort and filter the duplicated items in the string array automatically.

    Collections.addAll(s, args);

    System.out.println(s);

    }

    }

       

    The interface

       

    public interface Comparable<T> {

    int compareTo(T t);

    }

       

    public final class CaseInsensitiveString

    implements Comparable<CaseInsensitiveString> {

    public int compareTo(CaseInsensitiveString cis) {

    return String.CASE_INSENSITIVE_ORDER.compare(s, cis.s);

    }

    ... // Remainder omitted

    }

       

    Implementation Key point

    • The implementor must ensure sgn(x.compareTo(y)) == -sgn(y.compareTo(x))for all x and y. (This implies that x.compareTo(y)must throw an exception if and only if y.compareTo(x)throws an exception.)

    • The implementor must also ensure that the relation is transitive: (x.compareTo(y) > 0 && y.compareTo(z) > 0)implies x.compareTo(z) > 0.

    • Finally, the implementor must ensure that x.compareTo(y) == 0 implies that

    sgn(x.compareTo(z)) == sgn(y.compareTo(z)), for all z.

    • It is strongly recommended, but not strictly required, that (x.compareTo(y)== 0) == (x.equals(y)). Generally speaking, any class that implements the Comparable interface and violates this condition should clearly indicate this fact. The recommended language is "Note: This class has a natural ordering

    that is inconsistent with equals."

       

    Note

    Compare integral primitive fields using the relational operators < and >. For floating-point fields, use Double.compare or Float.compare in place of the relational operators, which do not obey the general contract for compareTo when applied to floating point values. For array fields, apply these guidelines to each element.

       

    // Normal implementation

    public int compareTo(PhoneNumber pn) {

    // Compare area codes

    if (areaCode < pn.areaCode)

    return -1;

    if (areaCode > pn.areaCode)

    return 1;

    // Area codes are equal, compare prefixes

    if (prefix < pn.prefix)

    return -1;

    if (prefix > pn.prefix)

    return 1;

    // Area codes and prefixes are equal, compare line numbers

    if (lineNumber < pn.lineNumber)

    return -1;

    if (lineNumber > pn.lineNumber)

    return 1;

    return 0; // All fields are equal

    }

       

    /* The code below should be used when you're certain the fields in question are non-negative or, more generally, that the difference between the lowest and highest possible field values is less than or equal to Integer.MAX_VALUE(231-1).

    */

    public int compareTo(PhoneNumber pn) {

    // Compare area codes

    int areaCodeDiff = areaCode - pn.areaCode;

    if (areaCodeDiff != 0)

    return areaCodeDiff;

    // Area codes are equal, compare prefixes

    int prefixDiff = prefix - pn.prefix;

    if (prefixDiff != 0)

    return prefixDiff;

    // Area codes and prefixes are equal, compare line numbers

    return lineNumber - pn.lineNumber;

    }

       

       

       

       

    作者:小郝
    出处:http://www.cnblogs.com/haokaibo/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    使用PHP Socket 编程模拟Http post和get请求
    php socket客户端及服务器端应用实例
    php五大运行模式CGI,FAST-CGI,CLI,ISAPI,APACHE模式浅谈
    php 连接 mssql sql2008
    开源内容管理系统Joomla正式发布3.5版本 基于PHP 7
    swift--使用 is 和 as 操作符来实现类型检查和转换 / AnyObject与Any的区别
    swift--获取window
    ios开发之--ios11适配:TableView的heightForHeaderInSection设置高度无效/UISearchBar消失
    swift--触摸(UITouch)事件(点击,移动,抬起)
    swift--添加新手引导页
  • 原文地址:https://www.cnblogs.com/haokaibo/p/consider-implementing-Comparable.html
Copyright © 2011-2022 走看看