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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:17:37+00:00 2026-05-31T05:17:37+00:00

I have an ArrayList. It will have duplicate objects in that arraylist. The duplicate

  • 0

I have an ArrayList. It will have duplicate objects in that arraylist. The duplicate is found on a “firstId” field. There will be one another field,
but that will not be used for comparing object equality. Once I find the duplicate I need to combine the info from both and populate a different object with 3 fields.

This is what I would like to do

ClassA
{
private string firstId;
private String secondId;

//equals() and hashcode() override code for firstId;
}

ClassB
{
private string firstId;
private String secondId;
private string thirdID;
}


Class A cls1A = new ClassA();
Class A cls2A = new ClassA();

ClassB clsB = new ClassB();


if(cls1A.equals(cls2A))
{
clsB.setFirstId(cls1A.getFirstId());
clsB.setSecondId(cls1A.getSecondId());
clsB.setThirdId(cls2A.getSecondID());
}

I have written quite a bit of code to identify the duplicates(basically trying to add it to a hashset and then again getting it from both the arraylist and hashset object, matching each one and trying to create the third object. But there are few bugs and also it makes my program very slow. Is there a better solution to this?

  • 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-31T05:17:38+00:00Added an answer on May 31, 2026 at 5:17 am

    You really want an easy way to group objects with the same firstId value together. Two possible approaches:

    1. Sort your ArrayList (using a Comparator that looks only at firstId), then iterate over the sorted list, keeping a reference to the last item you found. If the last item and the current item match on firstId, create a new ClassB.

    2. Instead of a HashSet, use a HashMap with String key and Set values. Iterate over ArrayList, storing in it a mapping from firstId values to all the secondId values you find with that firstId.

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

Sidebar

Related Questions

I have an ArrayList that I want to use to hold RaceCar objects that
I have an ArrayList<MyObject> that may (or may not) contain duplicates of MyObject I
Possible Duplicate: Java: how to clone ArrayList but also clone its items? I have
I have created an arraylist that is made up of custom objects. Basically the
I have problem adding arraylist to list view, will explain about my problem here..
So I have this method which will take an ArrayList of Integers, I compare
I have arraylist myorderdata . I want to retrieve this arraylist to one String
At the moment inside the class I have ArrayList which the objects are stored
I have an arraylist that gets different type of values in it, 1st value->
Possible Duplicate: How to clone ArrayList and also clone its contents? I have a

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.