0

I'm having a project that will suggest books to the user and i'm planning in making my application offline. My question is: Is it okay to store my large amount of Strings on my array? Multi-dimensional array to be exact. I'm planning in doing it this way.

public bookLibrary() { 
    public static String[][] books = {{bookID}, 
{bookTitle}, {bookGenre}, {bookPlot}, {etc}}
}

What do you guys think? Should I continue it this way? If using this Data Structure is slow, what else can I use to store data?

1
  • why not using some offline database for this? you'll get the power of SQL and it will consume much less resources Commented Nov 10, 2015 at 8:12

2 Answers 2

1

If you must use Java... I recommend you encapsulate the book data inside a Book class. Your library can store the books in a HashMap to provide quick access and scale-ability. In fact, you could use several HashMaps together to allow you to reach the book according to any one of its features (e.g. author or genre).

The Book class might look like this:

class Book{

    HashMap<String, String> features;

    public Book(){
        this.features= new HashMap<String,String>();
    }

    public HashMap<String, String> getFeatures() {
        return features;
    }

    public String addFeature(String key,String value){
        return features.put(key, value);
    }

    public void addFeatures(HashMap<String, String> newFeatures){
        this.features.putAll(newFeatures);
    }
}

Your Library class could use a HashMap of HashMaps:

HashMap<String,HashMap<String,Book>> library;

So, to access a book, you call

public Book getBook(String featureType,String key){
    return library.get(featureType).get(key);
}

The featureType string specifies whether you're looking up a book by author, genre, description, etc. The key is the specific author's name. For example, to get a book by Bob Smith, you would use getBook("author","Bob Smith");.

You can add books to the library as follows:

    public void addBookToLibrary(Book book){
        HashMap<String,String> bookFeatures = book.getFeatures();
        for(String featureType : bookFeatures.keySet()){
            HashMap<String,Book> libraryFeatureMap = library.get(featureType);
            libraryFeatureMap.put(bookFeatures.get(featureType), book);
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

1

Look at relational database.

It is not good to store your data as you suggested - in an array. What would you suppose to happen if your app stops? All your data would be lost.

After that read about ORMs. For Java one of the most popular is Hibernate. Using is you will retrieve info into a data structure like the following one:

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

And the Book is something like:

public class Book {
    private Integer bookID;
    private String bookTitle;
    //etc, read about foreign key declration e.g.
    //then follow getters/setters
}

Also if you use Eclipse IDE, try to install Hibernate Jboss tools. Using it you can generate a lot of useful code instead of writing it by yourself.

1 Comment

(Or NetBeansIDE and eclipseLink [JPA/ORM].) An embedded database like H2 would be painless to distribute.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.