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

  • Home
  • SEARCH
  • 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 8901911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:28:43+00:00 2026-06-15T01:28:43+00:00

I am trying to build a treemap, and have implemented my own Comparable interface

  • 0

I am trying to build a treemap, and have implemented my own Comparable interface for the class of keys, but I’m still getting a “cannot be cast to comparable” exception, and I really don’t understand why. Here is the relevant portion of my code:

Interface:

public interface Comparable<T> {

public int compareTo(T o);

}

Class of Keys:

public class PriorityBuy implements Comparable<PriorityBuy>{

protected double _priceDouble;
protected DollarValue _priceDollarValue = new DollarValue(_priceDouble);
protected Price _price = new Price(_priceDollarValue);
protected long _time;

public PriorityBuy(Price price, long time) {

    this._price = price;
    this._time = time;

}

public Price getPrice() {
    return _price;
}

public long getTime() {
    return _time;
}

/**
 * The following provides a new choice of hash function for this class.  The reason is that
 * we need to create a new equals method to match the compareTo method, and we need to know
 * that equal objects return equal hashCode.
 */

@Override
public int hashCode() {
    return (int) (((_price.hashCode())*13)^(_time));
}

/**
 * We re-implement the equals method to match our compareTo method, which depends on both price
 * and on time.
 */

@Override
public boolean equals(Object o) {
    if(! (o instanceof PriorityBuy)) {
        return false;
    }
    PriorityBuy p = (PriorityBuy) o;
    if(p.getPrice().getDollarValue().getValue() == _price.getDollarValue().getValue() && p.getTime() == _time) {
        return true;
    }

    return false;
}

/**
 * We are writing a compareTo method so that this class can implement Comparable<Priority>, which
 * in turn allows any treemap constructed with Priority as the class of keys to order the tree
 * according to the ordering defined by the compareTo method defined below, instead of using
 * the "natural ordering" as it usually does.
 */

@Override
public int compareTo(PriorityBuy a) {

    if(a.getPrice().getDollarValue().getValue() > this.getPrice().getDollarValue().getValue()) {
        return -1;
    }
    else if(a.getPrice().getDollarValue().getValue() < this.getPrice().getDollarValue().getValue()) {
        return 1;
    }
    else if(a.getTime() < this.getTime()) {
        return -1;
    }
    else if(a.getTime() > this.getTime()) {
        return 1;
    }
    else {
        return 0;   
    }
}


}

Portion where I add elements to the tree:

public void addToBuyBook(OrderBookMessage obm) throws Exception {
    Order order = this.createOrder(obm);
    TreeMap<PriorityBuy,Order> buyBookTree = _buyBook.get(obm.getTicker());
    buyBookTree.put(order.getPriorityBuy(), order);
    _buyBook.put(obm.getTicker(), buyBookTree);
    this.addBuyOrder(obm.getOrderID(), obm.getTicker(), obm.getLimitPrice(), obm.getQuantity());
}

Now when I go to test this code using the test:

public void testAddToBuyBook() throws Exception {
    OrderBookManager manager = new OrderBookManager();
    OrderBookMessage obm1 = new OrderBookMessage("IBM");
    OrderBookMessage obm2 = new OrderBookMessage("IBM");

    manager.addToBuyBook(obm1);

    manager.addToBuyBook(obm2);


}

then it returns the exception that PriorityBuy cannot be resolved to comparable. But it seems like I’ve built the program so that this should work. So…..can anyone help me see why it isn’t?

  • 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-15T01:28:44+00:00Added an answer on June 15, 2026 at 1:28 am

    You can’t create your own Comparable interface; you must use the built-in java.lang.Comparable.

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

Sidebar

Related Questions

I was trying Build For Archiving application (from Titanium Mobile) with xCode 4.4, but
I'm trying build a MVC framework, but I'm confused about manage themes. Well... I
I have a std::vector< tr1::shared_ptr<mapObject> > that I'm trying build from data contained in
Im trying to build and automated log in using Googles authSub, but I need
Trying to build my android project with Buildr (Apache), but can't find any info
Trying to build my own user control: <%@ Control Language=C# AutoEventWireup=true CodeFile=TabMenu.ascx.cs Inherits=TabMenu %>
Im trying to build a simple, lightweight product viewer using jquery. I have a
Trying to build a linq query based on parameters a user selects (reporting) but
Trying to build a dynamic grid with my json store, but for some reason
I'm currently trying to build the spring-security-oauth sample apps (for OAuth2), but the test

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.