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
  • 695
  • 1
  • 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,159
  • 1
  • 10
  • 20
2
ids.stream().map(id -> new AccountPermissionsUpdate(id, true));

You will call it like this.

Dang Nguyen
  • 1,199
  • 1
  • 16
  • 28