All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.icodegarden.nutrient.lang.algorithm.JavaStringFunction Maven / Gradle / Ivy

There is a newer version: 3.0.2
Show newest version
package io.github.icodegarden.nutrient.lang.algorithm;

/**
 * java 的string hash算法+seed
 * 
 * @author Fangfang.Xu
 *
 */
public class JavaStringFunction implements HashFunction {
	/**
	 * String类的seed默认是31
	 */
	private final int seed;

	/**
	 * 使用String默认的seed=31
	 */
	public JavaStringFunction() {
		this.seed = 31;
	}

	public JavaStringFunction(int seed) {
		this.seed = seed;
	}

	@Override
	public int hash(Object obj) {
		String value = obj.toString();
		
		int hash = 0;

		int h = hash;
		if (h == 0 && value.length() > 0) {
			for (int i = 0; i < value.length(); i++) {
				h = seed * h + value.charAt(i);
			}
			hash = h;
		}
		return h;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy