「Javaの学習」JavaでLambdaを使用するサンプルプログラム
1.Lambda syntax
(parameters) -> expression
(parameters) -> statement
(parameters) -> { statements }
2.Lamba表現式の例
(intx,inty) -> x + y
() -> System.out.println(“hi “+ s);
(String s) -> {intn = s.length();returnn; }
3.Runnableを実行
Runnable r = () -> System.out.println(“Hello!");
r.run();
3.PIメソッド
Callable<Double> pi = () ->3.14;
Double p = pi.call();
4.文字列長さによって文字列をソート
String[] words = {“aaa","b","cc"};
Arrays.sort(words, (s1, s2) -> s1.length() – s2.length());
// 等しい:
Arrays.sort(words, (String s1, String s2) -> s1.length() – s2.length());