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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:58:00+00:00 2026-06-15T18:58:00+00:00

Say I’m creating a library which provides amongst other stuff a priority-queue class. The

  • 0

Say I’m creating a library which provides amongst other stuff a priority-queue class. The user instantiates one and implements a Comparator interface which is then passed gently to the priority-queue.
I want to:

1. grant the user the possibility to define the Comparator class easily – by implementing it as an anonymous class, just like this example suggests:

    PriorityQueue<int> pq = new PriorityQueue<int>();
    pq.setComparator(new Comparator<int>() {
                @Override
                public int compare(int i1, int i2){
                     if(i1 < i2) return -1;
                     else if(i1 > i2) return 1;
                     else return 0;
                }
             };);

2. grant the user the possibility to serialize-and-deserialize the priority queue ALONG with its attached comparator.

3. use only JDK to achieve this, no other external libraries

What approach would be best to achieve this?

Currently I’m having problems deserializing the Comparator class, more specifically creating an instance of it since it’s private within the class that created it (that “owns” it) and it also does not have a null-constructor (this is not really a big problem since I can use the available constructors that it exposes).

Thanks for any suggestions 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-15T18:58:01+00:00Added an answer on June 15, 2026 at 6:58 pm

    Document the class, explaining that for the queue to be serialized properly, the comparator should be serializable, and preferrably not an non-static inner class, since this would also cause the serialization of its enclosing object. Also document the fact that the comparator class must be available when deserializing the queue, of course.

    java.util.TreeSet has the same “problem” as the one you have: it takes a comparator as argument, stores it as part of its internal state, and it is serializable. FindBugs generates a warning when you pass a non-serializable comparator to a TreeSet constructor.

    I don’t think you can do better than that.

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

Sidebar

Related Questions

Say I have a queue full of tasks which I need to submit to
Say I have a class called Money which has parameters Dollars and Cents I
Say I have two basic classes, one with a ManyToMany relationship to the other:
Say I have multiple objects laying on each other, if I click on one
Say I have a select box eg <div data-bind='visible: someProp'> <select class=selectSubWidgets data-bind='options: subWidgets,optionsText:
Say I have classes class A{ //code for class A } class B{ //code
Say, we have: <div class=outer> <div class=inner> <span class=text>Hello!</span> </div> </div> The 'outer' div
Say I have two classes like this: class A{ private static Random random =
Say I have a method getBook() within the class Bookshelf. The method should only
Say that I have a web user control that has several drop down lists

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.