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; } 
1
  • 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 Commented Nov 16, 2018 at 5:06

2 Answers 2

2
ids.stream().map(id -> new AccountPermissionsUpdate(id, true)); 

You will call it like this.

Sign up to request clarification or add additional context in comments.

Comments

2

Try out below code:

ids.stream().map(element -> new AccountPermissionsUpdate(element,true)); 

2 Comments

Wow you are fast, but the id seem not correct here, should be element?
Yes. My bad. It should be element. I will update my answer. I just copied and pasted your code from your question, that's didn't notice that.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.