java.io.Serializable使用哪个哈希函数?

时间:2014-11-28 09:46:46

标签: java python serialization hash

我正在阅读其他人的系数,并且有一个对象实现了java.io.Serializable函数:

import java.io.Serializable;

public class Pair<F, S> implements Serializable{

  private final F first;
  private final S second;

  public Pair(F f, S s)  {
    this.first = f;
    this.second = s;
  }

  public int hashCode()
  {
    int hashFirst = this.first != null ? this.first.hashCode() : 0;
    int hashSecond = this.second != null ? this.second.hashCode() : 0;

    return (hashFirst + hashSecond) * hashSecond + hashFirst;
  }



public boolean equals(Object obj)
  {
    if ((obj instanceof Pair))
    {
      Pair<?, ?> other = (Pair)obj;

      return ((this.first == other.first) || ((this.first != null) && (other.first != null) && (this.first.equals(other.first)))) && ((this.second == other.second) || ((this.second != null) && (other.second != null) && (this.second.equals(other.second))));
    }
    return false;
  }

}

this.first.hashCode()进行哈希处理时调用的哈希函数是什么?是md5吗?

只是一个附带问题。 Pair<F,S>是什么意思?为什么会有一个有角度的支架。

我的任务是将java类重新实现为python对象,现在我使用md5来散列代码,但我不确定它是否相同。即:

import md5

class Pair:
    serialVersionUID = 4496923633090911746L
    def __init__(self, f, s):
        self.f = f
        self.s = s

    def hashCode(self):    
        hashFirst = 0 if self.f else self.f.__hash__()
        hashSecond = 0 if self.f else self.s.__hash__()
        return (hashFirst + hashSecond) * hashSecond + hashFirst

    def equals(self, other):
        if isinstance(Pair, other):
            first_is_same_object = self.f is other.f
            second_is_same_object = self.s is other.s 

            first_is_same_value = self.f == other.f 
            second_is_same_value = self.s == other.s

            no_none_value = None not in [self.f, other.f, self.s, other.s]

            same_value = no_none_value and second_is_same_value and first_is_same_value
            same_object = first_is_same_object and second_is_same_object

            return same_value or same_object
        else:
            return False

1 个答案:

答案 0 :(得分:2)

首先是你的标题问题。

  

java.io.Serializable使用哪个哈希函数?

Serializable不使用任何哈希函数。它允许稍后存储(序列化)和检索(解序列化)java对象。阅读serialization使用此处。

  

对this.first.hashCode()进行哈希处理时调用的哈希函数是什么?是md5吗?

Hashcode是集合中的对象比较和对象搜索中使用的概念。 Python有类似的概念__hash__,请阅读它。它是一个对象的指纹,一个int。 Hashcode与MD5哈希具有概念上的相似性,但它们用于不同的目的。

  

对是什么意思?为什么会有一个有角度的支架。

这些是Java generics implementation。在强类型语言中,泛型有助于编译器检查类中是否存在方法和其他功能。由于您的目标是将此类转换为python(弱类型语言),因此这些检查将在运行时完成。

相关问题