java lamda expression urinate

java lamda expression urinate

1. What is functional programming (explanation on Baidu Encyclopedia)

 

 

 2. Why use functional programming (What are the benefits)

  1, the code is simple, reduce code

  2, close to natural language, easy to understand

  Traditional implementing the packet

List<Student> students;
Map<String,List<Student>> maps = Maps.newHashMap();
for(Student student : students){
    List<Student> studentList =   students.getOrDefault(student.getSex(), Lists.newArrayList());
    studentList.add(student);
    maps.put(student.getSex(), studentList);
}

  Use lambda expressions

Map<String, List<Student>> maps = student.stream.collect(Collerctor.groupBy(Student :: getSex));

  3, to achieve a good concurrent processing

3. What is a lambda expression

  1. A method for the expression thereof, using expression results returned back to return

  (paramates)-> expression

    (a, b) -> return a + b

  2, the method body code blocks, it must contain {}, and the need to use return a return value, but if the block is returned void, then no return value

  (parameter) -> {statements;}   

  (int a) -> (System.out.println(a))

  (int a) -> (return a*a);

4, the underlying implementation of lambda

BinaryOperator binaryOperator = (int a, int b) -> {return a + b;};
if(binaryOperator instanceof BinaryOperator){System.out.println(binaryOperator.getClass());}
int sum = binaryOperator.applyAsInt(2,3);
System.out.println(sum);

 It can be seen binaryOperator is actually an anonymous inner class that inherits the BinaryOperator

 

Guess you like

Origin www.cnblogs.com/zhangchiblog/p/11449046.html