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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:16:22+00:00 2026-06-12T08:16:22+00:00

Possible Duplicate: Java: strange order of queue made from priority queue I tired to

  • 0

Possible Duplicate:
Java: strange order of queue made from priority queue

I tired to turn a priority queue into a queue by implementing the following comparator:

  • Hack: The QueueComparator makes a PriorityQueue behaves like queue
    (FIFO) by always return 1
  • Since the “natural ordering” of a priority queue has the least
    element at the head and a conventional comparator returns -1 when
    the first is less than the second, the hacked comparator always
    return 1 so that the current (last) square will be placed at the
    tail (recursively)

Here is the code:

import java.util.Comparator;
public class QueueComparator implements Comparator<Square>{
    public int compare(Square square1, Square square2)
    {
        return 1;
    }
}

But the resulting “queue” does not keep things in order(FIFO). Why?

  • 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-12T08:16:24+00:00Added an answer on June 12, 2026 at 8:16 am

    The question is why should it?

    First your Comparator is completely broken, since it violates the basic constraint that

    sign(compare(a,b)) = - sign(compare(b,a))
    

    and

    compare(a,a) == 0
    

    So anything using it might come up with all kinds of results like loosing entries, running in an endless loop, throwing a stackoverflow …

    If you want to implement a IDontGiveAShitComparator it should return 0 all the time. Everything depending on a Comparator should be able to handle that.

    What order results is still up to the implementation. If it stores elements in a list FIFO or LIFO are kind of probable, If it stores in a balanced tree, it will probably add elements always on one side, causing rebalancing of the tree, which will pretty much mix everything up.

    Maybe it uses something based on hashes, in which case all elements with the same priority might come out ordered by their hash values.

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

Sidebar

Related Questions

Possible Duplicate: Java: What is the difference between implementing Comparable and Comparator? Difference between
Possible Duplicate: java for-loop problem Why is the output of following code: for (float
Possible Duplicate: java PreparedStatement Can I make the prepared statement SELECT * FROM STUDENTS
Possible Duplicate: Strange floating-point behaviour in a Java program Why does JSP/JSTL division by
Possible Duplicate: Java string comparison? I have encounter the following problem, I have an
Possible Duplicate: Strange floating-point behaviour in a Java program I came across this weird
Possible Duplicate: Java: Detecting client disconnects from server side What is the best way
Possible Duplicate: Java isEmpty() undefined for String? I copied my code from one java
Possible Duplicate: java thread - run() and start() methods I made a program which
Possible Duplicate: Java Hashmap: How to get key from value? I know that a

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.