【问题标题】:Search for a particular element from object in ArrayList从 ArrayList 中的对象中搜索特定元素
【发布时间】:2018-08-06 09:42:42
【问题描述】:

我在这里创建了一个类:

class book{
    String book_nm;
    String author_nm;
    String publication;
    int price;
    book(String book_nm,String author_nm,String publication,int price){
        this.book_nm=book_nm;
        this.author_nm=author_nm;
        this.publication=publication;
        this.price=price;
    }
}

现在我想根据作者和书名搜索特定值

ArrayList<book> bk = new ArrayList<book>();

我已经使用 switch case 创建了一个菜单驱动程序

case 3: System.out.println("Search:"+"\n"+"1.By Book Name\n2.By Author Name");
                    Scanner s= new Scanner(System.in);
                    int choice=s.nextInt();
                    while(choice<3){
                        switch(choice){
                            case 1:System.out.println("Enter the name of the book\n");
                                    String name=s.next();
                                    -------
                            case 2:System.out.println("Enter the name of the author\n");
                                    String name=s.next();       ------

                        }
                    }

我知道如何在 ArrayList 中查找和搜索特定元素,但不知道如何查找对象。

【问题讨论】:

标签: java


【解决方案1】:

在 ArrayList 上使用 for 循环可以解决您的问题,这是一种幼稚的方法和老式的方法。

下面是它的代码。

import java.util.ArrayList;

public class HelloWorld{

     public static void main(String []args){
        String author_name = "abc";
        ArrayList<book> bk = new ArrayList<book>();
        bk.add(new book("abc", "abc", "abc", 10));
        bk.add(new book("mno", "mno", "abc", 10));
        bk.add(new book("xyz", "abc", "abc", 10));
        ArrayList<book> booksByAuthor = new ArrayList<book>();
        for(book obj : bk)
        {
            if(obj.author_nm == author_name)
            {
                booksByAuthor.add(obj);
            }
        }

     }
}

class book{
      public String book_nm;
      public String author_nm;
      public String publication;
      public int price;
      public book(String book_nm,String author_nm,String publication,int price){
          this.book_nm=book_nm;
          this.author_nm=author_nm;
          this.publication=publication;
          this.price=price;
      }
}

希望你能从中得到一个想法。

【讨论】:

    【解决方案2】:

    以下代码根据您的search (filter)返回一个列表:

    List< Book> result = bk.stream().filter(book -> "booknamehere".equals(book.getBook_nm()))    
       .filter(book -> "authernamehere".equals(book.getAuther_nm()))    
       .collect(Collectors.toList());
    

    【讨论】:

      【解决方案3】:

      首先有一种新方法(使用 java 8+)和旧方法来做到这一点。新方法将是这样的:

      String authorName =  s.next();
      String bookName = s.next();
      List<String> result = bk.stream()                        // open stream
                  .filter(book-> book.getBook_nm().equals(bookName) && book.getAuthor_nm().equals(authorName ) ) 
                  .collect(Collectors.toList());
      

      另一种(老式)方法是使用 for 循环:

      ArrayList<book> result = new ArrayList<book>();
      for(Book book : bk) //By the way always use Big first letter for name of your Class! (Not book but Book)
      {
           if(book.getBook_nm().equals(bookName) && book.getAuthor_nm().equals(authorName))
           {
                result.add(book); 
           }
      }
      

      之后,您可以在这两种情况下打印包含该书的结果列表。但是如果你要搜索很多这个作者和书名,并且你有很多元素,你可以考虑检查性能。因为每次搜索都会遍历列表。也许有更好的解决方案使用 Map ...

      一些附加信息。如果您知道是否总是只能从标准中找到一个元素,那么这很重要。例如,在您的情况下,您会发现一本书的名称为 X 和作者 Y。不可能有另一本书具有相同的名称和作者。在这种情况下,您可以这样做:

      新方式(java 8之后):

       Book res = bk.stream()
      .filter(book -> book.getBook_nm().equals(bookName) && book.getAuthor_nm().equals(authorName))
      .findFirst()
      .get();
      

      老办法:

      Book result = null;
      for(Book book : bk)
      {
           if(book.getBook_nm().equals(bookName) && book.getAuthor_nm().equals(authorName))
           {
                result = book;
                break;
           }
      }
      

      这样搜索一个元素会更快

      祝你好运!

      【讨论】:

        猜你喜欢
        • 2012-11-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多