Funciones de java.util.function.*

package com.codemonkey; /** *@author Fer Carraro *@date 11/05/2018 * */ import static java.lang.System.err; import static java.lang.System.out; import java.util.List; import java.util.ArrayList; import java.util.function.Consumer;//Consumidor: un solo valor y no devuelve valor alguno //Consumer<T> (interface) // void accept(T t) // Consumer<T> andThen(Consumer<? super T> after) import java.util.function.Function;//Funciones: Reciben un argumento y devuelven un resultado //Function<T,R> // Function<T,V> andThen(Function<? super R,? extends V> after) // R apply(T t) // Function<V,R> compose(Function<? super V,? extends T> before) // Function<T,T> identity() import java.util.function.Predicate;//Predicado: reciben un argumento y devuelven un valor logico //Predicate<T> // Predicate<T> and(Predicate<? super T> other) // Predicate<T> isEqual(Object targetRef) // Predicate<T> negate() // Predicate<T> or(Predicate<? super T> other) // boolean test(T t) import java.util.function.Supplier;//Proveedor: no tiene parametros de entrada, pero devuelve un resultado //Supplier<T> // T get() public class Main{ public static void main(String[] args){ demoConsumer(); demoFunction(); demoPredicate(); demoSupplier(); } public static void demoSupplier(){ out.println("+++++++++++Supplier++++++++++++"); Supplier<Integer> supplierRandom = () -> (int)(Math.random()*100); Supplier<Double> supplierPI = () -> { final double PI = 3.141516; return PI; }; Supplier<List<Integer>> supplierList = () ->{ List<Integer> lista = new ArrayList<>(); int valor = 9; lista.add(0, new Integer("2")); lista.add(1, 4); lista.add(2, valor); lista.add(3, new Integer(7)); return lista; }; //Usando get() out.println(supplierPI.get());//3.141516 out.println(supplierRandom.get());//numero aleatorio out.println(supplierList.get()); supplierList.get() .stream() .forEach(out::println); } public static void demoPredicate(){ out.println("+++++++++++Predicate++++++++++++"); Predicate<Integer> predicateA = e -> { if(e > 0){ return true; }else{ return false; } }; //Usando test(T t) out.println(predicateA.test(0));//false out.println(predicateA.test(100));//true out.println(predicateA.test(-2));//false Predicate<String> predicateB = e -> {return e.equals("Fernando");}; //Usando and(Predicate<? super T> other) Predicate<String> predicateAnd = predicateB.and(e -> e.length() > 4); out.println(predicateAnd.test("Fernando")? "Correcto" : "Incorrecto");//Correcto out.println(predicateAnd.test("Uriel")? "Correcto" : "Incorrecto");//Incorrecto //Usando or(Predicate<? super T> other) Predicate<String> predicateOr=predicateB.or(s->s.length()==10); out.println(predicateOr.test("Fernando")? "Correcto" : "Incorrecto");//Correcto out.println(predicateOr.test("Alma")? "Correcto" : "Incorrecto");//Incorrecto //Usando negate() Predicate<String> predicateNegate = predicateB.negate(); out.println(predicateNegate.test("Camila"));//true out.println(predicateNegate.test("Fernando"));//false //Usando isEqual(Object targetRef) Predicate<String> predicateIsEquals = Predicate.isEqual("Negative"); out.println(predicateIsEquals.test("Negativo"));//false out.println(predicateIsEquals.test("negative"));//false out.println(predicateIsEquals.test("Negative"));//true } public static void demoFunction(){ out.println("+++++++++++Function++++++++++++"); Function<Integer,String> functionA = e -> { if(e % 3 == 0){ return e+" es divisible entre 3"; }else{ return e+" no es divisible entre 3"; } }; //Usando apply(T t) out.println(functionA.apply(new Integer("3"))); out.println(functionA.apply(27)); out.println(functionA.apply(-2)); Function<Integer,Integer> functionB = e -> (e - 4); Function<Integer,Integer> functionC = e -> (e * 2); //Usando andThen int a = functionB.andThen(functionC).apply(7); out.println(a);// 7 - 4= 3; 3 * 2 = 6 //Usando compose int b = functionB.compose(functionC).apply(3); out.println(b);// 3 - 4 = -1 ; -1 * 2 = 2 Function<Integer,Integer> functionD = Function.identity(); out.println(functionD.apply(5)); out.println(functionD.apply(12)); } public static void demoConsumer(){ out.println("+++++++++++Consumer++++++++++++"); //Uso de accept(T t) Consumer<CharSequence> consumeCharSeq = c -> out.println("Hola, "+c); CharSequence charSeqA = "Genaro"; CharSequence charSeqB = "Irene"; consumeCharSeq.accept(charSeqA); consumeCharSeq.accept(charSeqB); //Uso de andThen(Consumer<? super T> after) Consumer<String> consumeStrA = s -> out.println(s+ " Mundo!"); Consumer<String> consumeStrB = s -> out.println(s+ " Fernando!"); consumeStrA.andThen(consumeStrB).accept("Hola,"); } }
Ejemplos de uso de funciones de java.util.function.*

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.