REMEMBER JAVA

MINTE9
2REMEMBER




Lambdas

p193 Anonymous classes are verbose and hard to use.
 RunCode
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Anonymous class instance as a function object
 * Obsolete!
 */

class App {
            
    public static void main(String[] args) {
        
        List<String> words = new ArrayList<>();
        words.add("ccc");
        words.add("a");
        words.add("bb");

        Collections.sort(words, 
            new Comparator<String>() { // anonymous class
                @Override public int compare(String s1, String s2) {
                    return Integer.compare(
                        s1.length(), s2.length()
                    );
                }
            }
        );
        
        words.forEach(System.out::println); // a , bb , ccc
    }
}
... 26 lines
˄˄˄
p194 Java 8 allows us to create instances of functional interfaces using lamdba expressions.
 RunCode
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Lambdas are similar to anonymous classes,
 * but more concise 
 */
 class App {
            
    public static void main(String[] args) {
        
        List<String> words = Arrays.asList("ccc", "a", "bb");

        Collections.sort(words, // Look Here
            (s1, s2) -> Integer.compare(s1.length(), s2.length())
        );
        
        words.forEach(System.out::println); // a , bb , ccc
    }
}
... 18 lines
˄˄˄
 
import java.util.Arrays;
import java.util.List;

/**
 * We can make the code more concise ...
 * by using sort method added to List interface in Java 8
 */
 class App {
            
    public static void main(String[] args) {
        
        List<String> words = Arrays.asList("ccc", "a", "bb");

        words.sort( // Look Here
            (s1, s2) -> Integer.compare(s1.length(), s2.length())
        );
        
        words.forEach(System.out::println); // a , bb , ccc
    }
}
... 15 lines
˄˄˄
Enum    (2/2)

Enum

p195 The constructor stores the lambda in an instanced field. Later, the method < run > invokes the lambda expression.
 RunCode
/**
 * Without lambdas
 */
class App {

    public enum Operation {

        PLUS ('+') { public int run(int x, int y) { return x + y; } },
        MINUS('-') { public int run(int x, int y) { return x - y; } },
        ;

        private char ch;
        private Operation(char ch) { this.ch = ch; }
        public String toString() { return ch + ""; }
        public abstract int run(int x, int y);

    }

    public static void main(String[] args) {
        
        int a = Operation.PLUS.run(1, 2);
        int b = Operation.MINUS.run(20, 10);

        System.out.println(a); // 3
        System.out.println(b); // 10

        System.out.println(Operation.PLUS); // +
        System.out.println(Operation.MINUS); // -
    }
}
... 22 lines
˄˄˄
 RunCode
/**
 * With lambdas
 */

import java.util.function.IntBinaryOperator;

class App {

    public enum Operation {

        PLUS  ((x, y) ->  x + y ),
        MINUS ((x, y) ->  x - y );

        IntBinaryOperator op;
        private Operation(IntBinaryOperator op) { this.op = op; }
        public int run(int x, int y) { return op.applyAsInt(x, y); }
    }

    public static void main(String[] args) {

        int a = Operation.PLUS.run(1, 2);
        int b = Operation.MINUS.run(20, 10);

        System.out.println(a); // 3
        System.out.println(b); // 10
    }
}
... 19 lines
˄˄˄

Questions    
No. 63   What is JUnit
CARD
62
63

        A B C D E F
🔔
1/1