Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I combine 2 array lists of objects having same property value in java

Tags:

java

I have 2 array lists as follows. One array contains student information mapped with id, name and city. The other array contains the marks details mapped with id, marksandgrade. The goal is to compare each of these elements against each other and combine them based on the common field id in both the ArrayList`.

student_details=[
        {
            "id": 1,
            "name": "sam",
            "city": "Chennai"
           
        },
        {
            "id": 2,
            "name": "peter",
            "city": "Chennai"
            
        }
    ]
student_grades=[
        {
            "id": 1,
            "marks": 95,
            "grade": "A"
           
        },
        {
            "id": 2,
            "marks":63,
            "grade": "B"
            
        }
    ]

The result should look as follows.

[
        {
            "id": 1,
            "name": "sam",
            "city": "Chennai",
            "marks": 95,
            "grade": "A"
           
        },
        {
            "id": 2,
            "name": "peter",
            "city": "Chennai",
            "marks":63,
            "grade": "B"
            
        }
    ]

I used the below code to achieve the same in javascript. How do I implement this in java?

async mergeData(data1, data2, key) {
    const result = data1.map(a =>
      Object.assign({}, a,
        data2.find(b => b[key] === a[key])
      )
    )
    return result
  }
like image 522
monica_s Avatar asked Sep 17 '26 16:09

monica_s


1 Answers

If the attributes your data contains are always the same, then the easiest way would be to create an ArrayList and then call the toArrayMethod after all the entries were added to the ArrayList, otherwise you have to account for different cases where the id's don't match which would need a lot of code to account for different Array Sizes.

Then you simply iterate in 2 nested for loops over the arrays you were given and if the id's match, then you use the add method of ArrayList to add a new entry to your ArrayList.

The other question is what object type your data actually has, because I don't think you can create object pairs like of the nature of "attribute_name" = value in java, so your input is probably some json you're reading in. You could use gson or other libraries to map your json to 2 java classes, then use another 3rd class for the combined data.

Another way would be to map it yourself with a json handling library to a java class or some datastructure like a HashMap. A HashMap would make most sense if you have no clue what attributes your java class would have in the future.

Another way would be to map the object from json to another json with a json handling library, with 2 for loops iterating over your json arrays.

like image 69
HopefullyHelpful Avatar answered Sep 19 '26 06:09

HopefullyHelpful



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!