How to get a list of specific fields values from objects stored in a list?

后端 未结 12 1062
长发绾君心
长发绾君心 2021-01-31 08:23

Say I have an list of objects with two fields field1 and field2, both of String type.

How do I get a list of all field1 values wit

12条回答
  •  梦如初夏
    2021-01-31 08:53

    Let the object be of the following class.

    public class Bike {
    
        String bikeModel;
        Int price;
    }
    

    And now there is list of bikes called bikeList of type List

    So now we want a list of bikemodels of all the bikes in the above list.

    bikeList.map{ Bike b -> b.bikeModel }.toCollection(arrayListOf())
    

    returns an array list of first field of all the bike objects in the bikeList

提交回复
热议问题