我想轻松地将A类对象的集合(列表)转换为B类对象的集合,就像Python的map函数一样。这个(某种类型的库)有没有“知名”的实现?我已经在Apache的commons-lang中搜索过它,但没有运气。是否有Python的map函数的Java等价物?
回答
你可以试试番石榴,但你很可能会发现,如果你使用匿名类的将是更加复杂和较长的不仅仅是使用一个循环。
List<OldType> list1 =
List<NewType> list2 = new ArrayList<NewType>(list1.size());
for(OldType element: list1);
list2.add(transform(element));
值得记住的是,Java不是一种功能语言,它通常更适合使用循环。也许当Java关闭时......感叹。
好点,但它有时更易于定义这些类型o f在一条指令中的操作。即使匿名类的定义更长。此外,我很好奇;) – gregorej 2010-12-21 13:49:55
如果你习惯以这种方式阅读代码,我会说它更清晰。不幸的是Java并没有多大帮助。 :( – 2010-12-21 14:02:55
有几种功能性的库提到here,其中大部分的大概覆盖图:
http://www.cs.chalmers.se/~bringert/hoj/ 程序员(包含Java 5.0仿制 支持)。一点文件。
http://jakarta.apache.org/commons/sandbox/functor/ 看起来并不像它被维护, 不支持泛型。小 文件。
http://devnet.developerpipeline.com/documents/s=9851/q=1/ddj0511i/0511i.html 库。
http://functionalj.sourceforge.net
http://www.functologic.com/orbital/
http://jga.sourceforge.net/ 编程在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;
}
}
简单地说,扩展这个类定义你的函数f并调用列表中的方法图。
从的Java 8开始,它可以使用appopriate 映射Function
,我们将使用我们的A
类的实例转换为B
类的实例来完成得益于Stream API
。
的伪代码将是:
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());
下面是一个具体的例子,将使用Integer.valueOf(String)
作为映射器功能的String
一个List
转换为Integer
的List
:
List<String> input = Arrays.asList("1", "2", "3");
List<Integer> output = input.stream().map(Integer::valueOf).collect(Collectors.toList());
System.out.println(output);
输出:
[1, 2, 3]
对于以前版本的Java
,你仍然可以使用FluentIterable
从谷歌番石榴更换Stream
和使用com.google.common.base.Function
代替java.util.function.Function
为映射功能。
前面的例子将被改写为下一个:
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 8中,Java等效Python的地图叫...地图!
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)));
}
}
这个例子整数列表转换为与相应大小的数组列表:
[0]
[0, 0]
[0, 0, 0]
[0, 0, 0, 0]
[0, 0, 0, 0, 0]
你也可以写里面的地图不止一条语句:
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);
- 1. 是否有TCPDF的'annotate'函数的python ReportLab等价物?
- 2. 是否有python的int函数的postgres等价物?
- 3. 是否有Python的RedBeanPHP等价物?
- 4. 是否有Python的Term :: ANSIScreen等价物?
- 5. 是否有“python -i”的ruby等价物?
- 6. R的h2o.stack是否有python等价物?
- 7. 是否有.Net System.Data的Java等价物?
- 8. excel vlookup是否有Java的等价物?
- 9. 是否有IConvertible/System.Convert的Java等价物?
- 10. fortran中的Btest函数,python中是否有任何等价物?
- 11. 是否有Python的all()的Java等价物?
- 12. 是否有Python的defaultdict的Java等价物?
- 13. 在Python中是否有sessionInfo()等价物?
- 14. Filehelpers,Super CSV:是否有Python等价物?
- 15. 在Python中是否有Rake等价物?
- 16. 在Java中是否有TweenMax等价物
- 17. 在java中是否有array_intersect()等价物?
- 18. C++中是否存在Python中的“in”函数的等价物?
- 19. Python是否具有抛出新Exception的Java等价物(“text here”)
- 20. Python的等价物的MATLAB psf2otf函数
- 21. 什么是ES6 Map的Array.map等价物?
- 22. 是否有FreeBSD的make_dev()函数的OS X等价物?
- 23. 在PHP的explode()函数的C++中是否有等价物?
- 24. Python sched模块是否有Java等价物?
- 25. 是否有Android的Application :: onDestroy()等价物?
- 26. 是否有jQuery.offset的Elm等价物?
- 27. 是否有ToolStripButton的WPF等价物?
- 28. JavaScript的Function.prototype.bind是否有Ruby等价物?
- 29. Android中是否有MethodHandle的等价物?
- 30. PHP的ArrayObject是否有in_array等价物?
在Python `map()`不会成为实现这一目标的首选方式,而是大多数情况下会使用生成器表达式或列表理解 – 2016-10-28 10:09:23