[英]Filtering 2 array composite object elements using java 8 stream
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. 我有2个数组对象,第一个是复合对象,另一个是一个简单的对象数组,根据找到匹配的一些比较,创建一个新的列表。 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
. 我使用java 1.7
版本做到了这一点,但是如果使用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]
of accountOid
array (simple array) accountOid[i]
的accountOid
阵列(简单阵列) accts[j].getAccountOid()
of AccountHoldCompisiteObj
array (composite array) accts[j].getAccountOid()
的AccountHoldCompisiteObj
阵列(合成阵列) I suppose your goal is to transform the inner for-loop
with the outer one. 我想你的目标是将内部for-loop
转换for-loop
外部for-loop
。 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. 实际上,您所做的只是将从每个AccountHoldCompositeObject
接收的ObjectId
对象与一组预定义的String
OID作为要比较的ObjectId
的模板进行比较。 The .clone()
is not needed. .clone()
。
Before entering Stream I recommend prepare data first for the fluent and easy manipulation and usage. 在进入Stream之前,我建议首先准备数据,以便流畅和简单地操作和使用。 The last you want is to perform the complicated transformation inside a lambda. 你想要的最后一个是在lambda中执行复杂的转换。 Do: 做:
Set<ObjectId>
of the transformed OIDs ( String
to ObjectId
). 定义已转换的OID( String
to ObjectId
)的集合Set<ObjectId>
。 Set<ObjectId>
. 迭代列表并搜索预定义的Set<ObjectId>
可用OID的列表。 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
There is lots of for loop , I added new code like below. 有很多for循环,我添加了如下的新代码。 I commented out some lines to understand whats going on there. 我评论了一些线路,以了解那里发生的事情。
public static void main(String[] args) {
final List<AccountHoldCompositeObj> accts = prepare();
Set<ObjectId> setOfAccountObjectId = IntStream.range(1, 8) //Int stream range between 1-8
.map(str -> new ObjectId(str)) // Create ObjectId
.collect(Collectors.toSet()); // Collect into Set
accts.stream() // Stream for AccountHoldComposite array
.filter(acct -> setOfAccountObjectId.contains(acct.getAccountOid())) // Filter elements by existing ids
.collect(Collectors.toList())
.forEach(System.out::println);
}
private static List<AccountHoldCompositeObj> prepare() {
int k = 10;
//creating AccountHoldCompositeObj elements between 0-50
return Stream.iterate(0, m -> m + 1)
.limit(50)
.map(currentId -> {
AccountHoldCompositeObj create = new AccountHoldCompositeObj();
create.setAccountId(Integer.toString(k));//some more elements
create.setAccountOid(new ObjectId(currentId));
return create;
})
.collect(Collectors.toList());
}
Edit : I added Stream.iterate
for your for loop
which is between 0 and 50
. 编辑:我为你的for loop
添加了Stream.iterate
,介于0 and 50
之间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.