回答
有一個內置接口像這樣,儘管它只與Java 8兼容急性呼吸窘迫綜合徵。你可以找到它here。
從Javadoc中:
public interface Function<T,R>
表示一個接受一個參數併產生一個結果的功能。
這是一個功能接口,其功能方法是
apply(Object)
。類型參數:
T
- 輸入到功能
R
類型 - 函數[Java的
退房番石榴,它有一個Function interface:
public interface Function<F, T> {
/**
* Applies the function to an object of type {@code F}, resulting in an object of type {@code T}.
* Note that types {@code F} and {@code T} may or may not be the same.
*
* @param from the source object
* @return the resulting object
*/
T apply(@Nullable F from);
/**
* Indicates whether some other object is equal to this {@code Function}. This method can return
* {@code true} <i>only</i> if the specified object is also a {@code Function} and, for every
* input object {@code o}, it returns exactly the same value. Thus, {@code
* function1.equals(function2)} implies that either {@code function1.apply(o)} and {@code
* function2.apply(o)} are both null, or {@code function1.apply(o).equals(function2.apply(o))}.
*
* <p>Note that it is always safe <i>not</i> to override {@link Object#equals}.
*/
boolean equals(@Nullable Object obj);
}
事實上,考慮到你的目標是你,而不是Sun/Oracle的一個,你應該定義自己的接口(因爲它規定了合同你希望你的接口的實現者fullfil)。但是,如果某個框架已經存在這樣的接口,並且其目的與您的目的相同,您可以使用它的定義,但是要謹慎。
不幸的是,核心Java庫中沒有這樣的東西。因此,許多庫定義了自己的類似功能的接口。如果您恰好已經使用過這樣的庫,則可以重新使用它使用的功能。
我知道答案是舊的。但是從Java 8+開始,這是可能的。 https://www.leveluplunch.com/java/examples/java-util-function-function-example/ – Eternalcode 2017-06-13 20:46:55
可以使用這樣的庫如Apache Commons Functor具有實用的功能,如:
UnaryFunction
T evaluate(A obj);
BinaryFunction
T evaluate(L left, R right);
- 1. F#功能界面
- 2. 交換功能和一般功能
- 3. 一般保護的功能是什麼?
- 4. 界面與功能記錄
- 5. UVC功能配置界面
- 6. 的Java 8:功能界面,檢查權限和返回響應
- 7. jQuery的用戶界面功能問題
- 8. Scala的功能界面性狀
- 9. 居家般的用戶界面?
- 10. Prezi般的網絡界面。 Flash或HTML5?
- 11. ExtJS 4.1或一般Javascript HTTP_Authorization功能
- 12. 獲取一般錯誤處理功能
- 13. 一般化的功能爲一個ODE求解
- 14. 辦公功能界面和谷歌Chrome界面
- 15. 用戶界面功能建模語言?
- 16. jQuery Slider用戶界面 - 增強功能
- 17. Kotlin:使用lambda代替功能界面?
- 18. 可訪問性不一致:參數類型一般c#界面
- 19. Java中的一般例外
- 20. NUI/touch界面的一般注意事項
- 21. Java - 出界異常
- 22. Java何時捕獲一般異常和特定異常
- 23. Java + Web界面
- 24. Java中PB的一般解析
- 25. Android搜索界面和材質設計
- 26. 一般化了幾個行動統一到一個jQuery功能
- 27. 創建一個谷歌實驗室般的擴展功能
- 28. 一個或兩個用戶界面分開業務功能?
- 29. 用戶界面和一些功能刪除後不工作xcworkspace
- 30. 圖形用戶界面在哪裏去了一般應用
可能重複的結果類型:有沒有地圖功能? ](http://stackoverflow.com/questions/3907394/java-is-there-a-map-function) – Riduidel 2010-10-11 15:08:10
@Riduidel:它是如何是同一個問題?這是一個完全不同的問題。 – Albert 2010-10-11 15:15:27
這可能是Java被基因聚合時最明顯的疏忽。 – 2013-03-18 13:41:12