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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:56:51+00:00 2026-05-29T09:56:51+00:00

public ClassA { private String firstId; private String secondId; public void setFirstId(String firstId) {

  • 0
public ClassA
{
private String firstId;
private String secondId;

public void setFirstId(String firstId) {
           this.firstId = firstId;
   }


   public String getFirstId() {
           return id;
   }

   public void setSecondId(String secondId) {
           this.secondId = secondId;
   }


   public String getSecondId() {
           return secondId;
   }
}



public ClassB
{
private String firstId;
private String secondId;


   public void setFirstId(String firstId) {
           this.firstId = firstId;
   }


   public String getFirstId() {
           return id;
   }

   public void setSecondId(String secondId) {
           this.secondId = secondId;
   }

   public String getSecondId() {
           return secondId;
   }
}

I have the above 2 classes(POJOs) which are absolutely the same (except for the name of course) which I am adding to two arraylists: aListA and aListB. I need to compare if the two objects are the same. If they are same I need to add them to another list(commonList) and if they happen to be the different, I need to add them to another list(differentList). I have written the following code:

ClassA clsA = null;
public ArrayList genCommonList(aList, bList)
{

for(int i = 1; i<aList.size(); i++)
{
clsA = new ClassA;
Object obj = aList.get(i);
clsA = (ClassA)obj;
if(bList.contains(clsA)
{
genCommonList.add(clsA);
}
}

public ArrayList genDifferentList(aList, bList)
{

for(int i = 1; i<aList.size(); i++)
{
clsA = new ClassA;
Object obj = aList.get(i);
clsA = (ClassA)obj;
if(!bList.contains(clsA)
{
 genDifferentList.add(clsA);
}
}

My problem is that even when the data(both variables, firstId and secondID) in the 2 different POJOs are the same, they get put in the different list. That means the objects are not being compared for equality using the contains() method of the ArrayList class. I dont want to get the data from each variable inside the POJO and compare. Is there a way to compare the POJOs for both variables and be determined as either equal or unequal?

  • 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-29T09:56:52+00:00Added an answer on May 29, 2026 at 9:56 am

    Eventhough content is same they are different objects which doesn’t satifsy equal condition. You may need to do explicit a.getfirstId().equals(b.getfirsId()) and a.getscondID().equals(b.getSecondId()) by iterating.

    The other possibility may be overriding equals and hashcode methods in both POJO classes. Refer this link for equals/hashcode contract.

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

Sidebar

Related Questions

I have this class: private static class ClassA{ int id; String name; public ClassA(int
public class abc1 { private String s; public abc1(String s){this.s=s;} public static void main(String
I have this: public class accounts { private string mName; private string mEmail; private
I have this class defintion: @XmlRootElement public class RssRoot { private String _version; private
class ClassA { public delegate void WriteLog(string msg); private WriteLog m_WriteLogDelegate; public ClassA(WriteLog writelog)
I have class like this: public class Class1 { private String result; private String
i have a java class like this public class A { private String field1;
Imagine I have this: public class Animal { private String racaAnimal; private String corAnimal;
Consider this: public class TestClass { private String a; private String b; public TestClass()
I'm used to writing classes like this: public class foo { private string mBar

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.