Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8892155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:55:22+00:00 2026-06-14T22:55:22+00:00

I’m very new to Java, and especially the topics that I’m discussing below, but

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T22:55:23+00:00Added an answer on June 14, 2026 at 10:55 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to create an if statement in PHP that prevents a single post
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.