0

there is a List and i want to create books object and add all the book object into this List. here I did with normal approach like this .

public static List<Books> listOfBooks = new ArrayList<>();
public static void addProduct() {
        Books book1 = new Books("java", "games", 200.56);
        Books book2 = new Books("c", "dennis", 300.56);
        Books book3 = new Books("c++", "Bjarne", 250.56);
        Books book4 = new Books("javaScript", "Brendan", 209.56);
        Books book5 = new Books("Sql", "Donald", 249.56);
        listOfBooks.add(book1);
        listOfBooks.add(book2);
        listOfBooks.add(book3);
        listOfBooks.add(book4);
        listOfBooks.add(book5);
    }

I want the same operation by using lambda expression but i do not know how to do it. please help me out?

4
  • 2
    Do you know what lambda expressions are? Commented Nov 2, 2017 at 6:44
  • yeah @flown i know the basic of lamda and while learning i got these doubt . Commented Nov 2, 2017 at 6:48
  • 1
    Basically lambda expressions are single abstract method interfaces. So, which interface would you implement to do your task? Or are you trying to use the Stream API? Commented Nov 2, 2017 at 6:51
  • @Flown i'm not dealing with interface right now yes i'm trying to use stream API. and i'm trying to re factor above piece of code by lamda expression but i'm not getting how to do so i asked Commented Nov 2, 2017 at 7:02

2 Answers 2

1

You could write:

Arrays.asList(book1, book2, ...).forEach(b -> listOfBooks.add(b));

Or shorter using a method reference instead of a lambda:

Arrays.asList(book1, book2, ...).forEach(listOfBooks::add);

But that's not really better than writing:

listOfBooks.addAll(Arrays.asList(book1, book2, ...));
Sign up to request clarification or add additional context in comments.

4 Comments

thank you @janos but if i want to just write this piece of code by using lamda expression then how would i do that?
@chandmohd I changed the first variation to use a lambda expression. But that's missing the point. This is not a typical or recommended use case for lambdas.
i got the point but as you said that this is not recommended use .why ? are you saying in term of performance? if yes then how would i see which method taking how much time to execute ?
@chandmohd If you want to add all elements of list X to list Y, it's best to use Y.addAll(X) because that's the API designed for this, and you can expect implementations to be more optimal than other hand-crafted solutions. If you have individual items and you want to add them to a list, then the natural way is to add them one by one, rather than creating temporary objects to wrap them just so that you can use lambdas. A better example using lambdas would be something that requires passing a function as parameter.
0

The Stream API is using lambda expression, but are not part of it.

public static List<Books> listOfBooks = new ArrayList<>();
public static void addProduct() {
  String[] languages = {"java", "c", "c++", "javascript", "Sql"};
  String[] authors = {"games", "dennis", "Bjarne", "Brendan", "Donald"};
  double[] prices = {200.56d, 300.56d, 250.56d, 209.56d, 249.56d};
  listOfBooks = IntStream.range(0, languages.length)
                        .mapToObj(i -> new Books(languages[i], authors[i], prices[i]))
                        .collect(Collectors.toList());
  // Alternatively:
  /* IntStream.range(0, languages.length)
          .mapToObj(i -> new Books(languages[i], authors[i], prices[i]))
          .forEach(listOfBooks::add);
  */
}

1 Comment

Thank you @Flown appreciated!!

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.