About Question enthuware.ocpjp.v8.2.1833 :
Moderator: admin
-
- Posts: 56
- Joined: Tue Feb 21, 2017 4:24 pm
- Contact:
About Question enthuware.ocpjp.v8.2.1833 :
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.
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1833 :
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.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.
You can contact Oracle support and get more clarification. You can also open a feature request on their support site.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 1
- Joined: Fri May 19, 2017 6:13 am
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1833 :
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?
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?
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1833 :
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?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.
Here are the details: https://docs.oracle.com/javase/8/docs/a ... ction.html
-Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 7
- Joined: Wed Jun 25, 2014 12:44 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1833 :
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))
map.compute(key, (k, v) -> (v == null) ? msg : v.concat(msg))
as
map.compute(key, (k, v) -> (v == null) ? msg : String::concat(msg))
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1833 :
What happened when you tried it out?
If you like our products and services, please help us by posting your review here.
-
- Posts: 7
- Joined: Wed Jun 25, 2014 12:44 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1833 :
it did not work since it needed two operands for concat
String::concat is not equivalent to a.concat(b)
String::concat is not equivalent to a.concat(b)
Who is online
Users browsing this forum: No registered users and 44 guests