标签: hashcode

按第一个列表的键合并两个列表

我的主要问题描述如下:

我有两个BankAccount对象列表.A BankAccount具有诸如BankCode并且AccountNumber唯一标识帐户的属性.因此,无论该列表可能包含同一个银行帐户,但他们可能有他们Source,AmountAccountTypes有所不同.

这里的目的是合并这两个列表:

  1. 如果帐户在第二个列表中可用(但不在第一个列表中),则将帐户添加到第一个列表中.
  2. 如果两个列表中的银行帐户相同,请使用第二个列表中(匹配的)银行帐户的详细信息更新第一个列表中银行帐户的详细信息.

我试过实现一篇SO帖子中提到的解决方案.我去尝试在.NET代码填充网站上编写代码.但是在尝试执行行号后,我无法获得输出.93我评论过.

class BankAccount
{
    public string BankCode{get;set;}
    public string AccountNumber{get;set;}
    public string AccountType{get;set;}
    public string Amount{get;set;}
    public string Source{get;set;}

    public override bool Equals(object obj)
    {
      var acc = obj as BankAccount;
      return Equals(acc);
    }

    public override int GetHashCode()
    {
      return this.GetHashCode();
    }

    public bool Equals(BankAccount acc2)
    {
      if(acc2 == null) return false;
      if(string.IsNullOrEmpty(acc2.BankCode)) return false;
      if(string.IsNullOrEmpty(acc2.AccountNumber)) return false;
      return …
Run Code Online (Sandbox Code Playgroud)

.net c# hashcode

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

java hashset中项的相等性

我需要为一个Item实现equals()方法,该方法可以放在它的Maker的hashset中.Item可以有如下字段

class Item{
    private String isbn;
    private String name;
    private double price;
...
}

class Maker{
    private String name;
    private Set<Item> items;
    public Maker() {
        super();
        items = new HashSet<Item>();
    }
...
}
Run Code Online (Sandbox Code Playgroud)

如果我通过比较三个字段来实现equals并根据这些字段写一个hashCode(),那么我会得到错误的结果

1.add item to hashset
2.modify the price of item
3.try to find if item exists in hashset


@Override
public boolean equals(Object o){
    if(o == this){
        return true;
    }
    if (!(o instanceof Item)){
        return false;
    }
    Item a = (Item)o;
    if(hasSameName(a) && hasSameIsbn(a) && hasSamePrice(a)){
        return true; …
Run Code Online (Sandbox Code Playgroud)

java equals hashcode

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

Java Arrays.hashcode()奇怪的行为

我有一个对象有一个字段 - double [] _myField它的hashcode是

public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + Arrays.hashCode(_myField);
    return result;
}
Run Code Online (Sandbox Code Playgroud)

但是,如果我将对象用作Map中的键,我会得到以下奇怪的行为:

for (Map.Entry<MyObject, String> entry: _myMap.entrySet())
    {
         if (entry.getValue() != _myMap.get(entry.getKey()))
         {
                 System.out.println("found the problem the value is null");

         }

    }
Run Code Online (Sandbox Code Playgroud)

我能想到上述IF语句为真的唯一原因是我得到了一个不同的密钥哈希码.

事实上,我已经改变了哈希码函数,在所有情况下都返回1.效率不高,但有利于调试,实际上IF语句总是错误的.

Arrays.hashcode()有什么问题?

请注意(在阅读一些注释后):1)至于在IF语句中使用!=,它确实比较了引用,但在上面的情况下它应该是相同的.无论如何奇怪的是右手边返回NULL 2)至于发布Equals功能.当然我已经实现了它.但这无关紧要.在调试中跟踪代码表明只调用了哈希码.原因可能是奇怪的是,返回的哈希码与原始哈希码不同.在这种情况下,Map找不到匹配的条目,因此不需要调用Equals.

java arrays hashcode

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

为什么String哈希值会发生变化?

我有这个代码:

class DocumentIdentifier
  attr_reader :folder, :name

  def initialize( folder, name )
    @folder = folder
    @name = name
  end

  def ==(other)
    return true if other.equal?(self)
    return false unless other.kind_of?(self.class)
    folder == other.folder && name == other.name
  end

  def hash
    folder.hash ^ name.hash
  end

  def eql?(other)
    return false unless other.instance_of?(self.class)
    other.folder == folder && other.name == name
  end
end

first_id = DocumentIdentifier.new('secret/plans', 'raygun.txt')
puts first_id.hash
Run Code Online (Sandbox Code Playgroud)

为什么每次调用的哈希码都在变化?

我认为它应该与Java中的String哈希代码保持一致.或者,哈希码正在改变,因为每次调用都会给我新的实例foldername?Ruby的String类型具有hash方法的实现,因此相同的String应该为每个调用提供相同的数字.

ruby hashcode

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

在Java中覆盖域类的equal()和hashCode()方法

我有一个名为的域类Subscriber,它的定义是这样的:

public class Subscriber {
   private long id;
   private String email;
   private String subscriberName;
   private Topic subscribingTopic;

   //other attributes and getters setters.
}

public class Topic{
   private long id;
   private String topicName; //unique
}
Run Code Online (Sandbox Code Playgroud)

我的问题是我需要覆盖此Subscriber类的equal()和hashCode()方法.覆盖equal()是一项简单的任务(只是比较基本属性,在这种情况下有三个).但是我在覆盖hashCode()方法时遇到了问题.我如何编写hashCode(),我可以信任安全地使用hibernate,同时管理我的域.我可以信任IDE生成的吗?

任何帮助将不胜感激,并提前感谢!

java hibernate hashcode java-ee

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

覆盖hashCode()不起作用

我正在尝试Point使用HashSet 使类正常工作.这是我的Point类:

class Point {

    int x;
    int y;

    Point(int x, int y) {
        x = x;
        y = y;
    }

    @Override
    public int hashCode() {
        int hash = 1;
        hash = hash * 17 + x;
        hash = hash * 31 + y;
        return hash;
    }

    @Override
    public boolean equals(Object o) {
        if (o == null) {
            return false;
        }
        Point p = (Point) o;
        return x == p.x && y == p.y;
    }
}
Run Code Online (Sandbox Code Playgroud)

当我测试它并做

    HashSet<Point> …
Run Code Online (Sandbox Code Playgroud)

java equals hashcode hashset

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

Long hashCode为java中的不同对象返回相同的数字

假设我有这种情况

    Long id = -1L;
    System.out.println( id.hashCode() );

    id = 0L;
    System.out.println( id.hashCode() );
Run Code Online (Sandbox Code Playgroud)

你猜怎么着?两个输出都给出相同的数字(0)!我的问题是:

  1. 为什么会这样?
  2. 我怎么能省略这个并计算0和-1的正确哈希值?

在此先感谢回复:)

java hashcode

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

在JAVA中设置不正确

我只是想从列表中删除重复的元素.为此,我写了一个POJO类Student:

class Student{

private String roll;
private String name;

public Student(String roll, String name) {
    this.roll = roll;
    this.name = name;
}
@Override
public boolean equals(Object obj) {
    Student st = (Student) obj;
    return st.getRoll().equals(roll);
}

public String getRoll() {
    return roll;
}
public void setRoll(String roll) {
    this.roll = roll;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
@Override
public String toString() {
    return roll ;
}

} …
Run Code Online (Sandbox Code Playgroud)

java collections set hashcode hashset

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

Java HashMap没有从key获得价值

当我试图从具有一条记录的hashmap中获取错误时,我一直变为空.最后我用以下方式测试它:

Iterator<Position> it = pieces.keySet().iterator();
    while (it.hasNext()){
        System.out.println("object from key >> " + pieces.get(it.next()));
    }
    Iterator<Piece> itt = pieces.values().iterator();
    while (itt.hasNext()){
        System.out.println("direct object >> " + itt.next().getPosition());
    }
Run Code Online (Sandbox Code Playgroud)

我得到的输出是:

object from key >> null
direct object >> application.Position@37
Run Code Online (Sandbox Code Playgroud)

我已经显示的代码被使用,因为它之间没有任何其他东西.

关于位置对象,我重写了hashCode()函数,以根据Position类的值返回hashCode.因此,当对象中的变量发生变化时,HashCode会发生变化.上面的代码在位置对象的值更改之前很有效.但是一旦我改变了,我就会通过密钥获得空值.

java hash dictionary hashmap hashcode

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

System.identityHashCode()对原语的行为

jvm imgui中,我正在使用

System.identityHashCode(i++)

哪里

var i = 0

为每个帧始终为给定对象生成一个常数id(因此
可以对其进行跟踪)

但是,一个用户案例只是告诉我,这仅对中的值有效[0, 125]

尝试调试并查找错误,我结束了这段简短的代码测试:

    var i = 0
    val A = Array(256, { System.identityHashCode(i++) })
    i = 0
    val B = Array(256, { System.identityHashCode(i++) })
    repeat(256) {
        if (A[it] != B[it])
            println("[$it] different, A ${A[it]}, B ${B[it]}")
    }
Run Code Online (Sandbox Code Playgroud)

还有:

  • 字节(完全正常工作,对于所有256个值,A == B)
  • 短裤(不能从128开始工作)
  • 整数(从128开始无效)
  • 多头(不能从128开始工作)
  • 浮动(完全不起作用)
  • 加倍(根本不起作用)

这是为什么?

我是否可以安全地假设这种行为在其他平台上也能保持一致?

java primitive hashcode kotlin

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

标签 统计

hashcode ×10

java ×8

equals ×2

hashset ×2

.net ×1

arrays ×1

c# ×1

collections ×1

dictionary ×1

hash ×1

hashmap ×1

hibernate ×1

java-ee ×1

kotlin ×1

primitive ×1

ruby ×1

set ×1