简体   繁体   中英

How to associate two ArrayLists of objects?

Let's say I've got this two ArrayLists , the first one is of the type ObjectA , the second ObjectB .

These are ObjectA's variables:

 int id;
 double value;

And these are ObjectB's variables:

 int objAId;
 double disccountValue;

How can I retrieve the disccountValue of ObjectB where objAId is the same of the item on the first list? Just like in a SQL query, but in ArrayLists .

If you're trying to get the value for each object A:

for(ObjectA objA : arrayListOfObjectAs){
    int id = objA.id;
    for(ObjectB objB : arrayListOfObjectBs){
        if(id == objB.objAId){
            //do what you want with the objB.discountValue here
        break;
        }
    }
}

If you're only looking for a single id you can get rid of that outer for loop

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