01 28Java middle of functional programming

1 Lamda expression

JDK 1.8 from the start in order to simplify user code development, there is provided specifically Lambda expressions support With this operation can be realized in the form of functional programming. For the more well-known functional programming language: haskell, Scala, the use of functional programming to avoid the cumbersome process out for Issue in object-oriented programming.

Object-oriented in the process of its long-term development has been part of opponents, opponents argue that these object-oriented design is too complicated, and too cumbersome to program a simple example.
Example: Observation of traditional development issues

interface IMessage{
	public abstract void fun();
}

public class JavaDemo{

	public static void main(String[] args){
		IMessage msg = new IMessage(){
			public void fun(){
				System.out.println("hhy");
			}
		};
		msg.fun();
	}
}

In such a program which, in fact, the core design of the structure function only one line statement, but we still Yaoan given a complete object-oriented development. So these issues with the development of technology, but also more and more prominent.
Example: Use Lambda Expressions achieve exactly the same function as before

interface IMessage{
	public abstract void fun();
}

public class JavaDemo{

	public static void main(String[] args){
		IMessage msg = ()->{
				System.out.println("hhy");
		};
		msg.fun();
	}
}

With this form avoids the complex requirements of the object-oriented structure.
Lambda expressions If you want to use, it must meet an important requirement: SAM (Single Abstract Method), only an abstract method. Only an interface method is called a function interface, the interface function can only be used by Lambda expressions. In order to show a clear interface function interface, use Annotation annotation (@FunctionalInterface).
Example: Using functional annotation interfaces

@FunctionalInterface
interface IMessage{
	public abstract void fun();
}

public class JavaDemo{

	public static void main(String[] args){
		IMessage msg = ()->{
				System.out.println("hhy");
		};
		msg.fun();
	}
}

For purposes of lambda expressions, there is provided the following formats:
(1) no parameters: () -> {};
(2) :( parameter that has parameters, parameters) -> {};
(3) If the current :( statement returns only one line parameter, parameter) -> statement;
example: there is no way to define the parameters.

@FunctionalInterface
interface IMessage{
	public abstract void fun();
}

public class JavaDemo{

	public static void main(String[] args){
		IMessage msg = ()->{
				System.out.println("hhy");
		};
		msg.fun();
	}
}

Example: Defining the parameters of methods

@FunctionalInterface
interface IMath{
	public abstract int add(int x, int y);
}

public class JavaDemo{

	public static void main(String[] args){
		IMath math = (x, y)->{
				return x + y;
		};
		System.out.println(math.add(10, 3));
	}
}

Single statement can be further simplified
example: simplify the code

@FunctionalInterface
interface IMath{
	public abstract int add(int x, int y);
}

public class JavaDemo{

	public static void main(String[] args){
		IMath math = (x, y)-> x + y;
		System.out.println(math.add(10, 3));
	}
}

Use Lmbda expression can indeed get rid of in the traditional object-oriented restrictions on the structure, makes the code more simple.

2 reference method

Reference biggest feature is the type of data memory can point to deal with, but in the traditional development, has been using the object reference operations, from after JDK 1.8, there is also provided a method of reference. , Namely: Different methods can be described as the same method name. If you want to provide a reference method in Java which has four forms:
(1) references a static method: class name :: static method name;
(2) a reference to an instance of an object's method: :: instance of an object common methods;
(3 ) refer to a specific type of method: specific class :: conventional method;
(4) reference constructor: class name :: new.
Example: reference a static method
String static method: public static String valueOf (int i )

@FunctionalInterface
interface IFunction<T, U>{
	public abstract T fun(U n);
}

public class JavaDemo{

	public static void main(String[] args){
		IFunction<String, Integer> f = String::valueOf;
		System.out.println(String.valueOf(12));
		System.out.println(f.fun(12));
	}
}

The method of using a plurality of reference can be defined as a method name, but must be a function interface requirements.
Example: method object reference example

@FunctionalInterface
interface IFunction<T>{
	public abstract T fun();
}

public class JavaDemo{

	public static void main(String[] args){
		IFunction<String> f = "hhy"::toUpperCase;
		System.out.println(f.fun());
	}
}

When carrying out the method may be referenced in some operations reference method in a particular class.
Example: reference method specified class

@FunctionalInterface
interface IFunction<T, U>{
	public abstract T fun(U s1, U s2);
}

public class JavaDemo{

	public static void main(String[] args){
		IFunction<Integer, String> f = String::compareTo;
		System.out.println(f.fun("A", "a"));
	}
}

Example: reference constructor

@FunctionalInterface
interface IFunction<T>{
	public abstract T fun(String s1, int s2);
}

class Person{
	private String name;
	private int age;

	public Person(){}

	public Person(String name, int age){
		this.name = name;
		this.age = age;
	}

	public String toString(){
		return name + "  " + age;
	}
}

public class JavaDemo{

	public static void main(String[] args){
		IFunction<Person> f = Person::new;
		System.out.println(f.fun("lks", 23));
	}
}

Provide a reference method under more circumstances of concept only make up for a support reference.

3 built-in function interfaces

Provided in JDK 1.8 Lambda expressions have also provided a method references, but you will find now if defined function interface by the developers themselves, often need to use the "@FunctionalInterface" annotation to declare a lot, so a lot of cases if the order direct reference function can be easily provided in the system interface.

Specifically there is provided a package in java.util.function development system, which can directly interface function, in this package are the following a few core following interfaces:
1, functional interface function
interface definition:

@FunctionalInterface
public interface Function<T,R>{
    public R apply​(T t);
}

Interface:
String there is a method: public boolean startsWith (String str) ;

import java.util.function.*;
public class JavaDemo{

	public static void main(String[] args){
		Function<String, Boolean> fun = "**hello"::startsWith;
		System.out.println(fun.apply("**"));
	}
}

2, consumer interface functions: data processing operations only, without any return.
When the data output is performed using the system: System.out ... println ();
interface definition:

@FunctionalInterface
public interface Consumer<T>{
    public void accept​(T t);
}

Interface:

import java.util.function.*;
public class JavaDemo{

	public static void main(String[] args){
		Consumer<String> con = System.out::println;
		con.accept("hhy");
	}
}

3, supply and function interface
provided with a small letter the toLowerCase method (), the value of this method is not received in the String class, but have a return value
interface definition:

@FunctionalInterface
public interface Supplier<T>{
    public T get();
}

Interface:

import java.util.function.*;
public class JavaDemo{

	public static void main(String[] args){
		Supplier<String> sup = "HHY LOVE" :: toLowerCase;
		System.out.println(sup.get());
	}
}

4, the assertion type function interface: the judging process
has a equalsIngoreCase () method in class String.
Interface definition:

@FunctionalInterface
public interface Predicate<T>{
    public boolean test​(T t);
}

Interface:

import java.util.function.*;
public class JavaDemo{

	public static void main(String[] args){
		Predicate<String> pre = "hhy" :: equalsIgnoreCase;
		System.out.println(pre.test("HHY"));
	}
}

Among the actual project development for the future, if the JDK itself provides the function interface can be used by us, then there is no need to be redefined.

Published 77 original articles · won praise 11 · views 2637

Guess you like

Origin blog.csdn.net/weixin_43762330/article/details/104564486