简体   繁体   English

Java 8如何从数组中删除具有ID的数组列表中的项目

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM