CodyBugstein
CodyBugstein

Reputation: 23302

Which collection to use?

What kind of collection should I use if I need to create a collection that will allow me to store books and how many copies there are in circulation (for a library)? I would use an ArrayList, but I also want to be able to sort the books by order of issue year.

Upvotes: 0

Views: 144

Answers (4)

Rohit Jain
Rohit Jain

Reputation: 213203

You can create a Book Class with all the attributes you have for a book. And implement a Comparable for that Book Class and write sorting logic in there.

Maintain a List<Book>, and use Collections.sort method, to sort your List according to the implemented Sorting logic.

UPDATE: -

As far as, fast look-up is concerned, a Map is always the best bet. And is appropriate to implement a dictionary look-up kind of structure. For that, you would need some attribute that uniquely identifies each book. And then store your book as Map<String, Book>, where your key might be id of type String.

Also, in this case, your sorting logic will change a little. Now you would have to sort on the basis of your Map's value, i.e. on the basis of attributes of Book.

Here's a sample code you can make use of. I have just considered sorting on the basis of id. You can change the sorting logic as needed: -

class Book {
    private int id;
    private String title;

    public Book() {
    }

    public Book(int id, String title) {
        this.id = id;
        this.title = title;
    }

        @Override
    public String toString() {
        return "Book[Title:" + this.getTitle() + ", Id:" + this.getId() + "]";
    }

    // Getters and Setters
}

public class Demo {
    public static void main(String[] args) {

        final Map<String, Book> map = new HashMap<String, Book>() {
            {
                put("b1", new Book(3, "abc"));
                put("b2", new Book(2, "c"));
            }
        };
        List<Map.Entry<String, Book>> keyList = new LinkedList<Map.Entry<String, Book>>(map.entrySet());

        Collections.sort(keyList, new Comparator<Map.Entry<String, Book>>() {
            @Override
            public int compare(Map.Entry<String, Book> o1, Map.Entry<String, Book> o2) {
                return o1.getValue().getId() - o2.getValue().getId();
            }
        });


        Map<String, Book> result = new LinkedHashMap<String, Book>();
        for (Iterator<Map.Entry<String, Book>> it = keyList.iterator(); it.hasNext();) {
            Map.Entry<String, Book> entry = it.next();
            result.put(entry.getKey(), entry.getValue());
        }
        System.out.println(result);
    }
}

OUTPUT: -

"{b2=Book[Title:c, Id:2], b1=Book[Title:abc, Id:3]}"

Upvotes: 3

Santosh Gokak
Santosh Gokak

Reputation: 3411

java.util.TreeMap can be used to index and sort this kind of requirements.

Check http://docs.oracle.com/javase/6/docs/api/java/util/TreeMap.html for more details.

You can use your Book object as key mapped to the number of copies as the value.

Upvotes: 0

Alan Krueger
Alan Krueger

Reputation: 4786

Beyond a simple educational or toy project, you'd want to use a database rather than an in-memory collection. (Not really an answer, but I think worth stating.)

Upvotes: 0

Well, If the entire purpose of your collection is to store the counts of the books, than a dictionary/map, or whatever java's key-value collection is called.

It would probably have title as your key, and the count as your value.

Now I suspect that your collection might be a little more complicated than that, so you might want to make a Book class which has Count as a field, and then I'd probably have a string -> Book dictionary/map anyway, with the string as it's dewy decimal number or some other unique identifier.

Upvotes: 0

Related Questions