第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

比較器比較內(nèi)部列表字段

比較器比較內(nèi)部列表字段

胡子哥哥 2023-10-12 14:45:43
我有一個(gè)這樣的模型class Person {     Instant updateDate; // mandatory     List<Account> accounts; // can be empty}class Account {    Instant accountUpdateDate; // can be null}  假設(shè)我有一個(gè)人員列表(每個(gè)人都包含可以為空的帳戶列表)我怎樣才能獲得具有最大 accountUpdateDate 的人,否則我應(yīng)該檢索具有最大 updateDate 的人。類似這樣的事情Comparator<Person> UPDATE_DATE_COMPARATOR = Comparator    .comparing(person -> person.getAccounts().stream().map(Account::getAccountUpdateDate)..., Comparator.nullsFirst(naturalOrder()))    .thenComparing(Person::getUpdateDate));Collections.max(personList, UPDATE_DATE_COMPARATOR);
查看完整描述

1 回答

?
MM們

TA貢獻(xiàn)1886條經(jīng)驗(yàn) 獲得超2個(gè)贊

一種方法是使用orElseGet分離這兩種功能,例如:


Supplier<Person> supplyMaxUpdateDatePerson = () -> personList.stream()

        .max(Comparator.comparing(Person::getUpdateDate))

        .orElse(null); // or an identity value equivalent for 'Person'


Person maxAccountUpdateDateOrElseUpdateDate = personList.stream()

        .flatMap(person -> person.getAccounts().stream()

                .map(account -> new AbstractMap.SimpleEntry<>(person, account.getAccountUpdateDate())))

        .filter(entry -> entry.getValue() != null) // otherwise you would always have a result in max

        .max(Comparator.comparing(AbstractMap.SimpleEntry::getValue))

        .map(AbstractMap.SimpleEntry::getKey)

        .orElseGet(supplyMaxUpdateDatePerson); // invoked only when all 'accountUpdateDate' are 'null'



查看完整回答
反對(duì) 回復(fù) 2023-10-12
  • 1 回答
  • 0 關(guān)注
  • 93 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)