差異處

這裏顯示兩個版本的差異處。

連向這個比對檢視

java:effective_java:lambdas_and_streams:prefer_lambdas_to_anonymous_classes [2019/07/14 23:46]
tony
java:effective_java:lambdas_and_streams:prefer_lambdas_to_anonymous_classes [2023/06/25 09:48]
行 1: 行 1:
-{{tag>​java effective_java}} +
-====== Effective Java - Prefer lambdas to anonymous classes ====== +
-===== Introduction & My Opinion ===== +
-這個Item主要提倡使用Lambda去取代Anonymous Class。以書中Sort的例子來說:​ +
-<code java> +
-Collections.sort(words,​ new Comparator<​String>​() { +
- public int compare(String s1, String s2) { +
- return Integer.compare(s1.length(),​ s2.length());​ +
-+
-}); +
-</​code>​ +
-假如我改用Lambda去寫,程式會簡潔許多:​ +
-<code java> +
-Collections.sort(words,​ (s1, s2) -> Integer.compare(s1.length(),​ s2.length()));​ +
-</​code>​ +
-如果再搭配Comparator原本提供的comparingInt,會更簡潔:​ +
-<code java> +
-Collections.sort(words,​ Comparator.comparingInt(String::​length));​ +
-</​code>​ +
-針對這部分的使用,書中有幾點必須注意的:​ +
-  - 除非看不懂,否則可以省略參數Type宣告,讓code簡潔些;如s1和s2,並沒把它宣告成(String s1, String s2)。我的觀點:​ 省略宣告其實這是讓我有時需要多花點時間看參數種類的原因之一。 +
-  - 只適用於Functional interface。 +
-  - 假如method會存取到物件的member或使用this,就不適用這種方法。 +
-  - 假如要做序列化,就不適用這種方法,可改用private static nested class。 +
-  - 要使用Lambda寫法的method type必須夠明確,否則編譯器可能無法推斷出type而顯是錯誤。 +
-除了這幾點以外,我認為可測性也是要注意的部分;假如匿名類別內的實作與IO有關,我就不會使用匿名類別。\\ +
-\\ +
-書中還有提及到把這種做法使用在Operation的Enum上,這部分是我第一次看到的:​ +
-<code java> +
-public enum Operation { +
- PLUS ("​+",​ (x, y) -> x + y), +
- MINUS ("​-",​ (x, y) -> x - y), +
- TIMES ("​*",​ (x, y) -> x * y), +
- DIVIDE("/",​ (x, y) -> x / y); +
- private final String symbol; +
- private final DoubleBinaryOperator op; +
- Operation(String symbol, DoubleBinaryOperator op) { +
- this.symbol = symbol; +
- this.op = op; +
-+
- @Override public String toString() { return symbol; } +
- public double apply(double x, double y) { +
- return op.applyAsDouble(x,​ y); +
-+
-+
-</​code>​ +
-使用這種做法時,method的內容最好別超過3行,避免降低了程式的可讀性。 +
-===== Note ===== +
-Effective Java第三版Item 42。 +
-===== Reference ===== +
-  * Effective Java, 3/e +
-=====    ===== +
----- +
-\\ +
-~~DISQUS~~+