I have 2 array objects, the first one is composite and another one is a simple array of object, based upon some comparison if match found, create a new list. That would be a filtered list based upon conditions. I did that using java 1.7 version, however looking for guidance if this can be achievable with less code using java-8.
Here is my code which creates a new list based upon a conditional filter
public static void main(String[] args) throws CloneNotSupportedException {
String[] accountOid = {"1","2","3","4","5","6","7","8"};
AccountHoldCompositeObj[] accts = prepare();
ArrayList<AccountHoldCompositeObj> list = new ArrayList<AccountHoldCompositeObj>();
for (int i = 0; i < accountOid.length; i++) {
for (int j = 0; j < accts.length; j++) {
ObjectId oid = new ObjectId(accountOid[i]);
if (oid.equals(accts[j].getAccountOid())) {
AccountHoldCompositeObj anAccount = (AccountHoldCompositeObj) accts[j].clone();
list.add(anAccount);
break;
}
}
}
list.stream().forEach(s->System.out.println(s));
}
private static AccountHoldCompositeObj[] prepare() {
AccountHoldCompositeObj[] valArr = new AccountHoldCompositeObj[5];
int k =10;
for (int i=0;i<50;i++){
AccountHoldCompositeObj create = new AccountHoldCompositeObj();
create.setAccountId(Integer.toString(k));//some more elements
create.setAccountOid(new ObjectId(i));
valArr[i] = create;
k++;
}
return valArr;
}
I tried to convert array in stream and then used filter, but somehow not able to compare exactly 2 different elements, that is :
accountOid[i]ofaccountOidarray (simple array)accts[j].getAccountOid()ofAccountHoldCompisiteObjarray (composite array)