标签: hashcode

有没有办法使用ReSharper自动生成GetHashCode和Equals?

在eclipse中,当我使用Java进行编码时,有一个功能可以自动生成基本,高效且无错误的实现,hashCode()并且equals()不会消耗脑力.

是否在Visual Studio或ReSharper中内置了类似的功能?

.net resharper equals hashcode visual-studio

36
推荐指数
2
解决办法
1万
查看次数

如何为hashCode()进行单元测试?

如何在单元测试中测试 hashCode()函数?

public int hashCode(){
    int result = 17 + hashDouble(re);
    result = 31 * result + hashDouble(im);
    return result;
}
Run Code Online (Sandbox Code Playgroud)

java junit unit-testing hashcode

35
推荐指数
2
解决办法
3万
查看次数

如果Java的垃圾收集器移动对象,那么什么是Object.hashCode和System.identityHashCode?

我经常听说这些方法(Object.hashCodeSystem.identityHashCode)返回对象的地址,或者从地址快速计算的东西; 但我也很确定垃圾收集器会移动并压缩物体.由于哈希码不能改变,这就出现了问题.我知道这不是人们日常工作需要知道的事情,但我想了解内部情况.那么,有谁知道这是如何在Java中实现的?或.NET,因为它们可能类似.

java garbage-collection internals hashcode object-identity

35
推荐指数
1
解决办法
2084
查看次数

为什么hashCode()和getClass()是本机方法?

我检查的源代码Object级的,我发现其中的方法声明getClass()

public final native Class<?> getClass();
Run Code Online (Sandbox Code Playgroud)

而声明hashCode()

public native int hashCode();
Run Code Online (Sandbox Code Playgroud)

为什么这两个方法native在类中有哪些方法?如何获取这些方法的源代码?

c c++ java methods hashcode

35
推荐指数
2
解决办法
1万
查看次数

如何教eclipse从jdk 7 Objects类生成紧凑的equals()和hashCode()?

几天前我们在公司内部切换到了Java 7--最后!Jay\o /所以我发现了这个Objects类,并且惊讶于方法的实现有多短,hashCode()并且equals()与默认的eclipse(ALT + SHIFT + S - > H)生成的代码相比减少了许多boylerplate代码.

我在想,如果发生日食的默认行为,我可以改变hashCode()equals()

我很想看到这个:

@Override
public int hashCode()
{
  return Objects.hash(one, two, three, four/*, ...*/);
}
Run Code Online (Sandbox Code Playgroud)

而不是这个:

@Override
public int hashCode()
{
  final int prime = 31;
  int result = 1;
  result = prime * result + ((one == null) ? 0 : one.hashCode());
  result = prime * result + ((two == null) ? 0 : two.hashCode());
  result = prime * result …
Run Code Online (Sandbox Code Playgroud)

java eclipse equals hashcode

34
推荐指数
1
解决办法
9866
查看次数

为什么不同布尔实例的哈希码始终相同?

在下面的代码中,哈希码总是相同的.为什么会那样?

码:

public class BooleanClass {

    public static void main(String[] args) {
        Boolean b1 = new Boolean(true);
        Boolean b2 = new Boolean(false);
        Boolean b3 = new Boolean(true);
        Boolean b4 = new Boolean(false);
        Boolean b5 = new Boolean(false);
        Boolean b6 = new Boolean(true);

        System.out.println(b1.hashCode());
        System.out.println(b2.hashCode());
        System.out.println(b3.hashCode());
        System.out.println(b4.hashCode());
        System.out.println(b5.hashCode());
        System.out.println(b6.hashCode());
    }
}
Run Code Online (Sandbox Code Playgroud)

输出:

1231
1237
1231
1237
1237
1231
Run Code Online (Sandbox Code Playgroud)

始终使用相同的数字12311237打印.任何原因?

java boolean hashcode

34
推荐指数
5
解决办法
5268
查看次数

在Java中为具有循环引用的对象实现equals和hashCode

我定义了两个类,它们都包含对另一个对象的引用.它们看起来与此类似(这是简化的;在我的真实域模型中,A类包含B的列表,每个B都有一个返回父A的引用):

public class A {

    public B b;
    public String bKey;

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((b == null) ? 0 : b.hashCode());
        result = prime * result + ((bKey == null) ? 0 : bKey.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (!(obj instanceof A))
            return false;
        A other …
Run Code Online (Sandbox Code Playgroud)

java equals hashcode

33
推荐指数
1
解决办法
5114
查看次数

为什么2个委托实例返回相同的哈希码?

请考虑以下事项:

  var x =  new Action(() => { Console.Write("") ; });
  var y = new Action(() => { });
  var a = x.GetHashCode();
  var b = y.GetHashCode();
  Console.WriteLine(a == b);
  Console.WriteLine(x == y);
Run Code Online (Sandbox Code Playgroud)

这将打印:

True
False
Run Code Online (Sandbox Code Playgroud)

为什么哈希码相同?

这有点令人惊讶,并且会使代表的使用Dictionary速度与a List(也就是O(n)查找)一样慢.

更新:

问题是为什么.IOW谁做出这样一个(愚蠢的)决定?

一个更好的哈希码实现应该是:

return Method ^ Target == null ? 0 : Target.GetHashcode();
// where Method is IntPtr
Run Code Online (Sandbox Code Playgroud)

.net c# delegates hashcode

31
推荐指数
1
解决办法
2433
查看次数

HashCode给出负值

我通过执行以下函数将传入的字符串转换为哈希码,但某些值为负值.我不认为哈希值应该是负数.请告诉我我做错了什么.

int combine = (srcadd + dstadd + sourceport + destinationport + protocol).hashCode();
System.out.println(combine);
Run Code Online (Sandbox Code Playgroud)

java hashcode negative-number

31
推荐指数
3
解决办法
5万
查看次数

使用相同的哈希码生成两个不同的字符串

我想做一些测试,这些测试需要一些具有相同哈希码的字符串,但不是相同的字符串.我找不到任何例子,所以我决定写一个简单的程序来为我做.

下面的代码反复生成两个随机字符串,直到它们生成相同的哈希码.

    static Random r = new Random();
    static void Main(string[] args)
    {
        string str1, str2;
        do
        {
            str1 = GenerateString();
            str2 = GenerateString();
        } while (str1.GetHashCode() != str2.GetHashCode() && str1 != str2);

        Console.WriteLine("{0}\n{1}", str1, str2);
    }

    static string GenerateString()
    {
        string s = "";
        while (s.Length < 6)
        {
            s += (char)r.Next(char.MaxValue);
        }
        return s;
    }
Run Code Online (Sandbox Code Playgroud)

这段代码似乎有效(理论上),但可能需要几个世纪才能完成.所以我反过来考虑从一个哈希码生成两个字符串.

我知道从哈希码中检索字符串是不可能的,但是可以从中生成可能的字符串吗?

我正在使用Visual Studio 2015社区版.版本:14.0.23107.0D14REL.

.NET Framework:4.6.00081.

c# string hashcode

31
推荐指数
2
解决办法
2873
查看次数