CPSC 225, Fall 2007
Lab 11: Using the TreeMap Class


The lab for this week is a short exercise on using a Map. For the rest of the term, you should be putting most of your programming effort into your final project, so labs will be short and will not count for many points. However, I will also expect you to start producing code for your project, and I will ask to see that code at several times during the remainder of the term. I might even grade that code as part of your lab grade.

You should be able to finish the lab during class, but the lab will not be graded until after Thanksgiving break. It will be due at the same time as Lab 12. You might want to make a single Eclipse project for both labs, named, for example, "Labs 11 and 12".


A Map for Mime Types

You were introduced to mime types in the previous lab. In that lab, you used a short list of file extensions and their associated mime types. This list was used to look up the mime type associated with a given file extension. In fact, there are hundreds of file extensions that have associated mime types. When working with the full list, it would be natural to store the file-extension / mime-type pairs in a Map. This would make looking up the mime-type for a given extension much more efficient -- and the look up could be done in one line using the map's get() method.

On our Kubuntu Linux systems, the known mime types are stored in the file /etc/mime.types. However, they are not stored in the format that we need (that is, as file-extension / mime-type pairs). The format of this file is as follows: It contains some blank lines and comment lines that start with the character '#'; these lines don't contain any mime-type information and should be ignored. The other lines contain one or more tokens. The first token is a mime type. Additional tokens, if they exist on the line, are file extensions associated with that mime-type. A line that contains only a mime type can be ignored for our purposes, since that mime type has no file associations. For example, the line

          image/jpeg                    jpeg jpg jpe

says that files that end with any of the extensions 'jpeg', 'jpg', or 'jpe'. have mime-type 'image.jpeg', meaning that they are JPEG image files. Thus, this line corresponds to three file-extension / mime-type pairs.

For this lab, you should write a program that reads the file /etc/mime.types and makes a Map containing all the file-extension / mime-type pairs that are found in the file. Use a variable of type TreeMap<String,String>. (The first String is the file extension; the second is the mime type. In map terminology, the file extension is the key while the file extension is the value. The map makes it possible and efficient to look up the value associated with any given key.) Using a TreeMap means that the pairs are stored in alphabetical order by file extension.

Suggestion: Use a Scanner to read the file line-by-line. For non-blank, non-comment lines, create another Scanner (or StringTokenizer) to read the tokens from that line.

After reading all the data from the file into the map, you should print a list of file-extension / mime-type pairs from the map. You can print the list to System.out. Each line of output should contain one file extension and the associated mime type. You can do this easily with a for-each loop; however, you can't simply iterate through a map, only through a collection (that is, a list or set). If you think about it, you will see that you really want to iterate through the set of keys in the map (since given a key, you can always get the associated value from the map). If map is a variable of type Map, then the function map.keySet() returns a set containing all the keys from the map. (If the map is a TreeMap, than the key set will also be sorted.) This means that you an can say something like this to iterate through all the keys in the map:

           for ( String key : map.keySet() ) {
              // so something with the key
           }

David Eck, for CPSC 225