[英]What is the best approach to split an Arraylist based on their values
I would like to split an ArrayList
that I am looping trough and set a field called active
which can be true
or false
. 我想拆分一个我正在循环的
ArrayList
,并设置一个名为active
的字段,可以是true
或false
。 But at the end of loop I would like to split this collection in two groups.. active = false
and active = true
, so doing this I won't need to search in database twice.. 但是在循环结束时,我想将这个集合分成两组.active
active = false
和active = true
,所以这样做我不需要在数据库中搜索两次。
for example: 例如:
private List<Classes> searchClasses(ClassItems listItems) {
List<ClassItem> items = new ArrayList<ClassItem>();
for (Iterator<ClassItem> iterator = listItems.getItems().iterator(); iterator.hasNext();) {
ClassItems item = iterator.next();
ClassEntityManager classEnt = ClassEntityManager.search(item.getId);
if(classEnt.active()){
item.setActive(true);
items.add(item);
}
}
return items;
}
What is the best approach to do this?? 这样做的最佳方法是什么?
Make two lists instead of one. 制作两个列表而不是一个。
if(classEnt.active()) {
activeItems.add(item);
item.setActive(true);
} else {
inactiveItems.add(item);
}
Use two collections, one for actives and the other for not actives. 使用两个集合,一个用于活动,另一个用于非活动。
When you fetch the data from the DB, simply put the CalssItem
in the correct list: 从数据库中获取数据时,只需将
CalssItem
放在正确的列表中:
private List<ClassItem> searchClasses(ClassItems listItems) {
List<ClassItem> activeItems= new ArrayList<ClassItem>();
List<ClassItem> notActiveItems= new ArrayList<ClassItem>();
Iterator<ClassItem> i = listItems.getItems().iterator();
while(i.hasNext()) { //This is a better approach.
ClassEntityManager classEnt = ClassEntityManager.search(i.next().getId);
if(classEnt.active()){
item.setActive(true);
activeItems.add(item);
}else{
item.setActive(false);
notActiveItems.add(item);
}
}
List<ClassItem> ret = new ArrayList<ClassItem>(activeItems);
ret.addAll(notActiveItems);
return ret;
}
BUT, in this way, both activeItems
and notActiveItems
are unreacheable. 但是,通过这种方式,
activeItems
和notActiveItems
都是activeItems
notActiveItems
的。 Best thing to do is to have a loop outside your method that checks if the ClassItem
is active or not. 最好的办法是在方法之外设置一个循环来检查
ClassItem
是否处于活动状态。 In this way both activeItems
and notActiveItems
can be deleted from the method: 这样,可以从方法中删除
activeItems
和notActiveItems
:
private List<ClassItem> searchClasses(ClassItems listItems) {
List<ClassItem> items= new ArrayList<ClassItem>();
Iterator<ClassItem> i = listItems.getItems().iterator();
while(i.hasNext()) { //This is a better approach.
ClassEntityManager classEnt = ClassEntityManager.search(i.next().getId);
item.setActive(classEnt.active());
items.add(item);
}
return items;
}
And to use the list: 并使用该列表:
List<ClassItem> items = searchClasses(classItems);
for(ClassItem item: items){
if(item.isActive()){
//do something
}else{
//do something else
}
}
Better yet is to use the magnificient and beautiful Java 8 Stream API: 更好的是使用华丽而美观的Java 8 Stream API:
List<ClassItem> active = items.stream().filter(x->x.isActive).collect(Collectors.toList());
List<ClassItem> notActive = items.stream().filter(x->!x.isActive).collect(Collectors.toList());
or the one liner: 或者一个班轮:
List<ClassItem> active = searchClasses(classItems).stream().filter(x->x.isActive).collect(Collectors.toList());
NOTES : 注意 :
Your code has a return type of List<Classes>
, while the returned value is of List<ClassItem>
. 您的代码的返回类型为
List<Classes>
,而返回的值为List<ClassItem>
。 Which is right? 哪个是对的?
Your iterator has a generic type of ClassItem
while the next()
method returns a ClassItems
object. 您的迭代器具有泛型类型的
ClassItem
而next()
方法返回ClassItems
对象。 Which is right? 哪个是对的?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.