Does .equals on Hashset use true regardless of order?

For Hashset in java, there is a .equals method comparing the elements in each set. Will this return true regardless of order?

An example, say, we have one set with elements {a, b, c} and another with elements {b, c, a}

if you use .equals on these two sets, will it return true or should it be sorted?

+5
source share
5 answers

This should return true. The documentation states:

. true , , . , equals Set.

+6

HashSet Set ( ), , . equals Set .

+1

Java HashSet - . . {a,b,c} {b,c,a}. , HashSet AbstractSet#equals(Object), :

. true, , , . , equals Set.

+1

, , Set .

Set, TreeSet.

, - . , , "", -.

This means that the collection only needs to return to more time-consuming methods in the unlikely event of hash code colonization.

+1
source

A hash is essentially an unordered list, so yes,

if hash A contains {1,2,3,4,5}, and hash B contains {3,1,5,4,2}, then they will be equal.

0
source

All Articles