How to make a new list with a property of an object which is in another list

前端 未结 6 1160
借酒劲吻你
借酒劲吻你 2020-12-22 16:49

Imagine that I have a list of certain objects:

List

And I need to generate another list including the ids of

6条回答
  •  别那么骄傲
    2020-12-22 17:23

    It is Mathematically impossible to do this without a loop. In order to create a mapping, F, of a discrete set of values to another discrete set of values, F must operate on each element in the originating set. (A loop is required to do this, basically.)

    That being said:

    Why do you need a new list? You could be approaching whatever problem you are solving in the wrong way.

    If you have a list of Student, then you are only a step or two away, when iterating through this list, from iterating over the I.D. numbers of the students.

    for(Student s : list)
    {
        int current_id = s.getID();
        // Do something with current_id
    }
    

    If you have a different sort of problem, then comment/update the question and we'll try to help you.

提交回复
热议问题