2

I have a List of Book

List<Book> books = new ArrayList<Book>();

public class Book {
    String name;
    List<Author> authors;
}

Book contains a List of Author which contains an ID, Name and Age

public class Author {
    int ID;
    String Name;
    int Age;
}

I can iterate through the books List and return the Book where Name = x but I'm not sure how I can search Author and return where Name = y.

2
  • Why are you using Authors in an ArrayList ? Are there multiple Authors for every single book ? Commented Aug 18, 2013 at 16:46
  • Multiple authors for some books would be reason enough to use an arraylist Commented Aug 18, 2013 at 16:47

3 Answers 3

8

Add a method hasAuthor(String) to your Book which loops through the List<Author> list and compares the name. Like so:

boolean hasAuthor(String name) {
    for (Author author : authors) {
        if (author.name.equals(name)) {
            return true;
        }
    }
    return false;
}

To find books with a specific author, you loop over the List<Book> and invoke the hasAuthor(String) method.

Sign up to request clarification or add additional context in comments.

4 Comments

+1 I like this over iterating outside the Book class as it is more cleaner.
Or, you could avoid using a for loop if you override the equals(Object) method in your Author class.
I missed this from the original post. I'm wanting to check all the Authors for Name = x, regardless of what book they wrote. If I did it this way surely I'd have to iterate through each book and then run the author search iteration for each?
Thanks, I'll go with this method. I have about 4k Books and Authors so thought it might be slow iterating twice.
1

Maybe this is off topic, but when solving such problems, having objects with nested lists, though logically simple, is a really bad idea. Its extremely hard to maintain, scale and work with.

If you use this structure throughout and come across a problem like:

Give me all the books written by this author.

You are toast, you will have to iterate over each and every book. Or start maintaining caches and what not. It gets worse when the nesting deepens.

Lets say, now you need to maintain each Author's list of hobbies too. Think of how would you implement something like : Give me all the books whose authors like skydiving!

I know this doesn't answer your question. Just my 2 cents.

3 Comments

The real data I'm using does go 3 levels deep and does seem like a pain to work this. Unfortunately I'm deserializing someone else's data that I have no control over so I think it would be even more of a pain to try and change the objects on my side. But that's probably for another thread.
@rocketboy - conceptually, I agree with you. Q: How would you design a "searchable collection of books"?
@rocketboy, what do you recommend then in the type of situations where you to answer questions like "give me all the books written by this author". I have a similar type of problem at the moment and searching for a better way than next a list inside the object.
0

Something primitive:

List<Author> authors;
...
Iterator<Author> ai = authors.iterator();
Author a;
while(ai.hasNext()){
    a = ai.next();
    if(a.name.equalsIgnoreCase(yourname)){
       return a;
    }
    continue;
}

Edit: Too late :/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.