getOrDefault
s signature is getOrDefault(K, V)
, not getOrDefault(K, Supplier<V>)
. You want to use Map#computeIfAbsent
which accepts a Function<? super K, ? extends V>
as second argument to compute a value in the case of an absent key.
That said, it feels wrong to (ab)use computeIfAbsent
for this. Why not simply check the result, once retrieved from the map?
final var fileType = CertificationsConstants.FILE_TYPES.get(
uploadCertificateSchoolRequest.getTypeFile());
if (fileType == null) {
throw new IllegalStateException("unknown filetype");
}
cliente.setMIMECode(fileType);
Or, if you don't require this exact exception type, the following:
cliente.setMIMECode(
Objects.requireNonNull(
CertificationsConstants.FILE_TYPES.get(
uploadCertificateSchoolRequest.getTypeFile(),
"unknown filetype")));