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 8739487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:56:49+00:00 2026-06-13T10:56:49+00:00

When I try to add more than one element to a PriorityQueue in my

  • 0

When I try to add more than one element to a PriorityQueue in my Java class, the following exception is being thrown:

java.lang.ClassCastException

with the addition of the following message “Event cannot be cast to java.lang.Comparable

I get this expression when I’m trying to add two Event objects to the priority queue. Here’s how I initialize the priority queue etc. Maybe there’s an error in my construction of it, because it’s the first time I’m using this:

//the instance field
private PriorityQueue<Event> queue;

//Then in the constructor
queue = new PriorityQueue<Event>();

I’m just testing it out in the main method but this is when I get the above error:

public static void main(String[] args) {
    SimEngine engine = new SimEngine();
    Event event1 = new Event();
    Event event2 = new Event();

    engine.getQueue().offer(event1);
    engine.getQueue().offer(event2);

    System.out.println("Queue size" + engine.queue.size());

}

NOTE: I’ve tried both to invoke add and offer when trying to add to the queue. I get the same error.

All of the above code is in my SimEngine class. I know the queue needs to know how to order these elements, but I thought if you don’t specify any priority then it would just sort them naturally? Could someone please tell me what I’m doing wrong here thanks.

  • 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-13T10:56:51+00:00Added an answer on June 13, 2026 at 10:56 am

    Event needs to implement the Comparable<Event> interface. That means you need to write the compareTo(Event) method. There is no “natural” ordering for objects which do not implement Comparable.

    The other option is to pass a Comparator<Event> implementation to the queue when the it is constructed, to tell the queue how to compare Event instances.

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

Sidebar

Related Questions

I am new to this and when I try to add more than one
I try to add a class to a td-element using javascript with the internet
When i add more than one Fieldable to my Document i get a nullpointer
When I try to add more than 3 SERIES in a LINE or SCATTER
5 and I try to solve the more than one entity situation using a
I try to add a class to a link with jquery <a rel=/Career/ href=/Career/></a>
I everyone, I would like to add one or more field on my register
I'm working on a site that will have more than one 'Save' button per
I am trying to get my datagridview to display more than one row of
I have a view which interacts with more than one controller (post, comments, categories...)

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.