Java Hash Code Calculate hashCode(Object obj)

Here you can find the source of hashCode(Object obj)

Description

hash Code

License

Open Source License

Declaration

public static int hashCode(Object obj) 

Method Source Code

//package com.java2s;
/*// w ww.ja va2 s .  c o  m
* Copyright (c) 2015, Alachisoft. All Rights Reserved.
*
* 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(Object obj) {
        if (obj instanceof String) {
            char[] array = ((String) obj).toCharArray();
            int[] intArray = new int[(int) Math.ceil((double) (array.length) / 2)];

            for (int i = 0, j = 0; i < intArray.length; i++) {
                char[] toInt = new char[2];
                if (j < array.length) {
                    toInt[0] = array[j++];
                }
                if (j < array.length) {
                    toInt[1] = array[j++];
                }
                intArray[i] = charToInt(toInt);
            }

            int num = 0x15051505;
            int num2 = num;

            for (int i = array.length, j = 0; i > 0; i -= 4, j += 2) {

                num = (((num << 5) + num) + (num >> 0x1b)) ^ intArray[j];
                if (i <= 2) {
                    break;
                }
                num2 = (((num2 << 5) + num2) + (num2 >> 0x1b)) ^ intArray[j + 1];
            }
            return (num + (num2 * 0x5d588b65));
        } else
            return obj.hashCode();
    }

    private static int charToInt(char[] array) {
        return (array[0] | (array[1] << 16));
    }
}

Related

  1. hashCode(Object obj)
  2. hashcode(Object obj)
  3. hashCode(Object obj)
  4. hashCode(Object obj)
  5. hashCode(Object obj)
  6. hashCode(Object object)
  7. hashCode(Object object)
  8. hashCode(Object object)
  9. hashCode(Object object)