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