Java Hash Calculate hash(Object... as)

Here you can find the source of hash(Object... as)

Description

Create a hash code for a list of objects.

License

Educational Community License

Declaration

public static int hash(Object... as) 

Method Source Code

//package com.java2s;
/**/*  w w  w  .j ava  2 s .  c o  m*/
 *  Copyright 2009, 2010 The Regents of the University of California
 *  Licensed under the Educational Community 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.osedu.org/licenses/ECL-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 {
    /**
     * Create a hash code for a list of objects. Each of them may be null.
     * Algorithm adapted from "Programming in Scala, Second Edition", p670.
     */
    public static int hash(Object... as) {
        if (as == null)
            return 0;
        int hash = 0;
        for (Object a : as) {
            if (hash != 0)
                hash = 41 * hash + hash1(a);
            else
                hash = 41 + hash1(a);
        }
        return hash;
    }

    private static int hash1(Object a) {
        return a != null ? a.hashCode() : 0;
    }
}

Related

  1. hash(Object obj)
  2. hash(Object object)
  3. hash(Object v)
  4. hash(Object value)
  5. hash(Object value, int seed)
  6. hash(Object... value)
  7. hash(Object... values)
  8. hash(Object[] array)
  9. hash(Object[] array)