3 ms·
I see. So as an example of doing both, consider following Java pseudocode (simples example that comes to my mind now): Foo value = someCondition()
by TeMPOraL 9y ago
I see.
So as an example of doing both, consider following Java pseudocode (simples example that comes to my mind now):
Foo value = someCondition()
? map1.computeIfAbsent("key", defaultValueProvider)
: map2.computeIfAbsent("key", defaultValueProvider);
This both gets a value and changes state of the map; which map is queried/altered is determined by the condition. I personally don't mind such code, and prefer it to if/else version.
- Mithaldu 9y agoShouldn't that be: Foo value = ( someCondition() ? map1 : map2 ).computeIfAbsent("key", defaultValueProvider); Or some kind of moral equivalent?
- TeMPOraL 9y agoCould be, though this is an artifact of a simplified example that I didn't mean to be relevant here. For clarity, let's rewrite it as: Foo value = someCondition() ? map1.computeIfAbsent("key", defaultValueProvider) : list1.get(42);