1

I have a problem with editing item in ArrayList<Book> book = new ArrayList<Book>. I know how to do it in C# LINQ but I don't know how use lambda expression in Java 8.

C# with LINQ (it works): books = books.Select(b => { if (b.title == title) b.title = newTitle; return b; }).ToList();

Java with Lambda (it not works): book = book.stream().filter(b -> b.author().equals(title) = newTitle);

Can you help me?

Duzy
  • 79
  • 2
  • 8
  • 2
    `book.stream().filter(b -> b.author().equals(title)).forEach(b -> b.setTitle(newTitle))`. Note however, that this has **awful** performance characteristics, if you are doing this more than once or twice on a `List` of more than a few items (>100) then use a `Map`! – Boris the Spider Jan 20 '17 at 15:42
  • Really thanks! I want to use editing only a few times maybe for several elements – Duzy Jan 20 '17 at 15:58
  • Incidentally, your LINQ example seems to mutate an object in the `Select` method - this is [a **really** bad idea](http://stackoverflow.com/a/16594601/2071828). Both LINQ and Java 8 `Stream` expect some lambdas to be [pure](https://en.wikipedia.org/wiki/Pure_function) and doing what you're doing may lead to insidious and hard to diagnose bugs. Please learn about function programming concepts before using functional programming APIs! – Boris the Spider Jan 21 '17 at 11:55

0 Answers0