Skip to main content

Lesson 9 of 10

Mini Project: Library System - Part 5 (Collections Catalog)

Upgrade the Library catalog to use proper Collections: HashMap for O(1) ISBN lookup, TreeMap for sorted title browsing, HashSet for genre management, PriorityQueue for most-reserved books, and immutable views for public catalog access.

20 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Collections-Powered Catalog

Collections-Powered Catalog
import java.util.*;
import java.util.stream.*;

public class LibraryCatalog {
    // O(1) lookup by ISBN
    private final Map<String, Book> booksByIsbn = new HashMap<>();
    // Sorted by title for browsing
    private final TreeMap<String, Book> booksByTitle = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
    // Group books by genre
    private final Map<Genre, List<Book>> booksByGenre = new EnumMap<>(Genre.class);
    // Track available genres
    private final Set<String> activeGenres = new LinkedHashSet<>();

    public void addBook(Book book) {
        if (booksByIsbn.containsKey(book.getIsbn()))
            throw new IllegalArgumentException("Duplicate ISBN: " + book.getIsbn());
        booksByIsbn.put(book.getIsbn(), book);
        booksByTitle.put(book.getTitle(), book);
        booksByGenre.computeIfAbsent(book.getGenre(), g -> new ArrayList<>()).add(book);
        activeGenres.add(book.getGenre().getDisplayName());
    }

    public Optional<Book> findByIsbn(String isbn) {
        return Optional.ofNullable(booksByIsbn.get(isbn));
    }

    public List<Book> findByGenre(Genre genre) {
        return List.copyOf(booksByGenre.getOrDefault(genre, List.of()));  // immutable copy
    }

    public List<Book> findByTitlePrefix(String prefix) {
        return new ArrayList<>(booksByTitle
            .subMap(prefix, prefix + Character.MAX_VALUE)
            .values());
    }

    public List<Book> getTopReserved(int n) {
        PriorityQueue<Book> pq = new PriorityQueue<>(
            Comparator.comparingInt(Book::getReservationCount).reversed()
        );
        pq.addAll(booksByIsbn.values());
        List<Book> top = new ArrayList<>();
        for (int i = 0; i < n && !pq.isEmpty(); i++) top.add(pq.poll());
        return top;
    }

    // Statistics
    public Map<Genre, Long> genreBookCount() {
        Map<Genre, Long> counts = new LinkedHashMap<>();
        booksByGenre.forEach((g, books) -> counts.put(g, (long) books.size()));
        return Collections.unmodifiableMap(counts);
    }

    public void printCatalog() {
        System.out.println("=== LIBRARY CATALOG (" + booksByIsbn.size() + " books) ===");
        booksByTitle.forEach((title, book) -> System.out.println("  " + book));
    }
}

Quick Quiz

Tip

Tip

Practice Mini Project Library System Part 5 Collections Catalog in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

Visual architecture and concepts for Mini Project: Library System - Part 5 (Collections Catalog)

Practice Task

Note

Practice Task - (1) Write a working example of Mini Project Library System Part 5 Collections Catalog from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Common Mistake

Warning

A common mistake with Mini Project Library System Part 5 Collections Catalog is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready java code.

Topics in This Module

0/10 done