6

Is there a simple way in Java (that doesn't involve writing a for-loop) to create an array of objects from a property of another array of different objects?

For example, if I have an array of objects of type A, defined as:

public class A {
    private String p;
    public getP() {
        return p;
    }
}

I want to create an array of Strings that contains the value of A[i].p for each i.

Essentially, I'm I want to do this: Creating an array from properties of objects in another array, but in Java.

I attempted to use Arrays.copyOf(U[] original, int newLength, Class<? extends T[]> newType) along with a lambda expression, but that didn't seem to work. What I tried:

Arrays.copyOf(arrayA, arrayA.length, (A a) -> a.getP());
3
  • 2
    You could use Java 8 streams, but it's all going to come down to for loops in the end. Commented May 12, 2016 at 16:03
  • Possible duplicate of Make copy of array Java Commented May 12, 2016 at 16:09
  • @LouisWasserman I understand that, but I prefer one-liners when possible. Commented May 12, 2016 at 16:52

2 Answers 2

11

With Java 8, you can use the Stream API and particularly the map function:

A[] as = { new A("foo"), new A("bar"), new A("blub") };
String[] ps = Stream.of(as).map(A::getP).toArray(String[]::new);

Here, A::getP and String[]::new are method/constructor references. If you do not have a suitable method for the property you want to have, you could also use a lambda function:

String[] ps = Stream.of(as).map(a -> a.getP()).toArray(String[]::new);
Sign up to request clarification or add additional context in comments.

3 Comments

How is a -> a.getP() different from A::getP?
It's not. It's a method reference for a lambda expression.
@rom58 In this case, they are equivalent, but the method reference should be preferred, as it more clearly conveys that it will use just that method and nothing else. Use the lambda if you need a function without an existing method, for example a -> a.getP().trim().toUpperCase() (although this could also be done with method references and three map in a row.
2

This is where a powerful concept in functional programming called map is useful. Here's how map is defined:

map :: (a -> b) -> [a] -> [b]

Thus, map is a function that takes a function (that takes a and returns b) and a list and returns a list. It applies the given function to each element of the given list. Thus map is a higher order function.

In Java 8, you can use this idiom if you can convert the array into a stream. This can be done simply:

Arrays.stream(array).map(mappingFunction);

where the mappingFunction takes an element from stream (say of type A) and converts it to another (say of type B). What you now have is a stream of B's, which you can easily collect in a collector (e.g. in a list, or an array) for further processing.

1 Comment

This gives a stream. To convert to an array, you need to use Stream.toArray() like used in the other comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.