@w1024020103
        
        2017-03-12T15:46:55.000000Z
        字数 1732
        阅读 622
    CS61B

For the first time I used HashMap to store the tables as values using String and Integer as key respectively.
public class Catalog {private HashMap<String,Table> nameHash;private HashMap<Integer,Table> iDHash;public class Table{public String tableName;public String pkeyFieild;public DbFile tableFile;public Table(String name, String pkeyFieild, DbFile file){this.tableName = name;this.pkeyFieild = pkeyFieild;this.tableFile = file;}}/*** Constructor.* Creates a new, empty catalog.*/public Catalog() {// some code goes herethis.nameHash = new HashMap<>();this.iDHash = new HashMap<>();}
When implementing method like addTable(), I used put() method for HashMap for the fisrt time. Just use hashmap.put(key,value) to put a value to a key in a hashmap.
public void addTable(DbFile file, String name, String pkeyField) {// some code goes hereTable t = new Table(name, pkeyField, file);nameHash.put(name,t);iDHash.put(file.getId(),t);}
When implementing method like getDbFile(int tableid), I used get(key) method for HashMap. Just use hashmap.get(key) then you can get the corresponding value.
public DbFile getDbFile(int tableid) throws NoSuchElementException {// some code goes hereTable tableToGetDbFile = iDHash.get(tableid);if(tableToGetDbFile == null){throw new NoSuchElementException("the table doesn't exist");}else {return tableToGetDbFile.tableFile;}}
When writing tableIdIterator(), things become super easy for HashMap, because the key for HashMap data structure has to be a list or set that are iterable. So I can just return hashmap.keySet().iterator() to get an Iterator of hashmap's keys.
public Iterator<Integer> tableIdIterator() {// some code goes hereSet<Integer> keys = this.iDHash.keySet();return keys.iterator();}
Finally, there's a clear() method to delete all the info for a hashmap.
/** Delete all tables from the catalog */public void clear() {// some code goes herethis.iDHash.clear();this.nameHash.clear();}}
