Is there a way I can use the same method reference in these two lines for the getter? I tried a Supplier which works with the first statement but the second statement requires a Function.
Integer id = Integer.parseInt(accountModel.getExternalId());
accountsWithNonNumericIds.sort(Comparator.comparing(AccountModel::getExternalId));
The method reference can produce a Function
, but not a Supplier
, because in order to get the ID, you need to know the account to get it from.
Function<AccountModel,String> func = AccountModel::getExternalId;
Integer id = Integer.parseInt(func.apply(accountModel));
accountsWithNonNumericIds.sort(Comparator.comparing(func));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.