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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:12:08+00:00 2026-06-15T06:12:08+00:00

If I do the following myObject.myMethod(myClass.getComparator()); with public void myMethod(Comparator<? super myOtherObject> comparator) {

  • 0

If I do the following

myObject.myMethod(myClass.getComparator());

with

public void myMethod(Comparator<? super myOtherObject> comparator) {
if (comparator.equals(myClass.getComparator()) {
 //do sth
}
}

and in myClass

   static Comparator<ListItem> getComparator() {
        return new Comparator<myOtherObject>() {
            public int compare(myOtherObjectitem1, myOtherObjectitem2) {
                return (Integer.valueOf(myOtherObject.getRating()).compareTo(Integer.valueOf(myOtherObject.getRating())));
            }
        };
    }

then “//do sth” is not gonna be executed. So the objects I get from getComparator the two times are different. How can that be? Is there a chance to see, which comparator “myMethod” gets?

  • 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-15T06:12:11+00:00Added an answer on June 15, 2026 at 6:12 am

    You’re calling the equals method on this line:

    if (comparator.equals(myClass.getComparator())
    

    Since you haven’t defined this method explicitly on your Comparator class (which is an anonymous inner class), this defaults to the version inherited from Object – which considers two references equal only if they are the exact same object.

    And your getComparator() method states return new Comparator() { ... }, so it’s calling the constructor and creating a new object each time it’s called. Thus the result of one call to getComparator will be a distinct object, and hence will not be considered equal to, the result of another call.

    I can think of two possible ways to change your code so that the equality test returns true:

    1. Create the comparator only once, and return this same object from
      getComparator. This would involve a change somewhat like the
      following in myClass:

          private static Comparator<ListItem> cmp = new Comparator<myOtherObject>() {
             public int compare(myOtherObjectitem1, myOtherObjectitem2) {
                  return (Integer.valueOf(myOtherObject.getRating()).compareTo(Integer.valueOf(myOtherObject.getRating())));
              }
          };
      
          static Comparator<ListItem> getComparator() {
              return cmp;
          }
      
    2. Provide an explicit equals() implementation (and thus a hashCode() one too, ideally). You can then control exactly which objects are considered equal to one of your comparators. This might be much easier if you define a concrete class for your comparator rather than it being an anonymous inner class.


    At the end of the day, though, I fear your approach might not be right. What does it mean for two comparators to be equal to one another? I feel this is an ambiguous concept for anything other than data classes, and I would be hesitant to use the Object.equals method for this.

    (For example, if by equality you mean “they will sort lists in the same order”, then I’d add a method to your comparator class called isEquivalentSortOrder or something similar. This way you can specify exactly what you mean without having to rely on the woolly definition of “being the same”.)

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

Sidebar

Related Questions

Given the following: class ParamClass {...}; class MyObject { public: void myMethod(ParamClass const& param)
I have the following interface public interface IHandler<T> { void Handle(T myObject); } I'd
In my program I have a function of the following template: public MyObject myMethod()
I have the following code: @POST @Path(query) @Produces(MediaType.APPLICATION_JSON) public MyObject myMethod(@FormParam(id) String id) {
I'm trying to perform the following cast private void MyMethod(object myObject) { if(myObject is
Suppose I have the following: public class MyObject { public string Name {get; set;}
I have the following code: public class MyObject { public string Name { get;
Assuming the following view model definition: public class MyObject { public string Name {
I got the following code that generates a DLL : public class MyObject :
I have the following attempt at an immutable object: class MyObject { private static

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.