Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 221
lalet scaria Avatar asked Oct 19 '22 08:10

lalet scaria


1 Answers

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"));
like image 167
Nebu Pookins Avatar answered Oct 28 '22 23:10

Nebu Pookins