Shared publicly  - 
 
Table: a sparse Map<K1, K2, V>

In the code below, we want to perform an operation for each date (specifically, find the most popular site for that date). But the data store interface is written for the use case of performing an operation for each site. What if a single interface could support both use cases? That's where Table comes in. Table allows us to insert a value indexed by two keys, known as its row and column keys. We can then view the table by row, column, or individual cell:

 Table<LocalDate, String, Integer> dailySiteHits = HashBasedTable.create();
 // The input data is organized by site.
 for (String site : sites) {
   Multiset<LocalDate> hits = visitorsService.getDailyHits(site);
   for (LocalDate date : dateRange) {
     dailySiteHits.put(date, site, hits.count(date));
   }
 }

 // Now we can view the data by date!
 for (LocalDate date : dateRange) {
   Map<String, Integer> siteHits = dailySiteHits.row(date);
   String topSite = keyWithMaxValue(siteHits);
   System.out.printf("Top site for %s: %s\n", date, topSite);
 }

+Chris Povirk, Guava Team
8
4
Add a comment...