zoukankan      html  css  js  c++  java
  • 用最有效率的方法计算 2 乘以 8?

    2 << 3(左移 3 位相当于乘以 2 的 3 次方,右移 3 位相当于除以 2 的 3 次方)。

    补充:我们为编写的类重写 hashCode 方法时,可能会看到如下所示的代码,其实我们不太理解为什么要使用这样的乘法运算来产生哈希码(散列码),而且为什么这个数是个素数,为什么通常选择 31 这个数?前两个问题的答案你可以自己百度一下,选择 31 是因为可以用移位和减法运算来代替乘法,从而得到更好的性能。说到这里你可能已经想到了:31 * num 等价于(num << 5) - num,左移 5位相当于乘以 2 的 5 次方再减去自身就相当于乘以 31,现在的 VM 都能自动完成这个优化。

    public class PhoneNumber {

    private int areaCode;

    private String prefix;

    private String lineNumber;

    @Override

    public int hashCode() {

    final int prime = 31;

    int result = 1;

    result = prime * result + areaCode;

    result = prime * result

    + ((lineNumber == null) ? 0 : lineNumber.hashCode());

    result = prime * result + ((prefix == null) ? 0 : prefix.hashCode());

    return result;

    }

    @Override

    public boolean equals(Object obj) {

    if (this == obj)

    return true;

    if (obj == null)

    return false;

    if (getClass() != obj.getClass())

    return false;

    PhoneNumber other = (PhoneNumber) obj;

    if (areaCode != other.areaCode)

    return false;

    if (lineNumber == null) {

    if (other.lineNumber != null)

    return false;

    } else if (!lineNumber.equals(other.lineNumber))

    return false;

    if (prefix == null) {

    if (other.prefix != null)

    return false;

    } else if (!prefix.equals(other.prefix))

    return false;

    return true;

    }

    }

  • 相关阅读:
    MIN (Transact-SQL)【转】
    ROW_NUMBER() OVER函数的基本用法用法【转】
    读取文件中的内容
    Stopwatch 和TimeSpan介绍【转】
    TimeSpan类【转】
    Stopwatch 类【转】
    ToString()使用方法
    用c#读取文件内容中文是乱码的解决方法:
    vue实现购物车和地址选配(二)
    vue实现购物车和地址选配
  • 原文地址:https://www.cnblogs.com/programb/p/13021297.html
Copyright © 2011-2022 走看看