package com.codemonkey;
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.UnaryOperator;
//UnaryOperator<T>
// UnaryOperator<T> identity() ; ademas de andThen, apply, compose
import java.util.function.BinaryOperator;
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()
import java.util.Comparator;
import com.codemonkey.clases.Persona;
public class Main{
public static void main(String args[]){
demoConsumer();
demoFunction();
demoUnaryOperator();
demoBinaryOperator();
demoPredicate();
demoSupplier();
}
public static void demoUnaryOperator(){
out.println("+++++++++UnaryOperator+++++++++");
UnaryOperator<Integer> operator = t -> t * 2;
out.println(operator.apply(5));//10
out.println(operator.apply(10));//20
out.println(operator.apply(15));//30
UnaryOperator<Integer> operator1 = t -> t + 10;
UnaryOperator<Integer> operator2 = t -> t * 10;
//Usando andThen()
int a = operator1.andThen(operator2).apply(5);
out.println(a);//5+10 = 15 * 10 = 150
//Usando compose()
int b = operator1.compose(operator2).apply(5);
out.println(b);// 5 * 10 = 50 + 10 = 60
}
public static void demoBinaryOperator(){
out.println("+++++++++BinaryOperator+++++++++");
BinaryOperator<Integer> operator1 = (a, b) -> a + b;
out.println(operator1.apply(5, 7));// 5 + 7 =12
BinaryOperator<String> operator2 = (s1, s2) -> s1 + s2;
out.println(operator2.apply("Hola, ", "Fernando"));//Hola, Fernando
BinaryOperator<Integer> operator = (a, b) -> a + b;
Function<Integer, Integer> function = n -> n * 2;
//Usando andThen()
out.println(operator.andThen(function).apply(1, 6));//1 + 6 = 7 * 2 = 14
Comparator<Integer> comparator = (a, b) -> (a.compareTo(b));
// Using maxBy()
BinaryOperator<Integer> opMax = BinaryOperator.maxBy(comparator);
System.out.println("Max: " + opMax.apply(5, 6));//6
System.out.println("Max: " + opMax.apply(9, 6));//9
// Using minBy()
BinaryOperator<Integer> opMin = BinaryOperator.minBy(comparator);
System.out.println("Min: " + opMin.apply(5, 6));//5
System.out.println("Min: " + opMin.apply(9, 6));//6
}
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,");
}
}
Más funciones java.util.funtion.*
Serie de ejemplos OCA JSE 8
Serie de ejemplos OCA JSE 8
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.