Java:从另一个数组的属性创建一个数组

时间:2022-06-24 07:10:37

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?

Java中是否有一种简单的方法(不涉及编写for循环)来从另一个不同对象数组的属性创建对象数组?

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

例如,如果我有一个A类型的对象数组,定义为:

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.

我想创建一个字符串数组,其中包含[I]的值。我为每一个。

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

本质上,我想这样做:从另一个数组中对象的属性创建一个数组,但是在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:

我尝试使用数组。copyOf(U[] original, int newLength, Class newType)和lambda表达式,但这似乎行不通。我试着什么:

Arrays.copyOf(arrayA, arrayA.length, (A a) -> a.getP());

2 个解决方案

#1


3  

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

使用Java 8,您可以使用流API,特别是map函数:

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:

这里,A::getP和String[]::new是方法/构造函数引用。如果您没有适合您想要的属性的方法,您也可以使用lambda函数:

String[] ps = Stream.of(as).map(a -> a.getP()).toArray(String[]::new);

#2


1  

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

这就是函数式编程中称为map的强大概念的用处所在。地图的定义如下:

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.

因此,map是一个函数,它接受一个函数(获取并返回b)和一个列表并返回一个列表。它将给定的函数应用到给定列表的每个元素。因此映射是一个高阶函数。

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

在Java 8中,如果可以将数组转换为流,可以使用这个习语。这可以简单地做到:

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.

mappingFunction从流(比如A类型)获取一个元素并将其转换为另一个元素(比如B类型)。

#1


3  

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

使用Java 8,您可以使用流API,特别是map函数:

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:

这里,A::getP和String[]::new是方法/构造函数引用。如果您没有适合您想要的属性的方法,您也可以使用lambda函数:

String[] ps = Stream.of(as).map(a -> a.getP()).toArray(String[]::new);

#2


1  

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

这就是函数式编程中称为map的强大概念的用处所在。地图的定义如下:

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.

因此,map是一个函数,它接受一个函数(获取并返回b)和一个列表并返回一个列表。它将给定的函数应用到给定列表的每个元素。因此映射是一个高阶函数。

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

在Java 8中,如果可以将数组转换为流,可以使用这个习语。这可以简单地做到:

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.

mappingFunction从流(比如A类型)获取一个元素并将其转换为另一个元素(比如B类型)。