codeshood
codeshood

Reputation: 63

Search for a particular element from object in ArrayList

Here I have created a class:

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;
    }
}

Now I want to search for particular value according to author and book name

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

I have created a menu driver using 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();       ------

                        }
                    }

I know how to find and search for a particular element in ArrayList but not for objects.

Upvotes: 0

Views: 960

Answers (3)

Level_Up
Level_Up

Reputation: 824

First there is a new ways (With java 8+) and old way to do this. The new ways will be something like this:

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());

Another (old fashioned) way is with for loop:

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); 
     }
}

After this you can print in both cases the result list containing the book. But if you will search a lot of this author and book name and you have a lots of elements you can think check the performance. Because every search will loop over the list. Maybe there is better solution using Map ...

Some additional information. It is import if you know if there is always only one element that will be found from the criteria. For example in your case you unique find one book where has name X and author Y. There can not be another book with the same name and author. In such cases you can do like this:

New way (after java 8):

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

old way:

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

This way it is faster when you search one element

Good luck!

Upvotes: 0

Gaurav Shakya
Gaurav Shakya

Reputation: 121

Below code return a list, based on of your search (filter):

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

Upvotes: 0

Safwan Shaikh
Safwan Shaikh

Reputation: 546

Using for loop over ArrayList can solve your problem, it's naive method and old fashioned.

Below is the code for it.

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;
      }
}

Hope you can get an idea from it.

Upvotes: 1

Related Questions