Skip to content

Instantly share code, notes, and snippets.

@tpeng
Created August 28, 2012 09:17
Show Gist options
  • Save tpeng/3496464 to your computer and use it in GitHub Desktop.
Save tpeng/3496464 to your computer and use it in GitHub Desktop.
A simple minhash implementation in Java.
import com.google.common.hash.HashFunction;
import com.google.common.hash.Hashing;
/**
* A simple MinHash implementation inspired by https://github.com/jmhodges/minhash
*
* @author tpeng ([email protected])
*/
public class MinHash {
private HashFunction hash = Hashing.murmur3_32();
public String hash(String string) {
int min = Integer.MAX_VALUE;
for (int i=0; i<string.length(); i++) {
int c = string.charAt(i);
int n = hash.hashInt(c).asInt();
if (n < min) {
min = n;
}
}
return Integer.toHexString(min);
}
public static void main(String... args) {
MinHash minHash = new MinHash();
System.out.println(minHash.hash("abc"));
System.out.println(minHash.hash("abcd"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment