java - create a list from subproperties of another list

Alex

I have three classes defined with their getters and setters as follows by a ORM:

class Author {
  Integer id;
  String name;
}

class BookAuthor {
  Integer id_book;
  Author author;
}

class Book {
  String title;
  List<BookAuthor> authors;
}

I'd like to create a list of id_author from the class Book. I've found that a way to do it is using streams. I've tried this:

List<Integer> result = authors.stream().map(BookAuthor::getAuthor::getId).collect(Collectors.toList());

But it does not seem to work. I can I access the "id" property in the Author class?

EDIT: maybe a way would be:

List<Author> authorList = authors.stream().map(BookAuthor::getAuthor).collect(Collectors.toList());
List<Integer> result = authorList.stream().map(Author::getId).collect(Collectors.toList());

Thank you.

Zyga

I assume that the authors variable is a list (or collection) of BookAuthor, not Author (that what it seems like based on your code).

I think you have the right idea, I just dont think you can chain :: operators.

So try with lambda:

authors.stream().
     map(ba -> ba.getAuthor().getId()).
     collect(Collectors.toList());

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Create a list from another list with R

From Dev

How to create list of items from another list

From Dev

Create a list with items from another list at indices specified in a third list

From Dev

Create Alphabet List from list : Java

From Dev

Create tuple list of key,val from another list of values in Python

From Dev

How to create a List<int> basead on two properties from another list?

From Dev

How does haskell create a new list from another list?

From Dev

How does haskell create a new list from another list?

From Dev

Create JSONs from list in directories, created by another list

From Dev

Create a list of repeated consecutive numbers regarding number from another list

From Dev

How to create a list from another list according to a function?

From Dev

Create a list from two lists based on values in another list

From Dev

Using Java8 Streams to create a list of objects from another two lists

From Dev

Create an instance of a list of Class (1) that inherits from another list of class(2) with a list from the other (2)

From Dev

Remove Element in Nested List with Condidion From another List - Java 8

From Dev

Remove Element in Nested List with Condidion From another List - Java 8

From Dev

Create list of java objects from json

From Dev

Create a list from DbCursor in MongoDB and Java

From Dev

Create JSON String Array from Java List

From Dev

Java 8 lambda create list of Strings from list of objects

From Dev

C# asp.net create string list from a another list which returns list of class objects

From Dev

C# asp.net create string list from a another list which returns list of class objects

From Dev

Create a list in a list from a file

From Dev

Create dict from list of list

From Dev

Create list from list items

From Dev

Create a list in a list from a file

From Dev

create multiple columns from list of values of another column

From Dev

In Excel how do I create a condensed list from another array?

From Dev

create/modify a list in another form

Related Related

  1. 1

    Create a list from another list with R

  2. 2

    How to create list of items from another list

  3. 3

    Create a list with items from another list at indices specified in a third list

  4. 4

    Create Alphabet List from list : Java

  5. 5

    Create tuple list of key,val from another list of values in Python

  6. 6

    How to create a List<int> basead on two properties from another list?

  7. 7

    How does haskell create a new list from another list?

  8. 8

    How does haskell create a new list from another list?

  9. 9

    Create JSONs from list in directories, created by another list

  10. 10

    Create a list of repeated consecutive numbers regarding number from another list

  11. 11

    How to create a list from another list according to a function?

  12. 12

    Create a list from two lists based on values in another list

  13. 13

    Using Java8 Streams to create a list of objects from another two lists

  14. 14

    Create an instance of a list of Class (1) that inherits from another list of class(2) with a list from the other (2)

  15. 15

    Remove Element in Nested List with Condidion From another List - Java 8

  16. 16

    Remove Element in Nested List with Condidion From another List - Java 8

  17. 17

    Create list of java objects from json

  18. 18

    Create a list from DbCursor in MongoDB and Java

  19. 19

    Create JSON String Array from Java List

  20. 20

    Java 8 lambda create list of Strings from list of objects

  21. 21

    C# asp.net create string list from a another list which returns list of class objects

  22. 22

    C# asp.net create string list from a another list which returns list of class objects

  23. 23

    Create a list in a list from a file

  24. 24

    Create dict from list of list

  25. 25

    Create list from list items

  26. 26

    Create a list in a list from a file

  27. 27

    create multiple columns from list of values of another column

  28. 28

    In Excel how do I create a condensed list from another array?

  29. 29

    create/modify a list in another form

HotTag

Archive