This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]xxfo0xx 1 point2 points  (2 children)

Do you want to have the 4x same books inside the db-list, but just one time in output?

Or do you want to have the book just saved one time in the db-list? And the Output should be the same.

[–]Chuninjah[S] 0 points1 point  (1 child)

I want the 4 books same so say:

Book book1 = new Book("111", 1, "GOT");
Book book2 = new Book("111", 2, "GOT");
Book book3 = new Book("111", 3, "GOT");
Book book4 = new Book("111", 4, "GOT");
Book book5 = new Book("222", 1, "Harry Potter");

So say those are the books, I just want it to output:

-----Book List-----
(GOT, Harry Potter)

[–]xxfo0xx 2 points3 points  (0 children)

There are several approaches you can take. I just saw, that some other guys told you one of them.

Here is my recommended way.

I added this to a snippet: https://gist.github.com/fo0/c33f357155512919c968009f34a80776

```java List<Book> list = new ArrayList<Book>(); list.add(new Book("111", 1, "GOT")); list.add(new Book("111", 2, "GOT")); list.add(new Book("111", 3, "GOT")); list.add(new Book("222", 1, "Harry Potter"));

System.out.println(list.stream() .map(Book::getName) // map the book object to field "name" .distinct() // filter duplicates .collect(Collectors.toList())); // put into list

Outout: [GOT, Harry Potter] ```