2010-12-21 101 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); 
相關問題