0

I am not a 100% on how to do this yet . But I will explain the situation first.

I am letting a user search for book and give 10 recommendations based on the first result in search . I store the recommended Books in RecommendBooks(POJO) once I have the recommended books I am getting the review on each book storing in another POJO BookReview(POJO).

Intention : Now I want to show the recommeded book based on the review

I am kind of stuck on how to sort the reviews and not basically create a copy of RecommendedBooks that is orderd based on review.

Anyone has any good way of doing this?

Praveen
  • 101
  • 14
  • Have a `BookAndReview` POJO that holds a book and its review? – Aaron Jun 05 '16 at 21:36
  • Could you post some code and what your actual problem is with it? It would be very easy to say 'stop talking POJO if you want to add functionality' but without some code it's hard to understand what's going on. – sisyphus Jun 05 '16 at 21:45

1 Answers1

0

Take these classes:

class Book {
  String id;
  String name;
}

class Review {
  String bookId;
  String text;
}

class QueryResult {
  Book book;
  Review review;
}

First you get 10 recommended Books:

List<Book> recommended = queryFor10RecommendedBooks();

Then get the reviews and wrap everything in List<QueryResult>:

List<QueryResult> result = recommended.stream()
  .map(b -> new QueryResult(book, getReviewForBook(b)))
  .collect(Collectors.toList());

This code will query one Review at a time. You can also optimize it to query all reviews for all 10 Books, and then afterwards put them together. Depends on your data structure / database what is faster.


Other nice way would be to have a result as Map<Book, Review> which is simple as well:

Map<Book, Review> result = recommended.stream()
  .collect(Collectors.toMap(Function.identity(), b -> getReviewForBook(b)));
Benjamin M
  • 23,599
  • 32
  • 121
  • 201