Search code examples
javatreemap

Treemap Constructor


Is the way I am trying to setup my treemap constructor correct?

import java.util.TreeMap ;

public class Table<K extends Comparable<K>, T> { //K = Key, T = Item

    TreeMap<K, T> tm;

    public Table<K, T> () {
        tm = new TreeMap<K, T>();
    }


    public boolean isEmpty() {
        return tm.isEmpty();
    }

    public int size() {
        return tm.size();
    }

    public void tableInsert(K key, T item) throws TableException {
        tm.put(key, item);
    }

    public boolean tableDelete(K key) {
        if (tm.containsKey(key)) {
            tm.remove(key);
            return true;
        } else {
            return false;
        }
    }

    public T tableRetreive(K key) {
        return tm.get(key);
    } //return null if not found

    public void printTable() {
        TreeMap<K, T> tmclone = (TreeMap<K, T>) tm.clone();
        while (!tmclone.isEmpty()) {
            System.out.println(tmclone.pollFirstEntry());
        }


    } //print in search key order
}

I have another class that will create students, and with the put method will insert into a new map tree.. but the compiler says it was expecting a different character. Also, is the proper way to call the constructor is to input TreeMap blah<K,T> = new TreeMap correct?


Solution

  • This code here:

    public Table<K, T> () {
        tm = new TreeMap<K, T>();
    }
    

    needs to change to this:

    public Table() {
        tm = new TreeMap<K, T>();
    }
    

    Otherwise all good.