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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:58:59+00:00 2026-05-11T17:58:59+00:00

I have a class that I would like to use in a scala.collection.mutable.PriorityQueue, but

  • 0

I have a class that I would like to use in a scala.collection.mutable.PriorityQueue, but I don’t want to make it Ordered[A] just for this one purpose. I don’t consider the ordering I want to use with respect to the PriorityQueue as the natural ordering of the class.

class MyObject (sequence: Int, values: List[String]) ...

So, in my PriorityQueue, I would like the values to be ordered by ‘sequence’. However, just because two objects have the same sequence doesn’t make them naturally equal since the contents of their ‘values’ may be different.

This is where, in Java, it’s nice to be able to supply an alternate Comparator object to the PriorityQueue. My Comparator would simply order the objects with respect to their ‘sequence’ and ignores their ‘values’.

The PriorityQueue class must be parameterized with a “A <% Ordered[A]”

class PriorityQueue[A <% Ordered[A]] extends ... 

From what I’ve read, this means my class must extend Ordered[A] or I must provide an “implicit def” type conversion to Ordered[A], which, honestly, feels inelegant.

The Java solution seems more “functional” allowing me to pass a Comparator function-like object instead of forcing me into a class hierarchy or monkeypatching my class.

I realize there are alternatives to using PrioirityQueue, but I feel like I may be bumping up against the Scala learning curve here and don’t want to give up without exploring this design decision fully.

Is this just an unfortunate decision in the Scala library or am I misunderstanding some sort of calling convention that makes PriorityQueue more usable and ‘functional’?

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-05-11T17:58:59+00:00Added an answer on May 11, 2026 at 5:58 pm

    The syntax

    class PriorityQueue[A <% Ordered[A]] ...
    

    is really just a light sugaring on top of

    class PriorityQueue[A]()(implicit convert: A => Ordered[A]) ...
    

    This means you can write your own method A => Ordered[A]

    case class Foo(n: Int)
    def orderedFoo(f: Foo): Ordered[Foo] = new Ordered[Foo] {
      def compare(other: Foo) = f.n.compare(other.n)
    }
    

    And manually pass it into your PriorityQueue constructor

    new PriorityQueue[Foo]()(orderedFoo)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a static class that I would like to raise an event as
I have a class in silverlight that I would like to store to disk.
I have a class that is marked with DataContract attributes and I would like
I have an actionscript file that defines a class that I would like to
I have a class that implements multiple interfaces. I would like to register these
I have a class that is called regularly by several objects. I would like
I have a class that contains decoded video frames. I would like my decoder
I have a database that I would like class files generated from, and also
I have a custom-made view that extends the View class. I would like 2
Can I use scala List in Java, like : import scala.collection.immutable.List; class HelloScalaList {

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.