4

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.

Community
  • 1
  • 1
A. Masssey
  • 169
  • 1
  • 3
  • 10

1 Answers1

3

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.

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
  • Ok that's good to know, but I probably really do want to be able to compare any two Priority objects, instead of just comparing one to a given fixed one. That's why I initially rejected that approach (although I could think through again and see if that would make sense in this context). But without this, we would still need to pass the comparator in the constructor? – A. Masssey Nov 24 '12 at 06:40
  • Not sure what you mean by "instead of ... a given fixed one". If your class implements `Comparable` then the `TreeMap` will be calling the `compareTo()` method on various object instances, as needed to maintain ordering. There's no "fixed comparison" occurring. – Jim Garrison Nov 24 '12 at 06:43
  • Oh I see, that's a good point, and is probably the smarter way to approach this problem now that I think of it. Regarding the edit to your answer above, please see my edit to the original posting, and you can see exactly what it is I'm referring to with passing the comparator in the TreeMap inside the HashMap. I will probably edit my project to use your comparable suggestion; however, getting this data structure to work still seems like an interesting question (to me, at least). :) Thanks for your help so far, by the way. – A. Masssey Nov 24 '12 at 06:51