简体   繁体   English

如何在java 8中使用stream过滤两个列表对象并将值设置为新的List

[英]How to using stream in java 8 filter two list object and set value to new List

I using java 8 and i have two object look like : 我使用java 8,我有两个对象看起来像:

Person class : 人员类:

public class Person {

    private String id;
    private String name;

    public Person() {
    }

    public Person(String id, String name) {
        this.id = id;
        this.name = name;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Person{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                '}';
    }
}

Person1 Class : Person1类:

public class Person1 {
    private String id;
    private String name;

    public Person1() {
    }

    public Person1(String id, String name) {
        this.id = id;
        this.name = name;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Person1{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                '}';
    }
}

I have two list look like : 我有两个列表看起来像:

 List<Person> persons= Arrays.asList(new Person("1","A"),new Person("2","B"));
  List<Person1> persons1 = Arrays.asList(new Person1("3","C"),new Person1("1","F"));

Now i want using stream java 8 loop two list and compare. 现在我想使用流java 8循环两个列表并进行比较。 If any object in list persons equal with persons1 , i will create new list and set it with new value. 如果列表人员中的任何对象与persons1相同,我将创建新列表并使用新值设置它。 Example : If Person1("1","F") equal Person("1","A") because i using id compare it, i get name from Person1 set to Person. 示例: 如果Person1(“1”,“F”)等于Person(“1”,“A”),因为我使用id比较它,我将Person1的名称设置为Person。 Result : Person("1,"F") and add it two new list. 结果:Person(“1,”F“)并添加两个新列表。

My code when i using for: 我使用时的代码:

 for (Person person : persons) {
            for (Person1 person1 : persons1 ) {
                if (person1.getId().equals(person.getId())) {
                    person.setName(person1.getId());
                    break;
                }
            }
        }

Now i want convert it to Stream with new list look like : 现在我想将它转换为Stream,新列表如下所示:

 List<Person> personList =
                list.stream()
                        .filter (
                                person -> list1.stream()
                                        .anyMatch(person1 -> person1.getId()== person.getId()))
                        .collect(Collectors.toList());

But it only filter person. 但它只过滤人。 I want compare two list object. 我想比较两个列表对象。 If same id, i want set name from object in persons1 to persons. 如果相同的id,我想要从person1中的对象设置名称到人。 I know we can using map in stream java 8 but i can't how to do that. 我知道我们可以在流java 8中使用map但我不知道如何做到这一点。 Please help 请帮忙

Some tasks are best accomplished with streams, and others with iteration. 有些任务最好用流完成,其他任务用迭代完成。 Many tasks are best accomplished by combining the two approaches. 通过组合这两种方法可以最好地完成许多任务。 In this solution streams are used to construct the map and then iteration is used to update matching person's name. 在此解决方案中,流用于构建映射,然后迭代用于更新匹配人员的姓名。 Your solution runs in Quadratic time whereas this solution runs in linear time complexity. 您的解决方案以二次方运行,而此解决方案以线性时间复杂度运行。

Map<String, String> idToNameMap = persons1.stream()
    .collect(Collectors.toMap(Person1::getId, Person1::getName, (a, b) -> a));
for (Person person : persons) {
    if (idToNameMap.containsKey(person.getId())) {
        person.setName(idToNameMap.get(person.getId()));
    }
}

It's not the most beautiful answer etc but i guess it's gonna help u get idea how its work. 这不是最美丽的答案等,但我想它会帮助你了解它的工作原理。

List<Person> collect = persons.stream()
        .filter(person -> persons1.stream().anyMatch(person1 -> person1.getId() == person.getId()))
        .map(person -> {
            Person1 getted = persons1.stream()
                .filter(person1 -> person1.getId() == person.getId())
                .findAny()
                .orElseGet(null);
            if (getted == null) throw new IllegalStateException("Should be Impossible");
            person.setName(getted.getName());
            return person;
        })
        .collect(Collectors.toList());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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