todd.ginsberg
12/04/2018, 7:02 PMkarelpeeters
12/05/2018, 8:42 AMm.getValue('f') instead of m['f'] for the default to work.CypherK
12/05/2018, 8:49 AMwithDefault is that I don't CARE if the key is in the map or not, I just want all keys to deliver a value, regardless.
If I wanted to have to think about whether or not a key may or may not be in there and change all my map accesses to getValue, accordingly ... why bother? I could just as well use getOrDefault.karelpeeters
12/05/2018, 8:51 AM[] work would vialolate the Map contract. What should contains do? keySet()? valueSet(), how does it know whether all possible keys are in there already? How should iteration work?CypherK
12/05/2018, 8:57 AMcontains, keySet, valueSet, etc?karelpeeters
12/05/2018, 8:57 AMx !in map then map[x] should return null.karelpeeters
12/05/2018, 8:57 AMcontains means.CypherK
12/05/2018, 9:21 AMprivate fun implFindEntry(key: K): Map.Entry<K, V>? = entries.firstOrNull { it.key == key }
for its containsKey.
Likewise,
override fun containsValue(value: @UnsafeVariance V): Boolean = entries.any { it.value == value }
doesn't have to deal with get either.
Now the issue seems to be that the internal containsEntry ...
val ourValue = get(key)
if (value != ourValue) {
return false
}
if (ourValue == null && !containsKey(key)) {
return false
}
does an optimization in that it only checks containsKey if the value is null. This could be rewritten to
val ourValue = get(key)
if (value != ourValue) {
return false
}
if (!containsKey(key)) {
return false
}
in the .withDefault implementations, and voilà, "problem" solved.
Since containsEntry is only ever used in the equals method, I'm willing to argue the extra overhead of having to search for keys with associated non-null values IF the value returned by get matches the expected value is negligible.karelpeeters
12/05/2018, 9:23 AMx in map to return for a map with default?karelpeeters
12/05/2018, 9:23 AMkeySet() to return?CypherK
12/05/2018, 9:25 AMSet of entries ...
x in map if that set contains a corresponding entry
keySet() just iterate over the entries.karelpeeters
12/05/2018, 9:27 AMCypherK
12/05/2018, 9:46 AMCypherK
12/05/2018, 9:46 AMkarelpeeters
12/05/2018, 9:58 AMMap specification:
https://docs.oracle.com/javase/7/docs/api/java/util/Map.html#containsKey(java.lang.Object)
Returns true if this map contains a mapping for the specified key.https://docs.oracle.com/javase/7/docs/api/java/util/Map.html#get(java.lang.Object)
Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
karelpeeters
12/05/2018, 9:59 AMcontains and get must respect it.todd.ginsberg
12/05/2018, 1:50 PMCypherK
12/05/2018, 2:33 PMkarelpeeters
12/05/2018, 2:34 PMDefaultMap that doesn't implement Map and does everything you wan of course.CypherK
12/05/2018, 2:42 PMCypherK
12/05/2018, 2:43 PMkarelpeeters
12/05/2018, 2:47 PMCypherK
12/05/2018, 2:51 PM