A. Masssey
A. Masssey

Reputation: 179

TreeMaps with custom Comparators inside HashMaps

I'm very new to Java, and especially the topics that I'm discussing below, but I've spent a great deal of time trying to work this out and reading answers previously given on this site, and I am not really finding anything directly on point so I thought this might be worth asking.

My question is actually two-fold. The actual problem I'm looking to solve involves building an orderbook program, and I want to sort using price-time priority. The end goal is to build a structure that looks likeHasMap<ticker, TreeMap<Priority,Order>> where Priority is a class I wrote that implements a comparator based first on price and then on time. Doing this has led me to the questions that I'm asking here (the actual set-up of what I'm working on isn't really relevant, but I want to give a sense of why I'm looking at this).

Suppose I want to build a TreeMap with keys K and values V. Then of course TreeMap needs to know how to compare objects in K. My question: If K already implements a comparator, and defines the compare method, will TreeMap read that? Or will I still need to specify the comparator in the constructor? The closest thing I've fond that's on point with this question involves writing a comparator class within the class using the TreeMap, as seen here: Java: SortedMap, TreeMap, Comparable? How to use?

Now, I've actually so far passed the comparator to the constructor, and it seems to be built just fine. But then when I try to pass this into HashMap, I get an error. The code is as follows:

protected Comparator<Priority> priorityCompare;

protected TreeMap<Priority, Order> _buy = new TreeMap<Priority, Order>((Comparator<? super Priority>) priorityCompare);

protected HashMap<String, TreeMap<Priority, Order>> _buyBook;
protected HashMap<String, TreeMap<Priority,Order>> _sellBook;

The problem is that I really want the TreeMaps inside of the HashMaps to use this Comparator, but Java gets angry when I do this. So my second question is: If you want to use a structure like this (HashMap(-,TreeMap)), and you want the TreeMap to use a custom comparator, how do you go about doing that?

EDIT: I mentioned this above, but didn't show what I was talking about. Sorry about that. The situation I would actually like to have is something like:

protected HashMap<String, TreeMap<Priority, Order>((Comparator<? super Priority>) priorityCompare)> _buyBook;
protected HashMap<String, TreeMap<Priority,Order>((Comparator<? super Priority>) priorityCompare)> _sellBook;

That way the TreeMaps know how I would like them to use the Comparator on class Priority. However, upon doing this, it gives me a syntax error (and just tells me to delete these tokens). I have also tried to pass _buy (as written in my initial code box) as the value, but that doesn't work and I immediately realized that was stupid as soon as I had done it. Anyways, the question of how I can get TreeMap to know how I want it to compare keys in K while inside the HashMap above is really the bigger question I have.

Upvotes: 4

Views: 2741

Answers (1)

Jim Garrison
Jim Garrison

Reputation: 86774

If the key of your TreeMap implements equals(), hashCode() and Comparable<Key>, then the TreeMap will use the key class' compareTo() method.

EDIT:

As to your code, I get no errors when compiling with Java7 in Eclipse. I was even able to do this, no cast needed:

protected Comparator<Priority> priorityCompare;
protected TreeMap<Priority, Order> _buy = new TreeMap<Priority, Order>(priorityCompare);

EDIT 2:

Your code as it stands is not valid Java. Consider:

protected Map<String, Map<Priority, Order>> _buyBook = 
    new HashMap<String,Map<Priority,Order>>();
public void init() 
{
    _buyBook.put("key1", _buy);
    // or
    _buyBook.put("key1", new TreeMap<Priority, Order>(priorityCompare));
}

Your declaration of _buyBook instantiates the HashMap only. You have to create instances of TreeMap<Priority, Order> and put() them into the HashMap. You cannot set the comparator until you instantiate each of the contained TreeMaps.

Upvotes: 3

Related Questions