java lambda expression memo

Functional interface

A functional interface is an interface in which only one abstract method is declared. Other than that, there are no particular restrictions. For example, it doesn't matter if you have multiple default methods or private methods.

What is a lambda expression?

A function that gives an interface implementation on the client (caller). By using a lambda expression, you don't have to create a concrete class that implements the interface. Lambda expressions are only possible if you give an implementation of a functional interface. This is because when there are multiple abstract methods in the interface to which you want to give an implementation, the compiler cannot specify which abstract method to give the implementation (definition) to.

closure

When giving an implementation using a lambda expression, local class, or anonymous class in a method, a function that allows the client's local variables to be used in the block that gives the implementation. Variables used as closures are copied as fields in lambda instances (for java). In some languages references are passed. Variables passed in closures are final.

Code example

InfintieIntSequence.java


public interface InfiniteIntSequence{
        default public boolean hasNext(){ return true; }
        public int next();


        public static InfiniteIntSequence generate(IntSupplier s){
                return s::getAsInt; //@1
        }

        public static InfiniteIntSequence iterate(int seed, IntUnaryOperator f){
                return new InfiniteIntSequence(){
                        int lseed = seed; //@2
                        @Override public int next(){
                                int res=lseed;
                                lseed=f.applyAsInt(lseed);
                                return res;
                        }
                };
        }

        public static InfiniteIntSequence random(int from, int until){
                Random r = new Random();
                int bound = until-from;
                if(0<bound) return ()->r.nextInt(bound)+from;
                else throw new IllegalArgumentException();
        }

        public static InfiniteIntSequence range(int from){
                return iterate(from, i -> i+1 );
        }

        default public void forEach(IntConsumer c, int n){
                while(n-- > 0 && hasNext() ) c.accept(next());
        }
}

What is returned by @ 1: return is an instance of class that inherits the InfiniteIntSequence interface, overriding the next () method. Since InfiniteIntSequence is a functional interface, it is guaranteed that there is only one abstract and sod. Therefore, the compiler can understand that overriding next (), so you can omit writing various things. next () is a signature method that returns an int with no arguments.

Recommended Posts

java lambda expression memo
Java lambda expression [memo]
[Java] Lambda expression
Java lambda expression
Java lambda expression variations
Java 8 lambda expression Feature
Studying Java 8 (lambda expression)
Review java8 ~ Lambda expression ~
Java lambda expression again
[Java] Functional interface / lambda expression
Java memo
Java8 stream, lambda expression summary
Java basic learning content 9 (lambda expression)
What is a lambda expression (Java)
java anything memo
Java Silver memo
Java SE 7 memo
java anything memo 2
Hello Java Lambda
Java specification memo
Java pattern memo
Now let's recap the Java lambda expression
Java development environment memo
java basic knowledge memo
Java learning memo (method)
Java Kuche Day memo
Quarkus saves Java Lambda! ??
[Java ~ Method ~] Study memo (5)
java se 8 programmer Ⅰ memo
Java paid private memo
Understand Java 8 lambda expressions
How to use Java API with lambda expression
[Java ~ Array ~] Study memo 4
Java learning memo (basic)
(Memo) Java for statement
About Java lambda expressions
Java8 to start now ~ forEach and lambda expression ~
Introduction to lambda expression
Explain Java 8 lambda expressions
Java learning memo (interface)
[Java] Implicit inheritance memo
Java table expression injection
Java learning memo (inheritance)
java competitive programming memo
java regular expression summary
[Memo] Java Linked List
Java Lambda Command Pattern
Java8 Lambda expression & Stream design pattern reconsideration --Command pattern -
Getting Started with Legacy Java Engineers (Stream + Lambda Expression)
Java8 Lambda Expression & Stream Design Pattern Rethinking --Null Object Pattern -
Java8 Lambda expression & Stream design pattern reconsideration --Template Method pattern -
Java (WebSphere Application Server) memo [1]
[Java] Variable name naming memo
Java memo (standard class) substring
Java learning memo (data type)
Java memo (standard class) length
Java Silver Study Method Memo
[Java ~ Boolean value ~] Study memo (2)
Create a java method [Memo] [java11]
Java Silver exam preparation memo
[Java] Introduction to lambda expressions