Python是否有Java等价物';s映射函数?
我想简单地将类a的对象集合(列表)转换为类B的对象集合,就像Python的map函数那样。是否有这种(某种类型的库)的“著名”实现?我已经在Apache的commons lang中搜索过了,但没有找到 可以看看谷歌的番石榴图书馆:Python是否有Java等价物';s映射函数?,java,python,collections,Java,Python,Collections,我想简单地将类a的对象集合(列表)转换为类B的对象集合,就像Python的map函数那样。是否有这种(某种类型的库)的“著名”实现?我已经在Apache的commons lang中搜索过了,但没有找到 可以看看谷歌的番石榴图书馆: 您可以尝试番石榴,但您可能会发现,如果您使用匿名类,它将比仅使用循环更复杂、更长 List<OldType> list1 = List<NewType> list2 = new ArrayList<NewType>(list1.si
您可以尝试番石榴,但您可能会发现,如果您使用匿名类,它将比仅使用循环更复杂、更长
List<OldType> list1 =
List<NewType> list2 = new ArrayList<NewType>(list1.size());
for(OldType element: list1);
list2.add(transform(element));
列表列表1=
List list2=新的ArrayList(list1.size());
for(OldType元素:list1);
列表2.添加(转换(元素));
值得记住的是,Java不是函数式语言,最好只使用循环。也许当Java有闭包时。。。。叹气。提到了几个功能库,其中大部分可能包括地图: 程序员(包括Java 5.0泛型) 支持)。几乎没有文档 看起来它没有被维护, 不支持泛型。小 文件 图书馆 java编程(包括 仿制药)。期待更多 文档,也许更好 API的组织 看一看,它仍然不存在 功能编程功能将添加到 我认为现在最适合您的需要这是我的解决方案:
public abstract class MapF<T,S>
{
public abstract T f(S s) throws Exception;
public List<T> map(List<S> input) throws Exception
{
LinkedList<T> output = new LinkedList<T>();
for (S s: input)
output.add(f(s));
return output;
}
}
公共抽象类MapF
{
公共摘要tf(S)抛出异常;
公共列表映射(列表输入)引发异常
{
LinkedList输出=新建LinkedList();
用于(S:输入)
增加(f(s));
返回输出;
}
}
简单地说,扩展这个定义函数f的类并调用列表上的方法映射。从Java8开始,这可以通过使用适当的映射器来完成,我们将使用它将类
A
的实例转换为类B
的实例
伪代码应该是:
List<A> input = // a given list of instances of class A
Function<A, B> function = // a given function that converts an instance
// of A to an instance of B
// Call the mapper function for each element of the list input
// and collect the final result as a list
List<B> output = input.stream().map(function).collect(Collectors.toList());
[1, 2, 3]
List<Integer> output = FluentIterable.from(input)
.transform(
new Function<String, Integer>() {
public Integer apply(final String value) {
return Integer.valueOf(value);
}
}
).toList();
[1, 2, 3]
输出:
List<A> input = // a given list of instances of class A
Function<A, B> function = // a given function that converts an instance
// of A to an instance of B
// Call the mapper function for each element of the list input
// and collect the final result as a list
List<B> output = input.stream().map(function).collect(Collectors.toList());
[1, 2, 3]
List<Integer> output = FluentIterable.from(input)
.transform(
new Function<String, Integer>() {
public Integer apply(final String value) {
return Integer.valueOf(value);
}
}
).toList();
[1, 2, 3]
对于
Java
的早期版本,您仍然可以使用fromGoogle Guava替换流
,并使用而不是Java.util.function.function
作为映射器函数
上一个示例将被改写为下一个:
List<A> input = // a given list of instances of class A
Function<A, B> function = // a given function that converts an instance
// of A to an instance of B
// Call the mapper function for each element of the list input
// and collect the final result as a list
List<B> output = input.stream().map(function).collect(Collectors.toList());
[1, 2, 3]
List<Integer> output = FluentIterable.from(input)
.transform(
new Function<String, Integer>() {
public Integer apply(final String value) {
return Integer.valueOf(value);
}
}
).toList();
[1, 2, 3]
从Java8开始,Python映射的Java等价物被称为。。。地图
package stackOverflow;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class ListMapExample
{
public static void main(String[] args) {
List<Integer> integers = Arrays.asList(1, 2, 3, 4, 5);
List<int[]> arrays = integers.stream().map(size -> new int[size])
.collect(Collectors.toList());
arrays.forEach(ary -> System.out.println(Arrays.toString(ary)));
}
}
您还可以在映射内编写多条语句:
List<String> strings = Arrays.asList("1", "2", "3", "4", "5");
Stream<int[]> arraysStream = strings.stream().map(string -> {
int size = Integer.valueOf(string);
int size2 = size * 2;
return new int[size2];
});
arraysStream.map(Arrays::toString).forEach(System.out::println);
List strings=Arrays.asList(“1”、“2”、“3”、“4”、“5”);
Stream arraysStream=strings.Stream().map(字符串->{
int size=Integer.valueOf(字符串);
int size2=大小*2;
返回新的int[size2];
});
arraysStream.map(Arrays::toString.forEach(System.out::println);
这一点很好,但有时在一条指令中定义这类操作更容易理解。即使匿名类的定义更长。此外,我很好奇;)我想说,如果你习惯了用这种方式阅读代码,那就更清楚了。不幸的是,Java帮助不大(是的,Collections2.transform似乎满足了我的需要。在Python中,map()
并不是实现这一点的首选方法,相反,大多数人会使用生成器表达式或列表理解