Reassign a property of an object in the java object List

Description:
In our development process, we often encounter reassignment of a certain field when we get an object list collection, for example: several subtasks under no task, start to execute the main task and uniformly set the status of the subtasks to be implemented. At this time, you need to use Java8 stream or List forEach to realize it, which is convenient and fast, and you don’t need to traverse and add new lists as complicated, which makes the code bloated.

Way

Instance object list: List subtasks = new ArryList();

	**1.java 8 stream**
subtasks= subtasks.stream().map(item-> {
    
    
             item.setState("1");
             return object ;
             }).collect(Collectors.toList());

2. List forEach

subtasks.forEach(item-> item.setState("1"));

Guess you like

Origin blog.csdn.net/A_awen/article/details/127555661