Java tutorial
//package com.java2s; import java.util.List; import java.util.Set; public class Main { 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; } /** * 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; } }