如何从我的 ArrayList 中检索评分最高的 10 条评论

How do I retrieve the top 10 of the rated reviews from my ArrayList

早上好伙伴们,我想生成评分最高的前 10 本书(评论),我有一个包含所有书籍的 Arraylist,另一个包含与每本书相关的评论(书籍 ID)

Class 书

public class Book {
 
    private int idbook;
    private String title;
    private String editorial;

    public Book(int idbook, String title, String editorial) {
        this.idbook = idbook;
        this.title = title;
        this.editorial = editorial;
    }

    public int getIdbook() {
        return idbook;
    }

    public void setIdbook(int idbook) {
        this.idbook = idbook;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getEditorial() {
        return editorial;
    }

    public void setEditorial(String editorial) {
        this.editorial = editorial;
    }
   

Class 评论

public class Reviews {
    
    private int idreview;
    private int idbook;
    private int rating;

    public Reviews(int idreview, int idbook, int rating) {
        this.idreview = idreview;
        this.idbook = idbook;
        this.rating = rating;
    }

    public int getIdreview() {
        return idreview;
    }

    public void setIdreview(int idreview) {
        this.idreview = idreview;
    }

    public int getIdbook() {
        return idbook;
    }

    public void setIdbook(int idbook) {
        this.idbook = idbook;
    }

    public int getRating() {
        return rating;
    }

    public void setRating(int rating) {
        this.rating = rating;
    }
}

然后我通过图书数组列表获取其 ID,并将其与输入的评论相匹配,这些评论也具有图书 ID 及其评分

public static void main(String[] args) {
        // TODO code application logic here
        
       // Books
       ArrayList<Book> book = new ArrayList<Book>();
       Book book1 = new Book(123,"Dante Aligueri","Planet");
       Book book2 = new Book(456,"Virginia Wolf","Planet");
       book.add(book1);
       book.add(book2);
       
       // Reseñas 
       ArrayList<Reviews> top = new ArrayList<Reviews>();
       Reviews top1 = new Reviews(1,123,9);
       Reviews top2 = new Reviews(2,123,9);
       Reviews top3 = new Reviews(3,123,9);
       Reviews top4 = new Reviews(4, 456,10);
       top.add(top1);
       top.add(top2);
       top.add(top3);
       top.add(top4);
       
      int sum = 0;
      int rating = 0;
      long idbook = 0;
      
      for (int i = 0; i < mybook.size(); i++) {
            for (int j = 0; j < mytop.size(); j++) {
                if (book.get(i).getIdlibro() == top.get(j).getIdlibro()) {
                idbook = book.get(i).getIdlibro();        
                rating = top.get(j).getRating();
                sum += rating; 
                }
               tops.setText("book id: " + idbook + "total rating" + sum);
        }              
            
   }
}


我想获取每本书的评分,但它只显示一本书及其各自的分数,你能帮帮我吗?

我试着得到这样的输出:

book id: 123 total rating: 27 
book id: 456 total rating: 10 

I am trying to get all each book with its rating

您确定每本书都会有评分吗?

for (int i = 0; i < mybook.size(); i++) {
    boolean ratingFound= false;
    sum= 0;
    for (int j = 0; j < mytop.size(); j++) {
        if (mybook.get(i).getIdbook() == mytop.get(j).getIdbook()) {
            idbook = mybook.get(j).getIdbook();        
            rating = mytop.get(j).getrating();
            sum += rating; 
            ratingFound= true;
        }        
    }
    if (ratingFound) { // Or:  if (sum>0) {
//    I would store the values in a Map which you can sort in the end,
//    since you are interested only in the top 10.
      tops.setText("book id: " + idbook + "total rating" + sum);
    }
}

该列可以很容易地用子查询生成,那为什么要乱搞呢??相似之处:

(
    SELECT SUM(stars) FROM reviews WHERE reviews.book_id = books.book_id GROUP BY book_id;
),

否则您将得到一个不可排序的列表。