Java List Hash hashCode(List list)

Here you can find the source of hashCode(List list)

Description

Computes the hash code for a list per the contract defined by List#hashCode() .

License

Open Source License

Parameter

Parameter Description
list the list

Return

the hash code for list

Declaration

public static int hashCode(List<?> list) 

Method Source Code

//package com.java2s;

import java.util.List;

import java.util.Set;

public class Main {
    /**// www . ja v  a  2s.  com
     * Computes the hash code for a list per the contract defined by {@link List#hashCode()}.
     * 
     * @param list the list
     * @return the hash code for {@code list}
     */
    public static int hashCode(List<?> list) {
        return listHashCode(list);
    }

    /**
     * Computes the hash code for a set per the contract defined by {@link Set#hashCode()}.
     * 
     * @param set the set
     * @return the hash code for {@code list}
     */
    public static int hashCode(Set<?> set) {
        int hashCode = 0;
        for (Object item : set) {
            if (item != null) {
                // don't overflow stack if set contains itself -- substitute default hashcode
                hashCode += item == set ? System.identityHashCode(set) : item.hashCode();
            }
        }
        return hashCode;
    }

    private static int listHashCode(Iterable<?> l) {
        int ret = 1;
        for (Object e : l) {
            // don't overflow stack if list contains itself -- substitute default hashcode
            int elementHash = e == l ? System.identityHashCode(l) : (e == null ? 0 : e.hashCode());
            ret = 31 * ret + elementHash;
        }
        return ret;
    }
}

Related

  1. hashCode(List list)
  2. hashCodeForList(final Collection list)
  3. hashContents(List l)