Java - Easiest way to get single property from each object in a list/array?

前端 未结 3 1464
猫巷女王i
猫巷女王i 2021-01-05 06:07

Say I have a person object with properties such as name, hair color, and eye color. I have the following array Person[] people

3条回答
  •  时光说笑
    2021-01-05 06:10

    java 8:

    String[] names = Arrays.asStream(people).map(Person::getName).asArray(String[]::new);
    

提交回复
热议问题