2010-12-21 56 views
15

我想轻松地将A类对象的集合(列表)转换为B类对象的集合,就像Python的map函数一样。这个(某种类型的库)有没有“知名”的实现?我已经在Apache的commons-lang中搜索过它,但没有运气。是否有Python的map函数的Java等价物?

+0

在Python `map()`不会成为实现这一目标的首选方式,而是大多数情况下会使用生成器表达式或列表理解 – 2016-10-28 10:09:23

回答

1

你可以试试番石榴,但你很可能会发现,如果你使用匿名类的将是更加复杂和较长的不仅仅是使用一个循环。

List<OldType> list1 = 
List<NewType> list2 = new ArrayList<NewType>(list1.size()); 
for(OldType element: list1); 
    list2.add(transform(element)); 

值得记住的是,Java不是一种功能语言,它通常更适合使用循环。也许当Java关闭时......感叹。

+0

好点,但它有时更易于定义这些类型o f在一条指令中的操作。即使匿名类的定义更长。此外,我很好奇;) – gregorej 2010-12-21 13:49:55

+0

如果你习惯以这种方式阅读代码,我会说它更清晰。不幸的是Java并没有多大帮助。 :( – 2010-12-21 14:02:55

3

有几种功能性的库提到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的组织。

-1

这是我的解决方案:

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并调用列表中的方法图。

7

的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转换为IntegerList

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] 
0

由于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); 
相关问题