Modify property value of the objects in list using Java 8 streams

Java 8Java Stream

Java 8 Problem Overview


I have a list of Fruit objects in ArrayList and I want to modify fruitName to its plural name.

Refer the example:

@Data
@AllArgsConstructor
@ToString
class Fruit {

    long id;
    String name;
    String country;
}

List<Fruit> fruits = Lists.newArrayList();
fruits.add(new Fruit(1L, "Apple", "India"));
fruits.add(new Fruit(2L, "Pineapple", "India"));
fruits.add(new Fruit(3L, "Kiwi", "New Zealand"));

Comparator<Option> byNameComparator = (e1, e2) -> e1.getName().compareToIgnoreCase(e2.getName());

fruits = fruits.stream().filter(fruit -> "India".equals(fruit.getCountry()))
            .sorted(byNameComparator).collect(Collectors.toList());

List<Fruit> fruitsWithPluralNames = Lists.newArrayList();
for (Fruit fruit : fruits) {
	fruit.setName(fruit.getName() + "s");
	fruitsWithPluralNames.add(fruit);
}

System.out.println(fruitsWithPluralNames);

// which prints [Fruit(id=1, name=Apples, country=India), Fruit(id=2, name=Pineapples, country=India), Fruit(id=3, name=Kiwis, country=New Zealand)]


Do we have any way to achieve same behavior using Java 8 streams ?

Java 8 Solutions


Solution 1 - Java 8

If you wanna create new list, use Stream.map method:

List<Fruit> newList = fruits.stream()
    .map(f -> new Fruit(f.getId(), f.getName() + "s", f.getCountry()))
    .collect(Collectors.toList())

If you wanna modify current list, use Collection.forEach:

fruits.forEach(f -> f.setName(f.getName() + "s"))

Solution 2 - Java 8

You can use just forEach. No stream at all:

fruits.forEach(fruit -> fruit.setName(fruit.getName() + "s"));

Solution 3 - Java 8

You can use peek to do that.

List<Fruit> newList = fruits.stream()
    .peek(f -> f.setName(f.getName() + "s"))
    .collect(Collectors.toList());

Solution 4 - Java 8

just for modifying certain property from object collection you could directly use forEach with a collection as follows

collection.forEach(c -> c.setXyz(c.getXyz + "a"))

Solution 5 - Java 8

We can change the property via map without creating new objects. Below method increase the age by 2. It will modify your original list

List<Employee> l2=list.stream().map(t->{
				t.setAge(t.getAge()*2);
				return t;
			}
		).collect(Collectors.toList());

Solution 6 - Java 8

You can do it using streams map function like below, get result in new stream for further processing.

Stream<Fruit> newFruits = fruits.stream().map(fruit -> {fruit.name+="s"; return fruit;});
        newFruits.forEach(fruit->{
            System.out.println(fruit.name);
        });

Solution 7 - Java 8

You can use map from streams.

fruits.map(i-> {
   i.setFruit();
   return i;
 }).collect(Collectors.toList();

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBhavesh DangiView Question on Stackoverflow
Solution 1 - Java 8Sergii LagutinView Answer on Stackoverflow
Solution 2 - Java 8ApostolosView Answer on Stackoverflow
Solution 3 - Java 8SriView Answer on Stackoverflow
Solution 4 - Java 8Pravin BansalView Answer on Stackoverflow
Solution 5 - Java 8Vibhor BhardwajView Answer on Stackoverflow
Solution 6 - Java 8MurliView Answer on Stackoverflow
Solution 7 - Java 8GvSharmaView Answer on Stackoverflow