1

I am trying to use java stream instead of forloop

ids.stream().map(AccountPermissionsUpdate::new)

I have created an other constructor, my question is: how to call the second constructor

new AccountPermissionsUpdate(id,true)

Thanks

public AccountPermissionsUpdate(long accountId) {
        this.accountId = accountId;
    }

public AccountPermissionsUpdate(long accountId, boolean forcedLogout) {
        this.accountId = accountId;
        this.forcedLogout = forcedLogout;
}
Ben Luk
  • 735
  • 2
  • 8
  • 15
  • Just a side note (learned from Josh Bloch's book Effective Java), stream api is not a replacement for for loops, so be careful as it creates many stream instances in between. You can read more about that in his book though – Ketan Nov 16 '18 at 05:06

2 Answers2

2

Try out below code:

ids.stream().map(element -> new AccountPermissionsUpdate(element,true));
Jignesh M. Khatri
  • 1,407
  • 1
  • 14
  • 22
2
ids.stream().map(id -> new AccountPermissionsUpdate(id, true));

You will call it like this.

Dang Nguyen
  • 1,209
  • 1
  • 17
  • 29