标签: hashcode

Java集合 - 覆盖equals和hashCode

class Hash {
  int a;

  Hash(int h){
    a=h;
  }

  public boolean equals(Object o) {     
    Boolean h=super.equals(o);
    System.out.println("Inside equals ");
    return h;
  }

  public int hashCode() {    
    System.out.println("Inside Hash");    
    return 2;
  }    
}
Run Code Online (Sandbox Code Playgroud)
public class Eq {    
  public static void main(String...r) {    
    HashMap<Hash,Integer> map=new HashMap<Hash,Integer>();    
    Hash j=new Hash(2);    
    map.put(j,1);
    map.put(j,2);
    System.out.println(map.size());
  }
}
Run Code Online (Sandbox Code Playgroud)

输出是

inside hash

inside hash
1

由于它返回相同的哈希码,第二次在hashmap中添加一个对象时,它必须使用equals方法,但它不会调用.那么问这个问题呢?

java collections equals hashcode

0
推荐指数
1
解决办法
1377
查看次数

hashCode实现

class A{

      @Override
      public int hashCode() {
        return 10;
    }
}

public class SampleClass {

    public static void main(String[] args){
        Map map = new HashMap();

        map.put(new A(), "A");
        map.put(new A(), "B");

        System.out.println(map.size());

        System.out.println(new A().hashCode());
        System.out.println(new A().hashCode());
    }
}
Run Code Online (Sandbox Code Playgroud)

输出: -

2

10

10
Run Code Online (Sandbox Code Playgroud)

为什么2 ??? 如果我们正在实现返回相同整数的hashCode方法.不应该大小为1 ???

java hashcode

0
推荐指数
1
解决办法
736
查看次数

比较TreeMap内容会给出错误的答案

我在另一个TreeMap中使用TreeMap作为"键"

TreeMap<TreeMap<String, String>, Object>
Run Code Online (Sandbox Code Playgroud)

在我的代码中,'object'是一个个人构造,但是对于这种情况,我使用了一个字符串.

我创建了一对TreeMaps来测试TreeMap.CompareTo()TreeMap.HashCode()方法.这从以下开始......

public class TreeMapTest 

public void testTreeMap()
{
TreeMap<String, String> first = new TreeMap<String, String>();
TreeMap<String, String> second = new TreeMap<String, String>();

first.put("one", "une");
first.put("two", "deux");
first.put("three", "trois");
second.put("une", "one");
second.put("deux", "two");
second.put("trois", "three");

TreeMap<TreeMap<String, String>, String> english = new TreeMap<TreeMap<String, String>, String>();
TreeMap<TreeMap<String, String>, String> french = new TreeMap<TreeMap<String, String>, String>();

english.put(first, "english");
french.put(second, "french");
Run Code Online (Sandbox Code Playgroud)

从这里我现在调用英语项目来查看它是否包含密钥

if (english.containsKey(second))
{
System.out.println("english contains the key");
//throws error of ClassCastException: Java.util.TreeMap cannot …
Run Code Online (Sandbox Code Playgroud)

java dictionary hashcode treemap compareto

0
推荐指数
1
解决办法
1678
查看次数

有没有理由我应该调用Integer.hashCode()?

也许有一个我不知道的原因,但我看到它在我的代码中用于计算复杂对象的哈希码.与将Integer本身放在那里相比,它能提供什么吗?(我希望不是),或者只是为了更清晰?

class SomeClass() {
private Integer myIntegerField1;
private Integer myIntegerField2;
...
public int hashCode() {
 final int prime = 31;
 int result =1;
 result = prime * result + ((myIntegerField1 == null) ? 0 : myIntegerField1.hashCode());
 result = prime * result + ....
 ...
 return result;
}
}
Run Code Online (Sandbox Code Playgroud)

java hashcode

0
推荐指数
1
解决办法
275
查看次数

我可以从 HashCode 获取对象吗?

他们有什么办法可以从哈希码中获取对象吗???

实际上问题是我在我们的应用程序中发现了一些绑定警告,并且每个警告都有相同的源哈希代码。我尝试通过源名称和目标名称进行搜索,但没有找到这样的东西。

所以请帮我找到实际绑定警告即将到来的对象或样式或控制模板?或者帮助我通过其哈希代码找到警告即将到来的对象。

以下绑定警告即将到来。

System.Windows.Media.Animation Warning: 6 : Unable to perform action because the specified Storyboard was never applied to this object for interactive control.; Action='SkipToFill'; Storyboard='System.Windows.Media.Animation.Storyboard'; Storyboard.HashCode='33003048'; Storyboard.Type='System.Windows.Media.Animation.Storyboard'; TargetElement='DevExpress.Xpf.Editors.ErrorControl: DevExpress.Xpf.Grid.GridCellValidationError'; TargetElement.HashCode='56844144'; TargetElement.Type='DevExpress.Xpf.Editors.ErrorControl'
    System.Windows.Media.Animation Warning: 6 : Unable to perform action because the specified Storyboard was never applied to this object for interactive control.; Action='SkipToFill'; Storyboard='System.Windows.Media.Animation.Storyboard'; Storyboard.HashCode='33003048'; Storyboard.Type='System.Windows.Media.Animation.Storyboard'; TargetElement='DevExpress.Xpf.Editors.ErrorControl: DevExpress.Xpf.Grid.GridCellValidationError'; TargetElement.HashCode='64558826'; TargetElement.Type='DevExpress.Xpf.Editors.ErrorControl'
    System.Windows.Media.Animation Warning: 6 : Unable to perform action because the specified Storyboard was never applied to this object …
Run Code Online (Sandbox Code Playgroud)

c# wpf binding hashcode storyboard

0
推荐指数
1
解决办法
1411
查看次数

如何确保hashcode()在Java中不能解析为相同的值?

我有一个类的哈希码实现,哈希码实现与eclipse生成的一致,也是这里讨论的最常被接受的实践

这是我的哈希码实现(此方法中使用的所有ID都构成了对象的键):

public int hashCode() {
    final int prime = 31;
    int hashCode = 1;
    if(uId != null){
        hashCode = prime * hashCode + uId.hashCode();
    }
    if(rId != null){
        hashCode = prime * hashCode + rId.hashCode();
    }
    if(bId != null){
        hashCode = prime * hashCode + bId.hashCode();
    }
    if(reId != null){
        hashCode = prime * hashCode + reId.hashCode();
    }
    if(cId != null){
        hashCode = prime * hashCode + cId.hashCode();
    }
    return hashCode;
}
Run Code Online (Sandbox Code Playgroud)

我遇到了一个使用非常大的数据集进行测试的场景,而且我的集合没有这个类的预期数量的对象.仔细观察下面两个数据集导致相同的哈希码:50268236873,因此一条记录被添加到集合中的最后一个替换,因为它们的哈希码是相同的.

  Existing record :
  Record@2c0781cd[uId=54046,rId=10967,bId=177,reId=1728,cId=50194] …
Run Code Online (Sandbox Code Playgroud)

java hash-code-uniqueness hashcode java-ee java-ee-7

0
推荐指数
1
解决办法
535
查看次数

当我尝试在C++中推送一个项目时,为什么会出现seg错误?

我正在为我的Data Structures类开发一个哈希表实验室.当我在insert函数中使用push_back()函数时,我一直在遇到一个设定的错误.但是,我不确定导致此错误的原因.

    using namespace std;

    HashTable::HashTable(int buckets) {
        this->buckets = buckets;
        vector<Entry>* table = new vector<Entry>[buckets];
    }


    Entry HashTable::insert(GameBoard board, int number) {
        int index = compress(board.hashCode());
        Entry entry = Entry(board, number);
        table[index].push_back(entry);
        return entry;
    }


    int HashTable::compress(int hashCode) {
        return (hashCode % buckets);
    }


    Entry::Entry(GameBoard board, int value) { 
        this->board = board; 
        this->value = value; 
    }


    int GameBoard::hashCode() {

        int hashVal = 0;

        for (int r = 0; r < DIMENSION; r++) {
            for (int c = 0; c …
Run Code Online (Sandbox Code Playgroud)

c++ hashtable hashmap hashcode

0
推荐指数
1
解决办法
298
查看次数

自定义对象作为 Map 键

正如我读过的那样,如果自定义对象需要成为地图的键,则需要覆盖 hashcode 和 equals 方法,但在我的情况下,它可以在不覆盖它的情况下工作。有人能告诉我出了什么问题吗?

    Map<Student,Integer> map = new HashMap<>();
    Student s1=new Student(1,"A");
    Student s2=new Student(2,"B");
    Student s3=new Student(3,"C");

    map.put(s1,1);
    map.put(s2,2);
    map.put(s1,3);

    for(Student s:map.keySet()) {
        System.out.println(map.get(s) + "->" + s.id + " " +s.name);
    }
Run Code Online (Sandbox Code Playgroud)

正确的输出:

3-> 1A

2-> 2 B

java collections equals hashmap hashcode

0
推荐指数
1
解决办法
877
查看次数

随机整数作为 hashCode

在构造函数中生成一个随机数并从hashCode方法返回这个值是个好主意吗?有可能发生冲突,但这适用于编写自己的hashCode方法。那么有什么缺点呢?在 HashMap 中使用此对象时,它将与随机数一起存储为散列,然后由相同的散列检索。如果有冲突,它们将被解决equals

java random hash hashcode

0
推荐指数
1
解决办法
497
查看次数

HashCode.Combine 是可交换的吗?

具体来说,是

HashCode.Combine<T1,T2>(T1, T2)
Run Code Online (Sandbox Code Playgroud)

可交换的? 即是真的吗

HashCode.Combine(a, b) == HashCode.Combine(b, a)
Run Code Online (Sandbox Code Playgroud)

对于任何 ab

c# hashcode

0
推荐指数
1
解决办法
71
查看次数