3

Let's say I have an ArrayList of objects. For example: ArrayList<Person> personList, where each Person has 2 class variables String name and int age. These variables each have their own getter methods getName() and getAge().

What is the simplest way to retrieve an array (or ArrayList) of int ages[]?

Note this question is similar to the verbosely titled "Retrieve an array of values assigned to a particular class member from an array of objects in java", though without the arbitrary restriction on for-loops, and using an ArrayList instead of an Array.

Community
  • 1
  • 1
Stevoisiak
  • 23,794
  • 27
  • 122
  • 225
  • Why doesn't the linked question solve your problem? – Jorn Vernee Apr 28 '17 at 21:01
  • The linked question had an arbitrary restrictions on using for-loops, and was retrieving from an an `Array[]` of objects, than using an `ArrayList` – Stevoisiak Apr 28 '17 at 21:02
  • Possible duplicate of [Retrieve an array of values assigned to a particular class member from an array of objects in java](http://stackoverflow.com/questions/15473383/retrieve-an-array-of-values-assigned-to-a-particular-class-member-from-an-array) – lucasvw Apr 28 '17 at 21:02
  • 1
    It's functionally identical though – lucasvw Apr 28 '17 at 21:03

3 Answers3

5

Create a target array of the same size as the list then iterate through the list and add each element's age to the target array.

Lew Bloch
  • 3,364
  • 1
  • 16
  • 10
  • 1
    This worked perfectly! I used the following implementation. `int ages[] = new int[personList.size()]; for (int i = 0; i < ages.length; i++) { ages[i] = personList.get(i).getAge(); }` – Stevoisiak Apr 28 '17 at 21:12
  • @StevenVascellaro, I have done a similar implementation below :p – Devendra Lattu Apr 28 '17 at 21:14
4

Numerous ways to do this -- here is one.

First get the ages into a list (using a java8 stream), and then convert the list into an array.

public int[] getAges() {
    return personList.stream()
        .mapToInt(Person::getAge)
        .toArray();
}
Jorn Vernee
  • 31,735
  • 4
  • 76
  • 93
Jameson
  • 6,400
  • 6
  • 32
  • 53
2
Person P1 = new Person("Dev", 25);
Person P2 = new Person("Andy", 12);
Person P3 = new Person("Mark", 20);
Person P4 = new Person("Jon", 33);

ArrayList<Person> personList = new ArrayList<>(Arrays.asList(new Person[] { P1, P2, P3, P4 }));
int[] ages = getPersonAges(personList); // [25, 12, 20, 33]

private static int[] getPersonAges(ArrayList<Person> personList) {
    int[] ages = new int[personList.size()];
    int idx = 0;

    for (Person P : personList) {    // Iterate through the personList
        int age = P.getAge();
        ages[idx++] = age;
    }

    return ages;
}
Devendra Lattu
  • 2,732
  • 2
  • 18
  • 27
  • Is there a reason not to one-line the for loop to `ages[idx++] = P.getAge()`? – Stevoisiak Apr 28 '17 at 21:18
  • 1
    @StevenVascellaro No. Those two code segments would be identical. It is acceptable to do either. – Obicere Apr 28 '17 at 21:19
  • I have written on two lines just to explain that the return type from `P.getAge()` is of type `int` or `autoboxed` to `int`. – Devendra Lattu Apr 28 '17 at 21:19
  • @Steven, remember you can always mark the answer accepted that you found simple and helpful. Thanks :D – Devendra Lattu Apr 28 '17 at 21:21
  • @DevendraLattu Thanks. Typically, I try to avoid rushing to accept an answer so I can avoid the [Fastest Gun in the West problem](https://meta.stackexchange.com/questions/9731/fastest-gun-in-the-west-problem). – Stevoisiak Apr 28 '17 at 21:24
  • The variable name `P` violates the Java naming conventions and naming best practices. – Lew Bloch Apr 28 '17 at 23:34