Java Hash String hashString(String str)

Here you can find the source of hashString(String str)

Description

hash String

License

Open Source License

Declaration

public static double hashString(String str) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    private static final int HASH_STRING_CHARS = 3;
    private static final int HASH_STRING_BASE = 256;

    public static double hashString(String str) {
        if (str == null)
            return 0;
        // System.out.println("STRING FOR HASH: " + str);
        double hashStringVal = 0.0;

        if (str.length() >= 3) {

            char[] hashChars = new char[HASH_STRING_CHARS];

            for (int i = 0; i < HASH_STRING_CHARS; i++) {
                hashChars[i] = str.charAt(i);
                // System.out.println(hashChars[i]);
            }/*from  w w w.java  2 s . co  m*/

            for (int i = 0; i < HASH_STRING_CHARS; i++) {
                // System.out.println(hashChars[i] + " | " +
                // (double)((int)hashChars[i]) + " | " +
                // Math.pow((double)HASH_STRING_BASE, (double)i));

                hashStringVal += (double) ((int) hashChars[i])
                        * Math.pow((double) HASH_STRING_BASE, (double) (HASH_STRING_CHARS - i));

            }

            return hashStringVal;
        } else {

            char[] hashChars = new char[str.length()];

            for (int i = 0; i < str.length(); i++)
                hashChars[i] = str.charAt(i);

            for (int i = 0; i < str.length(); i++) {
                // System.out.println(hashChars[i] + " | " +
                // (double)((int)hashChars[i]) + " | " +
                // Math.pow((double)HASH_STRING_BASE, (double)i));

                hashStringVal += (double) ((int) hashChars[i])
                        * Math.pow((double) HASH_STRING_BASE, (double) (HASH_STRING_CHARS - i));

            }

            return hashStringVal;

        }

    }
}

Related

  1. hashString(String data, int seed)
  2. hashString(String s)
  3. hashString(String s)
  4. hashString(String s)
  5. hashString(String str)
  6. hashTermToString(int i, int h)
  7. hasHtmlTag(String content)
  8. hasHTMLTag(String html)
  9. hasHTMLTag(String text)