简体   繁体   English

如何在 Java 中简化此方法?

[英]How can I simplify this method in Java?

How can I simplify this expression in Java我怎样才能简化这个表达式 Java

public Map<String, List<Account>> findAllAccountsCredits(final List<String> listOfIds) {
    List<Account> accountCredit = executeQueryForAccountCredits(listOfIds);
    Map<String, List<Account>> groupedByOwnerId = accountCredit.stream().collect(Collectors.groupingBy(Account::getOwnerId));
    Map<String, List<Account>> result = new HashMap<>();

    for (Map.Entry<String, List<Account>> entry : groupedByOwnerId.entrySet()) {
        result.put(entry.getKey(), getSortedValues(entry.getValue()));
    }

    return result;
}

My point is, then for each key, I want to sort its values according to getSortedValues(entry.getValue ()) .我的观点是,对于每个键,我想根据getSortedValues(entry.getValue ())对其值进行排序。 But here, instead of doing it in one line, I'm functionally making another new object and loop.但是在这里,我不是在一行中做,而是在功能上制作另一个新的 object 和循环。

Map<String, List<Account>> result = new HashMap<>();

for (Map.Entry<String, List<Account>> entry : groupedByOwnerId.entrySet()) {
    result.put(entry.getKey(), getSortedValues(entry.getValue()));
}

How can I simplify this method while keeping the operation as it is now.如何在保持操作不变的同时简化此方法。

Just add accountCredit.sort(...);只需添加accountCredit.sort(...); before stream :stream之前:

public Map<String, List<Account>> findAllAccountsCredits(final List<String> listOfIds) {
    List<Account> accountCredit = executeQueryForAccountCredits(listOfIds);
    accountCredit.sort(...);
    return accountCredit.stream().collect(Collectors.groupingBy(Account::getOwnerId));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM