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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:57:46+00:00 2026-06-01T14:57:46+00:00

fringe = new PriorityQueue<Node>(10,new Comparator<Node>(){ @Override public int compare(Node node1,Node node2) { if (f(node1)>f(node2))

  • 0
fringe = new PriorityQueue<Node>(10,new Comparator<Node>(){
            @Override
            public int compare(Node node1,Node node2)
            {
                if (f(node1)>f(node2))
                    return 1;
                else
                    return -1;
            }
        });

I declared a PQ to store some nodes and i want to store the nodes in non-decreasing order according to the f value.function f(Node node) is to calculate the f value for the node. so i override the comparator but right now i found that some nodes with larger f value are placed before the ones with smaller f value in the queue, i checked all over but still cannot find what goes wrong, i assume maybe it is the PQ declaration’s problem. Anyone can help me? Thanks in advance!

  • 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-01T14:57:48+00:00Added an answer on June 1, 2026 at 2:57 pm

    As stated by @izomorphius, PriorityQueue does not guarantee full ordering, only that the head is always minimal.

    If you want full order – you might want to chose one of these possiblities:

    1. Use a TreeSet – but note it will not allow duplicates. Also, as stated by @JoonasPulakka, here you will probably want to override equals() and hashCode() as well.
    2. Use a List – populate it [unordered] and then use Collections.sort(List,Comparator) to sort it according to your comparator
    3. use an array [Node[]], populate it [unordered] and then use Arrays.sort() to sort it according to your comparator.

    EDIT:

    Your editted Comparator does not enforce full ordering, and thus the result of using it is undefined:

    let a,b be two Nodes such that f(a) == f(b)

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

    But the javadocs states:

    The implementor must ensure that sgn(compare(x, y)) == -sgn(compare(y,
    x)) for all x and y.

    Thus, the result of using this [the editted] comparator is undefined.

    EDIT2:

    Your comments suggest you are looking for a second criteria – of “adding time”. You can add another field long timestamp to your Node object, and in your Comparator return a result based on this field if and only if f(node1) == f(node2). This will guarantee consistency, and will achieve the wanted feature.

    Note: this field will be initialized once [and only once!] when you add an element to the queue for the first time [or when the object is created, if it’s an option].

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

Sidebar

Related Questions

I created my own color theme using this website . I've added a new
I am new starter android. I have simple question. When I choose date through
I'm trying out sharp-architecture (and ASP.NET MVC) for a new project after being on
I'm trying to determine how many classes are declared in a .java file, using
I need to store few items and its properties in form of a key
I'm having trouble figuring out the best way to store what information my users
I mean to add an attribute element to a node if and only if
I have a bit of a dilemma, which to be honest is a fringe
The situation is akin to the following: Assume I am a store selling fruits,
I'm trying to modify the fridge magnets example by adding a button that will

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.