Java – ArrayList changes a property value of all objects

Java Spring Collection

List<CustomObject> myList = new ArrayList<CustomObject>(1000); // list with thousnda objects

I want to set the customobject attribute 'type' of all objects in the list The simple way is to run the loop and set the following properties

for(CustomObject obj:myList){
obj.setType('value'); 
}

I want the code to look better, so what if there's any other way to do the same thing in one line of code?

Solution

In Java 8:

myList.forEach(c -> c.setType("value"));
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>