Lambda (two) lambda expression uses

Lambda (two) lambda expression uses

Lambda-expression:

/*
    param list   arrow        lambda body
    (o1,o2)       ->       o1.getColor().CompareTo(o2.getColor());
*/

Lambda expressions need to match a predefined function interfaces:

/ * 
FunctionalInterface Interface: Predicate <T> 
Method: Boolean Test (T T); 

FunctionalInterface Interface: Consume <T> 
method: void Accept (T T); 

FunctionalInterface Interface: Function <T, R> 
method: R apply (T t ); 

FunctionalInterface Interface: suppiler <T> 
method: GET T (); 
* /

 Simple use cases, source code as follows

Consumer<String> c = s -> System.out.println(s);

Function<String,Integer> lambda =  s->s.length();

Predicate<Apple> predicate = a->a.getColor().equals("green");

Function<Apple,Boolean> function = a->a.getColor().equals("red");

Supplier<Apple> instance = Apple::new;

If you now want to sort (conventional vsLambda) on Apple's list:

//implement item1
list.sort(new Comparator<Apple>() {
            @Override
            public int compare(Apple o1, Apple o2) {
                return o1.getColor().compareTo(o2.getColor());
            }
        });

//implement item2
list.sort((o1, o2) -> o1.getColor().compareTo(o2.getColor()));

//implement item2
list.sort(comparing(Apple::getColor));

Custom use , source code follows

public static List<Apple> filter(List<Apple> apples, Predicate<Apple> predicate){
        List<Apple> result = new ArrayList<>();
        for(Apple a:apples){
            if(predicate.test(a)){
                result.add(a);
            }
        }
        return result;
    }

public static void main(String[] args) {
        List<Apple> apples = Arrays.asList(new Apple("green", 120), new Apple("red", 150));
        List<Apple> green = filter(apples, a -> a.getColor().equals("green"));
        System.out.println(green);
    }
java 8 API into a maximum of only two parameters, if a plurality of parameters can define their own, source code follows
//自定义FunctionalInterface接口
@FunctionalInterface
public interface ThreeFunction<T,U,K,R> {
    R apply(T t,U u,K k);
}
//具体使用
ThreeFunction<String,Long,String,ComplexApple> tf = ComplexApple::new;
ComplexApple cp = tf.apply("yellow", 111L, "fushi");
System.out.println(cp);
The method of derivation (MethodReference):
/ ** 
 * Use derived by: 
 * 1, class instance method 
 * 2, example of a method object 
 * 3, the constructor 
 * / 
public  static <T> void useConsume (Consumer <T> Consumer, T T) { 
        Consumer .accept (T); 
} 
useConsume (the println the System.out ::, "chuangg" ); 

List <the Apple> Apples = Arrays.asList ( new new the Apple ( "Red", 100 ),
                 new new the Apple ( "Green", 150 ) ,
                 new new the Apple ( "abc", 110 )); 
. apples.stream () forEach (System.out :: println); 
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
int i = Integer.parseInt("123");

Function<String,Integer> f = Integer::parseInt;
Integer apply = f.apply("123");
System.out.println(apply);

BiFunction<String, Integer, Character> f1 = String::charAt;
Character r1 = f1.apply("hello", 2);
System.out.println(r1);

String s = new String("hello");
Function<Integer, Character> f2 = s::charAt;
Character r2 = f2.apply(1);
System.out.println(r2);

Supplier<Apple> appleSupplier = Apple::new;
Apple apple = appleSupplier.get();

BiFunction<String,Long,Apple> appleBiFunction = Apple::new;
Apple blue = appleBiFunction.apply("blue", 123L);
System.out.println(blue);

Guess you like

Origin www.cnblogs.com/itgcjava/p/lambda_r2.html
Recommended