简体   繁体   中英

How to join two ArrayList in Java

I have two objects which contains array of dictionary. Each entry of HashMap in Array will have around 40-50 fields. Object 2 HashMap might contains different keys but there will be one key common between both objects.

object1 = ArrayList<HashMap<String, Object>>

object2 = ArrayList<HashMap<String, Object>>

Object1 HashMap is having few fields Field1, Field2, Field3, field4...

Object2 HashMap is having few fields Field4, Field5, Field6, field7..

I want to join both object on common field4 and result array should have all fields 1 to 7

Output should be same as sql query.

Select * from object1, Object2 where object1.field4 = object2.field4

If object1 is having 3 rows and Object2 is having 5 rows then output list will have 15 rows if all keys are matching. matching field4 will not be unique in both objects.

You can simply do something like:

List<HashMap <String , Object >> listFinal = 
               new ArrayList<HashMap < String , Object >>();
listFinal.addAll(object1);
listFinal.addAll(object2);

Here I use the addAll() method to add the two lists.

For more information you can refer here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM