brotherLouie
brotherLouie

Reputation: 18

Store large amount of string in a multi-dimensional array

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?

Upvotes: 0

Views: 60

Answers (2)

Austin
Austin

Reputation: 8575

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

Upvotes: 1

Dmitrii Bocharov
Dmitrii Bocharov

Reputation: 935

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.

Upvotes: 1

Related Questions