PROGRAMMING

m9/ JAVA
REMEMBERS




Last update:   29-07-2021

Lambdas

p193 Anonymous classes are verbose and hard to use.
 
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
    }
}
... 20 lines
 
p194 Java 8 allows us to create instances of functional interfaces using lamdba expressions.
 
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
    }
}
... 12 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
    }
}
... 9 lines
 
Enum    (2/2)

Enum

p195 Enum example with no lambdas.
 
/**
 * Enum Example - 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); // -
    }
}
... 16 lines
 
Enum example with lambdas.
 
/**
 * Enum Example - With lambdas
 * 
 * The constructor stores the lambda in an instanced field
 * Later, the method run() invokes the lambda expression
 */

import java.util.function.IntBinaryOperator;

class App {

    public enum Operation {

        PLUS  ('+', (x, y) ->  x + y) {},
        MINUS ('-', (x, y) ->  x - y) {};

        IntBinaryOperator op;
        private Operation(char ch, 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
    }
}
... 16 lines
 

Questions    
What is JUnit

        A B C D E F
🔔
1/1