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

我使用 java 8,我有两个对象:


人类:


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 + '\'' +

                '}';

    }

}

人 1 类:


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 + '\'' +

                '}';

    }

}

我有两个列表:


 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"));

现在我想使用流 java 8 循环两个列表并进行比较。如果列表 persons 中的任何对象等于 persons1 ,我将创建新列表并为其设置新值。示例:如果 Person1("1","F") 等于 Person("1","A")因为我使用id比较它,我从 Person1 获得的名字设置为 Person。结果:Person("1,"F") 并将其添加到两个新列表中。

我使用时的代码:


 for (Person person : persons) {

            for (Person1 person1 : persons1 ) {

                if (person1.getId().equals(person.getId())) {

                    person.setName(person1.getId());

                    break;

                }

            }

        }




海绵宝宝撒
浏览 154回答 2
2回答

holdtom

有些任务最好用流来完成,而另一些则用迭代来完成。许多任务最好通过结合这两种方法来完成。在此解决方案中,使用流来构建地图,然后使用迭代来更新匹配人员的姓名。您的解决方案以二次时间运行,而此解决方案以线性时间复杂度运行。Map<String, String> idToNameMap = persons1.stream()&nbsp; &nbsp; .collect(Collectors.toMap(Person1::getId, Person1::getName, (a, b) -> a));for (Person person : persons) {&nbsp; &nbsp; if (idToNameMap.containsKey(person.getId())) {&nbsp; &nbsp; &nbsp; &nbsp; person.setName(idToNameMap.get(person.getId()));&nbsp; &nbsp; }}

蝴蝶不菲

这不是最漂亮的答案等等,但我想它会帮助你了解它是如何工作的。List<Person> collect = persons.stream()&nbsp; &nbsp; &nbsp; &nbsp; .filter(person -> persons1.stream().anyMatch(person1 -> person1.getId() == person.getId()))&nbsp; &nbsp; &nbsp; &nbsp; .map(person -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Person1 getted = persons1.stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .filter(person1 -> person1.getId() == person.getId())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .findAny()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .orElseGet(null);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (getted == null) throw new IllegalStateException("Should be Impossible");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; person.setName(getted.getName());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return person;&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.toList());
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java