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

collections, java

Solution

An object is a reference towards a memory address. Then, the fields of this objects are other references towards other memory addresses. Hence, a list of objects is a list of references. So, it's impossible for the list to direclty access the object fields (references given by the references). The short answer is no.

Note: anyway you'll find an API that does what you want, it still loops in the inside.

Problem

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 without having to iterate over the list, if it is at all possible?

Original source