1

I need some help mapping a Map<String, List<Fee>> with a List<FeeRequest>.

Fee object looks like this:

private String feeCode;

FeeRequest objects looks like this:

private String feeCode;
private String debtorAccount;

So what I need is to map:

String debtorAccount(from map) -> debtorAccount(from List)

feeCode(from List from map) -> feeCode(from List)

I want to try not to use foreach, but instead learn to properly use .stream().map() features.

What I've managed to do:

Map<String, List<Fee>> feeAccounts is parsed from another method.

 List<FeeRequest> feeRequests = feeAccounts.entrySet().stream().map(feeAcc -> {
        FeeRequest request = new FeeRequest();
        request.setDebtorAccount(feeAcc.getKey());
        request.setFeeCode(...);
        return request;
    }).collect(Collectors.toList());

I think that my approach is bad, but I don't know how to make it work. I tried looking at some examples but they're too basic. So I would be glad to get any help. Thanks!

6
  • 2
    I think that my approach is bad - why? Commented Jul 18, 2019 at 7:40
  • @Eran because I can't map the feeCode, it doesn't show me the Fee objects fields Commented Jul 18, 2019 at 7:44
  • 1
    you can access the List<Fee> with feeAcc.getValue(). It's not clear what you wish to do with it, though. Commented Jul 18, 2019 at 7:45
  • @Eran Basically what I want is to FeeRequest.setFeeCode(Fee.getFeeCode()). Feecodes from one list map with feecodes from another Commented Jul 18, 2019 at 7:48
  • 2
    But each entry of the input Map contains a List of Fee instances. Which one of them do you wish to use? Or do you want to create multiple FeeRequest instances (one for each Fee instance)? Commented Jul 18, 2019 at 7:51

2 Answers 2

1

If each Fee instance should generate a FeeRequest instance, you need flatMap:

List<FeeRequest> feeRequests =
    feeAccounts.entrySet()
               .stream()
               .flatMap(feeAcc -> feeAcc.getValue()
                                        .stream()
                                        .map(f -> {
                                            FeeRequest request = new FeeRequest();
                                            request.setDebtorAccount(feeAcc.getKey());
                                            request.setFeeCode(f.getCode());
                                            return request;
                                        }))
               .collect(Collectors.toList());
Sign up to request clarification or add additional context in comments.

1 Comment

Yes because you get a lot of lists in the first step which then have to be merged into one map.
0

If FeeRequest has a constructor with two arguments you can use something like this:

feeAccounts.entrySet().stream()
        .flatMap(
                accountEntry -> accountEntry.getValue().stream().map(
                        fee -> new FeeRequest(
                                accountEntry.getKey(),
                                fee.getFeeCode()
                        )
                )
        ).collect(toList());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.