[英]Use Java Stream to filter list based on values from second list
I'm trying to Filter a list of objects based on values from second list.我正在尝试根据第二个列表中的值过滤对象列表。
[
{
"id":"12345",
"name":"nameOfItem",
"description":"descriptionOfItem"
},
{
"id":"34567",
"name":"nameOfItem",
"description":"descriptionOfItem"
},
{
"id":"56789",
"name":"nameOfItem",
"description":"descriptionOfItem"
}
]
["12345", "56789"]
Now i want to remove the item of List A with IDs available in List B.现在我想删除列表 A 中具有列表 B 中可用 ID 的项目。
Im trying to use JavaStream but can't understand the syntax and i'm trying ForEach loop but its not working properly.我正在尝试使用 JavaStream 但无法理解语法,我正在尝试 ForEach 循环但它无法正常工作。
I've done something similar in Swift as following.我在 Swift 中做了类似的事情,如下所示。
if let allModOptions = allModifersList?.first?.options {
let excludedIDs = pObj?.excluded_ids
if excludedIDs!.count > 0 {
let allowedOptions = allModOptions
->>>> **.filter{ !excludedIDs!.contains($0.id!)}** <<<<-
.filter{c in c.deleted_at == nil}.sorted {
$0.index ?? 0 < $1.index ?? 0
}
allModsList?.first?.options = allowedOptions
}
modisList.append(contentsOf: allModsList!)
}
Any help is appreciated任何帮助表示赞赏
you should use filter on main collection and !contains on List collection你应该在主集合上使用过滤器,在列表集合上使用 !contains
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
class Scratch {
static class Element {
int id;
String name;
String description;
public Element(int id, String name, String description) {
this.id = id;
this.name = name;
this.description = description;
}
@Override
public String toString() {
return "Element{" +
"id=" + id +
", name='" + name + '\'' +
", description='" + description + '\'' +
'}';
}
}
public static void main(String[] args) {
List<Element> elements = new ArrayList<>(Arrays.asList(
new Element(12345, "nameofitem", "asdfafd"),
new Element(34567, "nameofitem", "asdfafd"),
new Element(56789, "nameofitem", "asdfafd")
));
List<Integer> filterNot = new ArrayList<>(Arrays.asList(12345, 56789));
List<Element> result = elements.stream().filter(item -> !filterNot.contains(item.id)).collect(Collectors.toList());
result.forEach(System.out::println);
}
}
id
, name
, description
.一个 class 具有 3 个字段: id
、 name
、 description
。 Lets call this class Item
.让我们称之为 class Item
。Item
s which should be removed from List A
.应从List A
中删除的Item
的 id 列表。 Lets call this list, idsOfItemsToRemove
.让我们将此列表idsOfItemsToRemove
。Item
s to evaluate要评估的所有Item
的列表Item
s which do not contain any value present in idsOfItemsToRemove
.不包含idsOfItemsToRemove
中存在的任何值的Item
列表。@Test
public void test() {
// given
Integer idOfItemToBeRemoved1 = 12345;
Integer idOfItemToBeRemoved2 = 56789;
Item itemExpectedToBeDeleted1 = new Item(idOfItemToBeRemoved1, "nameOfItem", "descriptionOfItem");
Item itemExpectedToBeDeleted2 = new Item(idOfItemToBeRemoved2, "nameOfItem", "descriptionOfItem");
Item itemExpectedToBeRetained1 = new Item(34567, "nameOfItem", "descriptionOfItem");
Item itemExpectedToBeRetained2 = new Item(98756, "nameOfItem", "descriptionOfItem");
List<Integer> idsOfItemsToRemove = Arrays.asList(
idOfItemToBeRemoved1,
idOfItemToBeRemoved2);
List<Item> listOfItems = Arrays.asList(
itemExpectedToBeDeleted1,
itemExpectedToBeDeleted2,
itemExpectedToBeRetained1,
itemExpectedToBeRetained2);
List<Item> expectedList = Arrays.asList(
itemExpectedToBeRetained1,
itemExpectedToBeRetained2);
// when
List<Item> actualList = listOfItems
.stream()
.filter(item -> !idsOfItemsToRemove.contains(item.getId()))
.collect(Collectors.toList());
// then
Assert.assertEquals(expectedList, actualList);
}
This code has also been push
ed to Github.此代码也已push
送到 Github。
`https://raw.githubusercontent.com/Git-Leon/stackoverflow-answers/master/javastreamfilter/ `https://raw.githubusercontent.com/Git-Leon/stackoverflow-answers/master/javastreamfilter/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.