【问题标题】:Can we assign values to a variable in object through a stream in Java8?我们可以通过Java8中的流为对象中的变量赋值吗?
【发布时间】:2015-09-14 04:21:37
【问题描述】:
List<Person> personsInOMwithTypeDsc = personsInOm.stream()
                    .filter(e -> e.getPersonType().getPersonTypeId() ==1 )
                    .forEach(personTypeList.stream()
                            .foreach(d -> d.getPersonTypeId() == 1 )
                            .map(Person::setPersonType(d))
                            .collect(Collectors.toList());

如果内部循环中的条件匹配,我想为第一个对象中的变量赋值。这在java流中可能吗?

【问题讨论】:

  • 你想做什么?以 forEach 结尾的流管道无法返回 List
  • “在第一个对象中”是什么意思?
  • 该功能就像在内部迭代两个for循环和if-else条件。如果条件匹配,我想将该值分配给外部对象循环中的变量。

标签: java collections lambda java-stream


【解决方案1】:

不太了解你的类型...

List<Person> persons = /*you're getting this from somewhere*/;
persons.stream()
  .filter(person -> person.isSatisfiedByYourConditon())
  .forEach(person -> person.setSomeField("new value"));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-03
    • 2019-07-23
    • 2018-03-08
    相关资源
    最近更新 更多