(hash-unordered-coll coll)
Returns the hash code, consistent with =, for an external unordered collection implementing Iterable. For maps, the iterator should return map entries whose hash is computed as (hash-ordered-coll [k v]). See http://clojure.org/data_structures#hash for full algorithms.
;;;; Clojure's (hash-unordered-coll) ALWAYS produces the same hash code EVEN:
;;;; (1) for different collection types
;;;; (2) for differently ordered collections of the same type
;;;; AS LONG AS the collections contain the same elements
(hash-unordered-coll #{1 2})
;;=> 460223544
(hash-unordered-coll (sorted-set 1 2))
;;=> 460223544
(hash-unordered-coll (doto (new java.util.HashSet) (.add 1) (.add 2)))
;;=> 460223544
(hash-unordered-coll (doto (new java.util.TreeSet) (.add 1) (.add 2)))
;;=> 460223544
(hash-unordered-coll [1 2])
;;=> 460223544
(hash-unordered-coll [2 1])
;;=> 460223544
(hash-unordered-coll (doto (new java.util.ArrayList) (.add 1) (.add 2)))
;;=> 460223544
(hash-unordered-coll (doto (new java.util.ArrayList) (.add 2) (.add 1)))
;;=> 460223544
;;;; On the other hand: both Clojure's (hash) and Java's .hashCode()
;;;; CAN (and often WILL) produce different hash codes:
;;;; (1) for different collection types
;;;; (2) for differently ordered collections of the same type
;;;; EVEN if the collections contain the same elements
(hash #{1 2})
;;=> 460223544
(hash (sorted-set 1 2))
;;=> 460223544
(hash (doto (new java.util.HashSet) (.add 1) (.add 2)))
;;=> 3
(hash (doto (new java.util.TreeSet) (.add 1) (.add 2)))
;;=> 3
(hash [1 2])
;;=> 156247261
(hash [2 1])
;;=> -1994590503
(hash (doto (new java.util.ArrayList) (.add 1) (.add 2)))
;;=> 994
(hash (doto (new java.util.ArrayList) (.add 2) (.add 1)))
;;=> 1024
(.hashCode #{1 2})
;;=> 3
(.hashCode (sorted-set 1 2))
;;=> 3
(.hashCode (doto (new java.util.HashSet) (.add 1) (.add 2)))
;;=> 3
(.hashCode (doto (new java.util.TreeSet) (.add 1) (.add 2)))
;;=> 3
(.hashCode [1 2])
;;=> 994
(.hashCode [2 1])
;;=> 1024
(.hashCode (doto (new java.util.ArrayList) (.add 1) (.add 2)))
;;=> 994
(.hashCode (doto (new java.util.ArrayList) (.add 2) (.add 1)))
;;=> 1024
;;;;
;;;; Only accepts implementations of java.lang.Iterable
;;;;
(hash-unordered-coll true)
;;=> ClassCastException java.lang.Boolean cannot be cast to java.lang.Iterable
(hash-unordered-coll 1)
;;=> ClassCastException java.lang.Long cannot be cast to java.lang.Iterable
(hash-unordered-coll \\c)
;;=> ClassCastException java.lang.Character cannot be cast to java.lang.Iterable
;;;;
;;;; Being seqable is not sufficient!
;;;;
(hash-unordered-coll "12")
;;=> ClassCastException java.lang.String cannot be cast to java.lang.Iterable
(hash-unordered-coll (int-array [1 2]))
;;=> ClassCastException [I cannot be cast to java.lang.Iterable
(hash-unordered-coll nil)
;;=> NullPointerException