Java List Hash hashCodeForList(final Collection list)

Here you can find the source of hashCodeForList(final Collection list)

Description

Generates a hash code using the algorithm specified in java.util.List#hashCode() .

License

Apache License

Parameter

Parameter Description
list the list to generate the hashCode for, may be null

Return

the hash code

Declaration

public static int hashCodeForList(final Collection list) 

Method Source Code

//package com.java2s;
/*//from ww  w. j  a va 2s .co m
 *  Copyright 2001-2004 The Apache Software Foundation
 *
 *  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.
 */

import java.util.Collection;

import java.util.Iterator;

public class Main {
    /**
     * Generates a hash code using the algorithm specified in 
     * {@link java.util.List#hashCode()}.
     * <p>
     * This method is useful for implementing <code>List</code> when you cannot
     * extend AbstractList. The method takes Collection instances to enable other
     * collection types to use the List implementation algorithm.
     * 
     * @see java.util.List#hashCode()
     * @param list  the list to generate the hashCode for, may be null
     * @return the hash code
     */
    public static int hashCodeForList(final Collection list) {
        if (list == null) {
            return 0;
        }
        int hashCode = 1;
        Iterator it = list.iterator();
        Object obj = null;

        while (it.hasNext()) {
            obj = it.next();
            hashCode = 31 * hashCode + (obj == null ? 0 : obj.hashCode());
        }
        return hashCode;
    }
}

Related

  1. hashCode(List list)
  2. hashCode(List list)
  3. hashContents(List l)