// CSE 373, Winter 2013, Marty Stepp // This program shows demonstrates the Guava collection library // using a set of data related to US presidents. // // We use Multimap to map from first names to presidents with that name; // BiMap to map from presidents to their vice-presidents and back; // and Table to map from a president/VP combination to the year they were inaugurated. // // To compile/run this program you must download and link the Guava JAR to your project. import java.io.*; import java.util.*; import com.google.common.collect.*; public class Lecture05Guava { public static void main(String[] args) throws FileNotFoundException { // (first name -> [presidents with that first name]) Multimap mmap = HashMultimap.create(); // (prez <-> vp) BiMap bmap = HashBiMap.create(); // (prez, vp -> year) Table table = TreeBasedTable.create(); // each line of input is of the following format: // Bill Clinton:D:1993:2001:Al Gore Scanner input = new Scanner(new File("prez.txt")); while (input.hasNextLine()) { Scanner tokens = new Scanner(input.nextLine()); tokens.useDelimiter(":"); String prez = tokens.next(); String[] parts = prez.split(" "); String firstName = parts[0]; String party = tokens.next(); int startYear = tokens.nextInt(); int endYear = tokens.nextInt(); String vp = tokens.next(); // put the data into various Guava collections mmap.put(firstName, prez); if (!vp.equals("No VP")) { bmap.put(prez, vp); } table.put(prez, vp, startYear); } // print a few things at the end to demonstrate the collections System.out.println(mmap.get("William")); System.out.println(bmap); System.out.println(bmap.get("Bill Clinton")); System.out.println(bmap.inverse().get("Adlai Stevenson")); System.out.println(table.get("George Bush", "Dan Quayle")); } }