/* * T0Mn01.java ('M' for Miscellaneous)-- Remember T01n22.java? OK, * probably not. That was the example that stored book objects in an * array in no particular order. * * Now we know about BSTs, and how, so long as the tree is "bushy", * we'd rather search a BST than an unordered array. * * As with many other data structures and algorithms, Java's designers * have included a general-purpose tree class that allows us to * "map" identifying values to objects so that the objects can be * easily (and efficiently) retrieved later. * * This program uses the Book class from that earlier program example * to demonstrate how a TreeMap is used. Three books are placed in the * TreeMap by their ISBN values, and a few of the available TreeMap * methods are demonstrated. */ import java.util.*; // The Book class, but now with private data members, a constructor, // and getters. class Book { private String isbn13; // 13-digit ISBN private String title; // The title of the book private String author; // The name of the book's author private int copyright; // The date the work was published (approx.) public Book (String isbn13, String title, String author, int copyright) { this.isbn13 = isbn13; this.title = title; this.author = author; this.copyright = copyright; } public String getISBN13 () { return isbn13; } public String getTitle () { return title; } public String getAuthor () { return author; } public int getCopyright () { return copyright; } } public class T0Mn01 { public static void main (String [] args) { // Create a TreeMap object that uses Strings as keys // and Book objects as values. TreeMap library = new TreeMap (); // Construct three book objects Book book1 = new Book("9780394800783","Horton Hears a Who", "Dr. Suess",1954); Book book2 = new Book("9780385333849","Slaugherhouse-Five", "Kurt Vonnegut",1969); Book book3 = new Book("0123456789012", "Harry Potter and the Disfiguring Zit", "J. K. Rowling",2017); // Add the three books to the map. Note that we're using // the ISBN numbers as the keys; we want to be able to // uniquely identify the books. library.put(book1.getISBN13(),book1); library.put(book2.getISBN13(),book2); library.put(book3.getISBN13(),book3); // TreeMap supplies a variety of methods to query the // the map. The following printlns demonstrate a few of them. System.out.println("\nThe library has " + library.size() + " books."); System.out.println("\nThe title of the book with with smallest ISBN (" + library.firstKey() + ")\n\tis '" + library.get(library.firstKey()).getTitle() + "'."); // Want to access every item in the map? Here's one way to // do it. The values() method returns a Collection of the // map's content, from which we can request an Iterator. System.out.println("\nThe books in the library are:"); Iterator it = library.values().iterator(); while (it.hasNext()) { Book tome = it.next(); System.out.println(tome.getISBN13() + ": '" + tome.getTitle() + "' by " + tome.getAuthor() + " (" + tome.getCopyright() + ")"); } // Or we can hide some of the mess and use the enhanced FOR // loop! // for (Book tome: library.values()) { // System.out.println(tome.getISBN13() + ": '" + tome.getTitle() // + "' by " + tome.getAuthor() // + " (" + tome.getCopyright() + ")"); // } } }