dave08
02/07/2024, 2:56 PM==
check a set's content?Youssef Shoaib [MOD]
02/07/2024, 3:37 PMhashCode
probably, which speeds things up.dave08
02/07/2024, 4:12 PMdave08
02/07/2024, 4:14 PMCLOVIS
02/07/2024, 4:31 PMCLOVIS
02/07/2024, 4:31 PMYoussef Shoaib [MOD]
02/07/2024, 4:32 PMCLOVIS
02/07/2024, 4:32 PMI guess you mean hashCode on each elementThat's going to depend on the Set implementation; the right-hand-side set's
containAll
is the one that decides. HashSet
will use the hash to compare, but some other implementations like EnumSet
won't.CLOVIS
02/07/2024, 4:39 PMSet.equals
does it for you.dave08
02/07/2024, 4:48 PMThat's going to depend on the Set implementation; the right-hand-side set'sAnd I guess kotlin'sis the one that decides.containAll
will use the hash to compare, but some other implementations likeHashSet
won't.EnumSet
setOf(..)
uses a LinkedHashSet (it seems to be expect in https://github.com/JetBrains/kotlin/blob/d39a7e59a75a464c656af6bed9718c427e91f236/libraries/stdlib/src/kotlin/collections/Sets.kt#L54? And listOf()
would also be the same?CLOVIS
02/07/2024, 4:48 PMsetOf
: yes
listOf
: well, no, it's not a Set
, so it will never be equal to a Set
(there's a shortcut that tests the type)dave08
02/07/2024, 4:49 PMCLOVIS
02/07/2024, 4:49 PMCLOVIS
02/07/2024, 4:49 PMequals
directly on all elementsCLOVIS
02/07/2024, 4:50 PMhashCode
here may be a valuable performance improvement. There's a chance it may break code when hashCode
is not implemented properlyCLOVIS
02/07/2024, 4:51 PMdave08
02/07/2024, 4:52 PMso it shouldn't matter muchyou don't mean performance?
Youssef Shoaib [MOD]
02/07/2024, 4:53 PMCLOVIS
02/07/2024, 4:53 PMCLOVIS
02/07/2024, 4:54 PMI think equals is expected to perform a hashcode comparison usually btw.At the very least, having an
hashCode
that doesn't match equals
is considered a bug, even if it's not called, so maybe we can say that all code broken by this was already broken.Youssef Shoaib [MOD]
02/07/2024, 4:54 PMHashSet
uses hashcode directly is because it already deals with them to have fast access to elements. Otherwise it would've used equals I guess. I think the expectation is that equals returns false pretty fast for most things.Youssef Shoaib [MOD]
02/07/2024, 4:58 PMequals
include the same quickly-returning-false checks. I might completely wrong though; this is outside my realm of knowledgeCLOVIS
02/07/2024, 4:59 PMI figured it was more to allow some way of associating a number to an object easily, thus allowing for hash tables.I think you're right
I think the onus is usually on the developer to have theirIt's the first time I hear of this, though. I've never seen aninclude the same quickly-returning-false checks. I might completely wrong though; this is outside my realm of knowledgeequals
equals
call use hashCode
internally except containers (sets…).CLOVIS
02/07/2024, 5:00 PMequals
implementation call the same class' hashCode
.dave08
02/08/2024, 10:29 AM