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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:44:24+00:00 2026-06-07T04:44:24+00:00

I want to write a java program which has to find the differences between

  • 0

I want to write a java program which has to find the differences between the two objects which is of different instances. I have implemented it using equals() and comparator. But here I want to find differences and have to show that in logging format.

My program is below :

public class A implements Comparator<A>{

private int id1, id2;

/* setters and getters for id1 and id2 */

     public boolean equals(Object arg0) {
    if (this.getClass() != arg0.getClass()) {
        return false;
        }

        if (((A) arg0).getId1() == this.id1 && ((A) arg0).getId2() == this.id2) {
        return true;
        }

        return false;
     }

 public static void main(String args[]) {

    A obj1 = new A();
    obj1.id1 = 10;
    obj1.id2 = 20;

    A obj2 = new A();
    obj2.id1 = 30;
    obj2.id2 = 20;

    /*
     * equals comparison
     */

    if (obj1.equals(obj2)) {
        System.out.println("EQUALS");
    } else {
        System.out.println("NOT EQUALS");
    }

}

Please can any body advise me how i can find the differences and show that in logging format.

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-06-07T04:44:26+00:00Added an answer on June 7, 2026 at 4:44 am

    To implement Comparator<A> you need a method public int compare(A o1, A o2). This is an example of such an implementation:

    @Override
    public int compare(A o1, A o2) {
        if (o1 == o2) {
            return 0;
        } else if (o1 == null) {
            return -1;
        } else if (o2 == null) {
            return 1;
        }
        if (o1.getId1() != o2.getId1()) {
            return o1.getId1() - o2.getId1();
        } else {
            return o1.getId2() - o2.getId2();
        }
    }
    

    Then you can use it like this:

        if (obj1.compare(obj1, obj2) == 0) {
            System.out.println("EQUALS");
        } else {
            System.out.println("NOT EQUALS");
        }
    

    It is probably more common to make another class implement the Comparator<A>, instead of putting this in A itself.

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

Sidebar

Related Questions

I have a dummy Java Program, which I want to write in Clojure. It
HI I want to write a java program by which I can delete all
In a Java program, I have an array of objects. Each object has a
I am writing a java program in which I have to write all integers
I want to write a java program that acts as a user interface to
Hi I want to write a java program where I will provide my email
I'm a beginner and I want to write Java code in eclipse. This program
I want to write a program, which search some HTML addresses. I assume that
I want to write a simple java agent which can print the name of
I need to write a java program which when executed pushes a command into

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.