我想輕鬆地將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