Android examples for java.util:Map
hashCode function that properly handles arrays, collections, maps, collections of arrays, and maps of arrays.
/*//ww w.j a v a2 s . c o m * -------------------- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * Copyright 2008-2009 Sun Microsystems, Inc. All rights reserved. * * The contents of this file are subject to the terms of the Common Development * and Distribution License("CDDL") (the "License"). You may not use this file * except in compliance with the License. * * You can obtain a copy of the License at * http://opensource.org/licenses/cddl1.php * See the License for the specific language governing permissions and limitations * under the License. * * When distributing the Covered Code, include this CDDL Header Notice in each file * and include the License file at http://opensource.org/licenses/cddl1.php. * If applicable, add the following below this CDDL Header, with the fields * enclosed by brackets [] replaced by your own identifying information: * "Portions Copyrighted [year] [name of copyright owner]" * -------------------- */ import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; import java.util.SortedMap; import java.util.SortedSet; import java.util.TreeMap; import java.util.TreeSet; public class Main{ public static void main(String[] argv){ Object o = "book2s.com"; System.out.println(hashCode(o)); } /** * hashCode function that properly handles arrays, collections, maps, * collections of arrays, and maps of arrays. * * @param o * The object. May be null. * @return the hashCode */ public static int hashCode(Object o) { if (o == null) { return 0; } else if (o.getClass().isArray()) { int length = Array.getLength(o); int rv = 0; for (int i = 0; i < length; i++) { Object el = Array.get(o, i); rv += CollectionUtil.hashCode(el); } return rv; } else if (o instanceof Collection) { Collection<?> l = (Collection<?>) o; int rv = 0; for (Object el : l) { rv += CollectionUtil.hashCode(el); } return rv; } else if (o instanceof Map) { Map<?, ?> map = (Map<?, ?>) o; return CollectionUtil.hashCode(map.values()); } else { return o.hashCode(); } } }