tags:

views:

177

answers:

2

Hi. I'm having some problems with TreeMap constructors. I have a class with 2 TreeMaps<Client> inside it. A tree sorted by name and another sorted by number.(client class : String name, int number, ...)

private TreeMap<String, Client> nameTree;
private TreeMap<Integer, Client> numberTree;

How do i build the constructors for this class? So far i wrote this:

public ManagerTreeMap(){
        nameTree = new TreeMap<String, Client>(new StringComparator()); 
        numberTree  = new TreeMap<Integer, Client>(new IntegerComparator()); 
    }

My major problem is the construtor "TreeMap(Comparator c)". Can i write two comparators? if not what do i have to do?

    public ManagerTreeMap(Comparator<String> cp){
       nameTree = new TreeMap<String, Client>(cp);          
    }

    public ManagerTreeMap(Comparator<Integer> cpt){
       nameTree = new TreeMap<Integer, Client>(cpt); 
    }
+1  A: 

Maybe

public ManagerTreeMap(Comparator<String> cs, Comparator<Integer> ci){
   nameTree = new TreeMap<String, Client>(cs);          
   numberTree  = new TreeMap<Integer, Client>(ci); 
}
Thilo
+3  A: 

It seems that you don't need custom comparators.

public ManagerTreeMap(){
    nameTree = new TreeMap<String, Client>(); 
    numberTree  = new TreeMap<Integer, Client>(); 
}
newacct
Not for simple types like String and Integer. If you need a case-insensitive comparator for String you can just pass in String.CASE_INSENSITIVE_ORDER to the TreeMap constructor.
banjollity