lalet scaria
lalet scaria

Reputation: 87

Can we assign values to a variable in object through a stream in Java8?

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

I want to assign a value to a variable in the first object if the condition in the inner loop matches. Is that possible in java streams ?

Upvotes: 5

Views: 10402

Answers (1)

Nebu Pookins
Nebu Pookins

Reputation: 328

Without knowing much about your types...

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

Upvotes: 3

Related Questions