I’m writing a peer to peer application and I need a hash function for storing IP/port pairs in a java hashset. Rather than re-invent the wheel I wondered if there are already solutions out there but google has not yielded much.
Can anyone recommend a hash function for IPv4 (bonus if it works for IPv6 as well!) and a remote port number?
The port number is likely to be the same unless the client is on the same host in which case it will be sequential.
The
String.hashCode()is pretty reasonable. I would simply do this:It is “random” enough for coding purposes, so much so that it is relied upon by much of the JDK API.
Remember this mantra… “less code is good”