Java Hash Code Calculate hashCode(final char[] text, final int textOffset, final int textLen)

Here you can find the source of hashCode(final char[] text, final int textOffset, final int textLen)

Description

hash Code

License

Apache License

Declaration

public static int hashCode(final char[] text, final int textOffset, final int textLen) 

Method Source Code

//package com.java2s;
/*// w w w  . j  ava2  s  .c om
 * =============================================================================
 * 
 *   Copyright (c) 2012-2014, The ATTOPARSER team (http://www.attoparser.org)
 * 
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 * 
 * =============================================================================
 */

public class Main {
    public static int hashCode(final char[] text, final int textOffset, final int textLen) {
        // This basically mimics what the String.hashCode() method does, without the need to
        // convert the char[] into a new String object
        // If the text to compute was already a String, it would be better to directly call
        // its 'hashCode()' method, because Strings cache their hash codes.
        // ---------------------------------------
        // NOTE: Even if relying on the specific implementation of String.hashCode() might seem
        //       a potential issue for cross-platform compatibility, the fact is that the
        //       implementation of String.hashCode() is actually a part of the Java Specification
        //       since Java 1.2, and its internal workings are explained in the JavaDoc for the
        //       String.hashCode() method.
        // ---------------------------------------
        int h = 0;
        int off = textOffset;
        for (int i = 0; i < textLen; i++) {
            h = 31 * h + text[off++];
        }
        return h;
    }

    public static int hashCode(final CharSequence text) {
        // This basically mimics what the String.hashCode() method does, without the need to
        // convert the CharSequence into a new String object (unless it is a String already,
        // in which case String.hashCode() is used directly.
        // If the text to compute was already a String, it would be better to directly call
        // its 'hashCode()' method, because Strings cache their hash codes.
        // ---------------------------------------
        // NOTE: Even if relying on the specific implementation of String.hashCode() might seem
        //       a potential issue for cross-platform compatibility, the fact is that the
        //       implementation of String.hashCode() is actually a part of the Java Specification
        //       since Java 1.2, and its internal workings are explained in the JavaDoc for the
        //       String.hashCode() method.
        // ---------------------------------------
        if (text instanceof String) {
            return text.hashCode();
        }
        int h = 0;
        final int textLen = text.length();
        for (int i = 0; i < textLen; i++) {
            h = 31 * h + text.charAt(i);
        }
        return h;
    }
}

Related

  1. hashCode(CharSequence seq)
  2. hashCode(double dbl)
  3. hashCode(double v)
  4. hashCode(double val)
  5. hashCode(final byte[] data)
  6. hashCode(final int i)
  7. hashCode(final int i)
  8. hashCode(final int seed, final int hashcode)
  9. hashCode(final int x, final int y)