1

I am reading other people's coe and there is an object that implements a java.io.Serializable function as such:

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;
  }

}

What is the hash function that was called when this.first.hashCode() is hashed? Is it md5?

Just a side question. What does Pair<F,S> mean? Why was there an angled bracket.

My task is to reimplement the java class into a python object and now i'm using md5 to hash the code but i'm not sure whether it's the same. i.e. :

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
alvas
  • 115,346
  • 109
  • 446
  • 738

1 Answers1

2

First your title question.

Which hash function does java.io.Serializable uses?

Serializable does not use any hash function. It allows a java object to be stored (Serialized) and retrieved (de-Serialized) later. Read serialization uses here.

What is the hash function that was called when this.first.hashCode() is hashed? Is it md5?

Hashcode is a concept used in object comparison and object search in collections. Python has similar concept __hash__, read it. It is a thumbprint for an object,an int. Hashcode has conceptual similarity to MD5 hashing but they are used for different purposes.

What does Pair mean? Why was there an angled bracket.

These are Java generics implementation. In strongly typed languages, generics helps compiler to check that methods and other features exists in classes. Since you are aiming to convert this class to python,a weakly typed language, these checks will be done in run-time.

Community
  • 1
  • 1
Atilla Ozgur
  • 14,339
  • 3
  • 49
  • 69