JAva 8が提供するFunction、あなたはどのくらい知っていて、一緒に見てみましょう


JAva 8にはFunctionキーワードが追加され、関数式プログラミングモードをサポートし、エンジニアリングプロジェクトに応用することを学び、使用過程の経験を記録しようとしています.
関数式プログラミングの経験がある人は、関数式プログラミングの簡潔さ、直接、関数は普通の変数と同じようにfirst class一等公民として扱われ、応用が柔軟で、運用がよく、大量の重複性のコードを減らすことができ、符号化効率を高めることができ、関数はコードブロックテンプレートと見なすことができる.
JAva 8以前はfunction機能はサポートされておらず、それに対応して多くの匿名クラスが関連インターネットを実現し、コードが冗長であり、1つのクラスの性能をインスタンス化することによっても影響を及ぼしていた.
JAva 8が提供するfunctionメカニズムは、実現原理が何なのか、性能面に問題があるのか、閉包問題がどのように解決されるのか、本文は徐々に整理し、これらの疑問を解く.
1、基本使用
Function
  • 公式紹介文書はここ
  • 基本命名規則The functional interfaces in this package follow an extensible nameng convention,as follows:There are several basic function shapes,including Function(unary function from T to R),Consumer(unary function from T to void),Predicate(unary function from T to boolean),and Supplier(nilary function to R). Function shapes have a natural arity based on how they are most commonly used. The basic shapes can be modified by an arity prefix to indicate a different arity, such as BiFunction (binary function from T and U to R). There are additional derived function shapes which extend the basic function shapes, including UnaryOperator (extends Function) and BinaryOperator (extends BiFunction). Type parameters of functional interfaces can be specialized to primitives with additional type prefixes. To specialize the return type for a type that has both generic return type and generic arguments, we prefix ToXxx, as in ToIntFunction. Otherwise, type arguments are specialized left-to-right, as in DoubleConsumer or ObjIntConsumer. (The type prefix Obj is used to indicate that we don’t want to specialize this parameter, but want to move on to the next parameter, as in ObjIntConsumer.) These schemes can be combined, as in IntToDoubleFunction. If there are specialization prefixes for all arguments, the arity prefix may be left out (as in ObjIntConsumer).
  • 命名規則を見ると、以下の4種類に基づいて発生し、以下のいくつかのfunctionの使用を把握すればよい.
  • basic function shapes, including
  • Function(unary function from T to R):一元関数r=f(t)
  • Consumer(unary function from T to void):一元関数f(t)、戻り値
  • なし
  • Predicate(unary function from T to boolean):一元関数boolean r=f(t)
  • Supplier (nilary function to R)

  • BiFunction(binary function from T and U to R).二元関数r=f(t,u)...

  • 2、メリットとデメリット
    メリット
    テンプレートコードは,符号化効率を向上させ,簡潔,高次Function,柔軟性と可読性の間で1度を把握し,バランスをとる.
    欠点
        
    

    3、注意事項
    クローズド・パッケージの問題、変数の役割ドメインの問題
    4、結合Stream
    5、関数式プログラミングについて