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]
ofaccountOid
array (simple array)accts[j].getAccountOid()
ofAccountHoldCompisiteObj
array (composite array)
I suppose your goal is to transform the inner for-loop
with the outer one. Practically, all you do is a comparison of the ObjectId
object received from each of AccountHoldCompositeObject
with a group of predefined String
OIDs as a template for the ObjectId
to be compared. The .clone()
is not needed.
Before entering Stream I recommend prepare data first for the fluent and easy manipulation and usage. The last you want is to perform the complicated transformation inside a lambda. Do:
- Define a set
Set<ObjectId>
of the transformed OIDs (String
toObjectId
). - Iterate the list and search ones with OID available in the predefined
Set<ObjectId>
. - Collect the remaining to a desired output.
Here you go:
Set<ObjectId> idSet = Arrays.stream(accountOid) // Stream over String[]
.map(str -> new ObjectId(str)) // Create ObjectId from each
.collect(Collectors.toSet()); // Collect to Set<ObjectId>
List<AccountHoldCompositeObj> list = Arrays
.stream(accts) // Stream over AccountHoldCompositeObj[]
.filter(acct -> idSet.contains(acct.getAccountOid())) // Filter elements by IDs
.collect(Collectors.toList()); // Collect to List