Page 1 of 1

About Question enthuware.ocpjp.v8.2.1833 :

Posted: Sat May 13, 2017 1:03 pm
by lenalena
I understand the question and the answer, but I'm a bit confused by functionality of compute (all 3 flavors). If the BiFunction that's passed as a parameter returns null, then the mapping, if it existed, is removed. But doesn't that contradict the the property of allowing null values for those implementations of Map that do? If you can call map.put (key, null) (on a HashMap, for example), shouldn't compute also accept null as BiFubction return value? What happens if you had a valid null in the map - if compute runs on that mapping and BiFunction returns null, that valid null mapping that was originally in the map will be removed. Why is it implemented like that? Why not just set the value to null for that key for that map? And the implementations that do not support nulls (like ConcurrentHashMap) can do whatever they usually do when one attempts to insert a null.

Re: About Question enthuware.ocpjp.v8.2.1833 :

Posted: Sat May 13, 2017 10:44 pm
by admin
lenalena wrote:I understand the question and the answer, but I'm a bit confused by functionality of compute (all 3 flavors). If the BiFunction that's passed as a parameter returns null, then the mapping, if it existed, is removed. But doesn't that contradict the the property of allowing null values for those implementations of Map that do? If you can call map.put (key, null) (on a HashMap, for example), shouldn't compute also accept null as BiFubction return value? What happens if you had a valid null in the map - if compute runs on that mapping and BiFunction returns null, that valid null mapping that was originally in the map will be removed. Why is it implemented like that? Why not just set the value to null for that key for that map? And the implementations that do not support nulls (like ConcurrentHashMap) can do whatever they usually do when one attempts to insert a null.
Yes, that is correct. Even if a particular implementation of Map supports null values, then if the function returns null, instead of associating the key with null, the key will be removed. I don't think anyone other than the API designers can give the exact reason but I believe they chose a behavior that they thought will satisfy most use cases.
You can contact Oracle support and get more clarification. You can also open a feature request on their support site.

HTH,
Paul.

Re: About Question enthuware.ocpjp.v8.2.1833 :

Posted: Fri May 19, 2017 6:22 am
by ted.samuelsson
Hi,

There is something I don't quite understand.

The BiFunction is declared as: BiFunction<String, Account, Account> bif = (a1, a2)-> a2 instanceof BankAccount?new BankAccount(a1, 300.0):new Account(a1);

Unless I'm mistaken a1 and a2 are of type Account (otherwise a2 instanceof BankAccount always fail), and the answer marked as correct wouldn't be correct.

Now to what I don't understand.

new BankAccount(a1, 300.0) tries to make a new BankAccount. However, There is no constructor with a signature matching (Account, double), there is only a constructor taking String and double. The same reasoning also applies to new Account(a1), Account only has a constructor that takes a String.

With this reasoning I stand by my answer: It will not compile due to code at //1.

Or am I missing something obvious?

Re: About Question enthuware.ocpjp.v8.2.1833 :

Posted: Fri May 19, 2017 9:19 am
by admin
ted.samuelsson wrote:Hi,
The BiFunction is declared as: BiFunction<String, Account, Account> bif = (a1, a2)-> a2 instanceof BankAccount?new BankAccount(a1, 300.0):new Account(a1);

Unless I'm mistaken a1 and a2 are of type Account (otherwise a2 instanceof BankAccount always fail), and the answer marked as correct wouldn't be correct.
Why do you think a1 and a2 are of type Account? Why would a2 instanceof BankAccount always fail if a1 is not of type Account?

Here are the details: https://docs.oracle.com/javase/8/docs/a ... ction.html

-Paul.

Re: About Question enthuware.ocpjp.v8.2.1833 :

Posted: Tue Feb 06, 2018 6:01 am
by rnatesan
in the explanation section can I rewrite this line as following?

map.compute(key, (k, v) -> (v == null) ? msg : v.concat(msg))

as

map.compute(key, (k, v) -> (v == null) ? msg : String::concat(msg))

Re: About Question enthuware.ocpjp.v8.2.1833 :

Posted: Tue Feb 06, 2018 8:07 am
by admin
What happened when you tried it out?

Re: About Question enthuware.ocpjp.v8.2.1833 :

Posted: Fri Feb 09, 2018 11:14 am
by rnatesan
it did not work since it needed two operands for concat

String::concat is not equivalent to a.concat(b)