[英]Java 8 how to remove item from arraylist with ids from array
How to remove item from arraylist with ids from array. 如何从数组中删除具有ID的arraylist中的项目。 I have tried:
我努力了:
List <Group> loadedGroupList = iGroupRepository.findAll();
String [] groupIds = StringUtils.split(selectionGroupsIds, ',');
for (int i = 0 ; i < groupIds.length; i++) {
String groupId = groupIds[i];
loadedGroupList.removeIf(x -> x.getId() != Long.parseLong(groupId));
}
But I get an empty arraylist. 但是我得到一个空的arraylist。 Please help.
请帮忙。 Thanks.
谢谢。
groupIds
is an String
array, convert it into List<String>
and use contains()
groupIds
是一个String
数组,将其转换为List<String>
并使用contains()
removes Group
from loadedGroupList if groupId is in groupIds array 如果groupId在groupIds数组中,则从loadedGroupList中移除
Group
loadedGroupList.removeIf(x -> Arrays.asList(groupIds).contains(x.getId()));
removes Group
from loadedGroupList if groupId is not in groupIds array 如果groupId不在groupIds数组中,则从loadedGroupList中移除
Group
loadedGroupList.removeIf(x -> !Arrays.asList(groupIds).contains(x.getId()));
By using java-8
streams, filter Group
that doesn't have id
in String
array groupIds
通过使用
java-8
流,过滤在String
数组groupIds
中没有id
Group
List<Group> result = loadedGroupList.stream()
.filter(x->!Arrays.asList(groupIds).contains(x.getId()))
.collect(Collectors.toList());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.