Java Hash String hash(String s)

Here you can find the source of hash(String s)

Description

Generates a hash number according with a given string

License

GNU General Public License

Parameter

Parameter Description
s the string representing the key

Return

the hash value of string

Declaration

public static int hash(String s) 

Method Source Code

//package com.java2s;
/*/*from  w w w .  j a v a 2s  . c o  m*/
 * Title:        Mobile CloudSim Toolkit
 * Description:  Extension of CloudSim Toolkit for Modeling and Simulation of Publish/Subscribe 
 *              Communication Paradigm with Subscriber Connectivity Change
 * Licence:      GPL - http://www.gnu.org/copyleft/gpl.html
 *
 * Copyright (c) 2014-2016, Universidade Federal de Goi?s, Brazil
 */

public class Main {
    private static int hashSuffix = 1000;

    /**
     * Generates a hash number according with a given string
     * 
     * @param s
     *            the string representing the key
     * @return the hash value of string
     */
    public static int hash(String s) {
        int h = 0;

        // The hash value is the number the string represents or a function
        // using ASCII value
        try {
            h = Integer.parseInt(s);
        } catch (NumberFormatException e) {
            for (int i = 0; i < s.length(); i++) {
                h += s.charAt(i) - 48; // Don't use the ASCII value but the real
                // value
            }
        }
        // To have different values we concatenate the hash value if current
        // time
        String hr = String.valueOf(h) + String.valueOf(hashSuffix++);
        return Integer.parseInt(hr);
    }
}

Related

  1. hash(String data)
  2. hash(String data)
  3. hash(String name)
  4. hash(String password)
  5. hash(String s)
  6. hash(String s, int start, int end)
  7. hash(String src)
  8. hash(String str, int max)
  9. hash(String str, int offset)