使用Java流根据第二个列表中的值筛选列表

使用Java流根据第二个列表中的值筛选列表,java,sorting,java-stream,filtering,Java,Sorting,Java Stream,Filtering,我正在尝试根据第二个列表中的值筛选对象列表 清单A: 清单B: 现在我想删除列表A中的项目,列表B中有可用的ID 我尝试使用JavaStream,但不理解语法,我尝试ForEach循环,但它不能正常工作 我在Swift中做了如下类似的事情 if let allModOptions = allModifersList?.first?.options { let excludedIDs = pObj?.excluded_ids

我正在尝试根据第二个列表中的值筛选对象列表

清单A: 清单B: 现在我想删除列表A中的项目,列表B中有可用的ID

我尝试使用JavaStream,但不理解语法,我尝试ForEach循环,但它不能正常工作

我在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!)
            }
如果让allModOptions=allModifersList?.first?.options{
设excludedIDs=pObj?.excluded\u id
如果排除了!计数>0{
让allowedOptions=allModOptions

->>>>**.filter{!excludedIDs!.contains($0.id!)}**您应该在主集合上使用filter,在列表集合上使用!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);
    }


}
import java.util.ArrayList;
导入java.util.array;
导入java.util.List;
导入java.util.stream.collector;
课堂擦伤{
静态类元素{
int-id;
字符串名;
字符串描述;
公共元素(int-id、字符串名称、字符串描述){
this.id=id;
this.name=名称;
this.description=描述;
}
@凌驾
公共字符串toString(){
返回“元素{”+
“id=”+id+
“,name=”“+name+”\“””+
,description=''+description+'\''+
'}';
}
}
公共静态void main(字符串[]args){
列表元素=新的ArrayList(Arrays.asList(
新元素(12345,“项目名称”、“asdfafd”),
新元素(34567,“项目名称”、“asdfafd”),
新元素(56789,“项目名称”、“asdfafd”)
));
List filterNot=newarraylist(Arrays.asList(1234556789));
列表结果=elements.stream().filter(item->!filterNot.contains(item.id)).collect(Collectors.toList());
result.forEach(System.out::println);
}
}
  • 如果我理解正确,您将获得以下信息:
    • 一个包含3个字段的类:
      id
      名称
      描述
      。我们将该类称为
    • 应从
      列表a
      中删除的
      项的ID列表。我们将此列表称为
      idsOfItemsToRemove
    • 要评估的所有
      项目的列表
    • 预期列表;
      项的列表,这些项不包含
      idsOfItemsToRemove
      中的任何值
  • 如果上述假设是正确的,那么下面的代码片段应该表明您正在寻求做什么
@测试
公开无效测试(){
//给定
整数idOfItemToBeRemoved1=12345;
整数idOfItemToBeRemoved2=56789;
Item itemExpectedToBeDeleted1=新项目(idOfItemToBeRemoved1,“项目名称”、“描述项”);
Item itemExpectedToBeDeleted2=新项目(idOfItemToBeRemoved2,“项目名称”,“描述项”);
Item ItemExpectedTobertained1=新项目(34567,“项目名称”,“描述项”);
Item ItemExpectedTobertained2=新项目(98756,“项目名称”,“描述项”);
列表idsOfItemsToRemove=Arrays.asList(
idOfItemToBeRemoved1,
idOfItemToBeRemoved2);
List listOfItems=Arrays.asList(
预期删除的项目1,
项目ExpectedTobeDeleted2,
预计将保留的项目1,
项目预期目标2);
List expectedList=Arrays.asList(
预计将保留的项目1,
项目预期目标2);
//什么时候
List actualList=ListoItems
.stream()
.filter(项->!idsOfItemsToRemove.contains(项.getId()))
.collect(Collectors.toList());
//然后
Assert.assertEquals(expectedList,actualList);
}
此代码也已推送到Github

`https://raw.githubusercontent.com/Git-Leon/stackoverflow-answers/master/javastreamfilter/


请发布您的代码,并解释您在itm尝试使用JavaStream时遇到的问题…-这是一个非常基本的问题。发布您的代码,以便我们能够准确了解您的处境。我从Swift添加了一行解决方案,但无法理解Java流语法。非常感谢。它很有效。我缺少了这一点->!filterNot.contains(item.id)
            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!)
            }
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);
    }


}
@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);
}