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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:50:57+00:00 2026-06-12T13:50:57+00:00

I have some design problems with Java Comparator Interface. I have a class which

  • 0

I have some design problems with Java Comparator Interface.

I have a class which contains a Set of a simple custom data structure:

class data {  
    Long ID;  
    int Priority;  
    ...
}

IDs are unique, so it is possible to get the whole data using ID‍‍‍‍‍.

and the container class:

class Container {
    Set<data> mySet = ...;
    List<Long> myList = ...;
    ...
}

for some inevitable reasons, I need to keep a sorted List of data IDs in parallel. I need the List to be sorted by Priority.

Since, the Comparator should compare Prioritys it should implements Comparator<int>. But the List only contains IDs and the Prioritys are not available directly.

This is the problem. There is only ID in the List. Therefore, the Comparator class has no access to Priority.

How can I design such concept?

  • 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-12T13:50:58+00:00Added an answer on June 12, 2026 at 1:50 pm

    You could use something that smells like higher order functions. That is, make a static function that takes a map of sorts from Long to int (which is the priority) or data and returns a new Comparator.

    The class Foo has a static method getComparator which takes an Orange. An Orange is a class that has a method getPriority which takes an ID an return the corresponding priority. The getComparator method constructs a new Comparator object. The new Comparator object’s compare method takes two IDs. It looks up the corresponding priorities of the two IDs and compares them.

    public interface Orange {
        // Looks up id and returns the corresponding Priority.
        public int getPriority(Long id);
    }
    
    public class Foo {
        public static Comparator<Long> getComparator(final Orange orange) {
            return new Comparator<Long>() {
                public int compare(Long id1, Long id2) {
                    // Get priority through orange, or
                    // Make orange juice from our orange.
                    // You may want to compare them in a different way.
                    return orange.getPriority(id1) - orange.getPriority(id2);
            };
        }
    }
    

    My java is a bit rusty so the code may be flawed. The general idea should work, though.

    Usage:

    // This is defined somewhere. It could be a local variable or an instance
    // field or whatever. There's no exception (except is has to be in scope).
    Collection c = ...;
    ...
    Orange orange = new Orange() {
        public int getPriority(Long id) {
            // Insert code that searches c.mySet for an instance of data
            // with the desired ID and return its Priority
        }
    };
    Collections.sort(c.myList, Foo.getComparator(orange));
    

    I have not given an example on how an Orange could look.

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

Sidebar

Related Questions

I need some advice for a design which I have to implement in application.
Lets say we have a program which contains such classes: public interface AbstractItem {
I'm designing a Java application using SWT. I have some tables which will display
We have a Java Swing application which contains components like JTable, JCombobox, JTextArea and
Updated Problem solved, I have some design problem here. The directory looks like that:
I have some sort of a design problem with my Django AJAX application. I
I have a problem trying to design some generic storage.. Basically I have the
I'm setting up a Magento site and have some design requirements that we can't
I have some thoughts on the design of the commanding part in CQRS. I
I have some transitive dependencies in my database design. I have been told by

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.