Java 8 nov kousky java util function obsahuje
Java 8 / nové kousky java. util. function: obsahuje pouze funkcionální interfejsy Bi+, Int+, Double+, Long+ Consumer, Function, Predicate, Supplier, Unary. Operator, Binary. Operator java. util. stream: Collector, Base. Stream<…>, Stream<T>, ( Int | Long | Double ) Stream, Collectors, Stream. Support , Primitive. Iterator, Split. Iterator Base 64, Split. Iterators, String. Joiner Optional ( Int | Long | Double | <T> ) ( Int | Long | Double )Summary. Statistics, PJV 25 1
java. util JDK 8 java. util Split. Iterator <E> Primitive. Iterator <T> Optional Split. Iterators <T> Optional. Int Double. Consumer Double. Summary Statistics Int. Consumer Long. Summary Long. Consumer Statistics Int. Summary Statistics PJV 25 Base 64 Int alter Long Double 2
Funkcionální interfejsy Predicate boolean test(T) Consumer <T> Int. Predicate boolean test( int ) Bi. Predicate boolean test <T, U> void accept(T) Supplier <T> Int. Consumer void accept( int ) <T> Int. Supplier Boolean. Supplier <T, U> Obj. Int. Consumer void accept(T, int) T get () Int. Function int get. As. Int() Bi. Consumer void accept(T, U) java. util. function. <T> boolean get. As. Boolean() R apply( int ) <R> Function R apply Bi. Function <T, R> Unary. Operator T apply( T ) <T> Binary. Operator <T> T apply( T, T ) Int. Binary. Operator int apply. As. Int( int ) int apply. As. Int (int , int) int apply. As. Int(T) <T> Int. To. Long Function apply. As. Long. To. Int Function apply. As. Int PJV 25 <T, U, R> Int. Unary. Operator To. Int. Function Int alter Long Double R apply To. Int. Bi. Function int apply. As. Int <T, U> Int. To. Double Function apply. As. Double Long. To. Double Function apply. As. Double. To. Int Double. To. Long Function apply. As. Int Function apply. As. Long 3
java. util. function. public interface Supplier<T> { T get( ); } public interface Int. Supplier { int get. As. Int( ); } public interface Consumer<T> { void accept( T t ); default Consumer<T> and. Then( Consumer<? super T> after ) { Objects. require. Non. Null( after ); return ( T t ) -> { accept( t ); after. accept( t ); } } } public interface Int. Consumer { void accept( int value ) ; default Int. Consumer and. Then( Int. Consumer after ) { Objects. require. Non. Null( after ); return ( int t ) -> { accept( t ); after. accept( t ); } } } PJV 25 4
java. util. function. public interface Predicate<T> { boolean test( T t ); default Predicate<T> and ( Predicate<? super T> other ) { // též pro or return this. test( t ) && other. test( t ); } default Predicate<T> negate ( ) { return ! test( t ); } static <T> Predicate<T> is. Equal( Object target. Ref ) { … } } public interface Bi. Predicate<T, U> { boolean test( T t, U u ); } } public interface Int. Predicate { boolean test( int value ); }PJV 25 // ostatní jako v Predicate<T> // též pro Long a Double // ostatní jako v Predicate<T> 5
java. util. function. Function<T, R> public interface Function<T, R> { R apply( T t ); default <V> Function<V, R> compose( Function<? super V, ? extends T> before ) { Objects. require. Non. Null( before ); return ( V v ) -> apply( before. apply(v) ); } default <V> Function<T, V> and. Then( Function<? super R, ? Extends V> after ) { Objects. require. Non. Null( after ); return ( T t ) -> after. apply( t ) ); } static <T> Function<T, T> identity( ) { return t -> t; } } PJV 25 6
Příklad class Values<T> { public T z; } // pomocná třída pro úschovu dat { Function<Number, Double> | Unary. Operator<Number> } KMHto. MSEC = v -> v. double. Value() / 3. 6 ; Values<Number> w = new Values<>(); // pomocný objekt Arrays. as. List( 1, 10. 0, 36 ) // typ List<Number>. stream(). peek( t -> w. z = t ). map( KMHto. MSEC ). map ( v -> v + " " ). for. Each( v -> System. out. println( w. z + " " + v ) ); Jelikož výsledek MSEC je typu double lze Stream<Number>. flat. Map( v -> Stream. of( v. double. Value() / 3. 6 ) ). flat. Map. To. Double( v -> Double. Stream. of( v. double. Value() / 3. 6 ) ) PJV 25 7
java. util. function. Bi. Function<T, U, R> public interface Function<T, U, R> { R apply( T t , U u ); default <V> Bi. Function< T, U, V > and. Then( Function<? super R, ? Extends V> after ) { Objects. require. Non. Null( after ); return ( T t, U u ) -> after. apply( t, u ) ); } } Příklad - objem válce: Bi. Function<Double, Double> volume = ( r , h ) -> Math. PI * r * h; PJV 25 8
java. util. function. public interface Unary. Operator<T> extends Function<T, T> { static <T> Unary. Operator<T> identity( ) { return t -> t; } } public interface Binary. Operator<T> extends Function<T, T, T> { public static <T> Binary. Operator<T> min. By( Comparator <? super T> comparator ) { Objects. require. Non. Null( comparator ); return ( a, b ) -> comparator. compare( a, b ) <= 0 a : b; } } Analogicky max. By PJV 25 při comparator. compare( a, b ) >= 0 a : b; 9
Příklad Int. Stream public interface Unary. Operator<T> extends Function<T, T> { Int. Unary. Operator g = new Int. Op( ), h = n -> n*5; Int. Stream. iterate(1, h). limit( 25 ). for. Each( p -> System. out. println( p ) ); …. . class Int. Op implements Int. Unary. Operator { @Override public int apply. As. Int( int m ) { return 3 * m; } } PJV 25 10
Příklad Funkce static Function<Number, Double> p = a -> a. double. Value( )*2 ; static Function<Number, Double> q = a -> a. double. Value( )/2 ; static Function<Number, Double> r = x -> double a = x. a. double. Value( ); p. and. Then(q). apply( a. double. Value( ) ); System. out. println( fg. apply( 2 ) ); static Double. Function<Double> f = x -> Math. log(x); static Double. Function<Double> g = x -> Math. exp(x); static Double. Function<Double> h = x -> g. apply( f. apply(x) ); double y = fg. apply( 5 ); static Function<String, Integer> to. Integer = Integer: : value. Of; int k = to. Integer. apply("123"); PJV 25 11
Příklad public interface P { public void m( ); } public interface Q { public int m( int i ); } public class C implements P, Q { P p; Q q; public C( P p ) { this. p = p; } public C( Q q ) { this. q = q; } @Override public void m( ) { p. m( ); } @Override public int m( int i ) { return q. m( i ); } } P p = ( ) -> { System. out. println("Run p. m"); }; p. m( ); new C( ( ) -> { System. out. println("Run p. m"); } ). m(); int k = new C( ( int i ) -> 7 * i ). m( 3 ); PJV 25 12
Příklad Mapping ( 1/2 ) Metoda pro konverzi seznamu položek typu T na seznam položek typu R: static <T, R> List<R> map( Function<T, R> function, List<T> source ) { List<R> to = new Array. List<>( ); for (T item : source) to. add( function. apply( item ) ); // T -->> R return to; } kde lze automaticky převést na: source. stream( ). for. Each( item -> to. add( function. apply( item ) ) ); a ten dále na: source. stream(). for. Each( new Consumer<T>( ) { public void accept( T item ) { to. add( function. apply( item ) ); } } ); PJV 25 13
Příklad Mapping ( 2/2 ) Použití: String[ ] ss = { "1", "2", "333", "4", "5" }; List<String> digits = Arrays. as. List( ss ); List<Integer> numbers = map( x -> new Integer( x ), digits ); System. out. println( numbers ); PJV 25 14
Příklad Consuming ( 1/2 ) Občanům-konzumentům jen dobré zprávy zajistí cenzor s prdikátem: List< Integer > num = Arrays. as. List( 1, -2, 3 ); Citizen c 1 = new Citizen( ), c 2 = new Citizen( ), c 3 = new Citizen( ); Censor z = new Censor( ); num. stream( ). filter( z ). iterator( ). for. Each. Remaining( c 1. and. Then( c 2. and. Then( c 3 ) ) ); kde: class Citizen implements Consumer { public void accept( Object t ) { System. out. println( t ); } } class Censor implements Predicate<Integer> { public boolean test( Integer t ) { return t > 0; } } PJV 25 15
Příklad Consuming ( 2/2 ) Obdobné napsané pomocí LE: List< Integer > num = Arrays. as. List( new Integer[ ] { 1, -2, 3 } ); Citizen c 1 = t -> System. out. println( "X: "+t ) ; Citizen c 2 = t -> System. out. println( "Y: "+t ) ; Citizen c 3 = t -> System. out. println( "Z: "+t ) ; num. stream( ). filter( x -> x > 0 ) // <<< skrytý cenzor. iterator( ). for. Each. Remaining( c 1. and. Then( c 2. and. Then( c 3 ) ) ); kde: @Functional. Interface interface Citizen extends Consumer{ } PJV 25 16
- Slides: 16