Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

public class Main {
    /**
     * <p>
     * Calculates a 8-byte (64bit) hash from a {@link String}
     * </p>
     * 
     * @param string
     *            The string object to generate the 64bit hash value for
     * @return The 64bit long hash value for the provided string
     */
    public static long hash(final String string) {
        long h = 1125899906842597L; // prime
        int len = string.length();

        for (int i = 0; i < len; i++)
            h = 31 * h + string.charAt(i);
        return h;
    }

    /**
     * <p>
     * Calculates a 8-byte (64bit) hash from an object by invoking the objects
     * {@link Object#toString()} first and then generating the hash value for
     * the generated string value.
     * </p>
     * 
     * @param object
     *            The object whose key shall be calculated
     * @return The 64bit long hash value for the provided object
     */
    public static long hash(final Object object) {
        return hash(object.toString());
    }
}