package com.mycompany.lambdaexam;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
class Person {
private String name;
private int age;
}
public class Main {
public static void main(String[] args) {
List<Person> list = new ArrayList<>();
list.add(new Person("Zach", 18));
list.add(new Person("Albert", 59));
list.add(new Person("Donna", 38));
list.add(new Person("Erich", 40));
list.add(new Person("Brenda", 16));
Collections.sort(list, new Comparator<Person>() {
@Override
public int compare(Person p1, Person p2) {
return p1.getName().compareTo(p2.getName());
}
});
for (Person p : list) {
System.out.println(p);
}
}
}
Person(name=Albert, age=59) Person(name=Brenda, age=16) Person(name=Donna, age=38) Person(name=Erich, age=40) Person(name=Zach, age=18)
package com.mycompany.lambdaexam;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
class Person {
private String name;
private int age;
}
public class Main {
public static void main(String[] args) {
List<Person> list = new ArrayList<>();
list.add(new Person("Zach", 18));
list.add(new Person("Albert", 59));
list.add(new Person("Donna", 38));
list.add(new Person("Erich", 40));
list.add(new Person("Brenda", 16));
Collections.sort(list,
(p1, p2) -> p1.getName().compareTo(p2.getName())
);
list.forEach(System.out::println);
}
}
(型 引数, 型 引数) -> {処理; 処理; return 返値;} (Person p1, Person p2) -> { return p1.getName().compareTo(p2.getName()); };
(引数, 引数) -> {処理; 処理; return 返値;} (p1, p2) -> { return p1.getName().compareTo(p2.getName()); };
(引数, 引数) -> return 返値; (p1, p2) -> p1.getName().compareTo(p2.getName());
list.forEach(s -> System.out.println(s));
list.forEach(System.out::println); // Static Method
list.forEach(this::doSomething()); // Class Method
package com.mycompany.lambdaexam;
interface MyFunctionIf {
String pre(String arg);
String main(String arg);
String post(String arg);
}
@FunctionalInterface
interface MyFunctionImpl extends MyFunctionIf {
@Override
default String pre(String arg) {
return "(PRE-" + arg + ")";
}
@Override
default String post(String arg) {
return "(" + arg + "-POST)";
}
}
class StringProcessor {
public static String process(String str, MyFunctionImpl func) {
return func.post(func.main(func.pre(str)));
}
}
public class FunctionExam {
public static void main(String[] args) {
System.out.println(StringProcessor.process("aBcDeF", s -> s.toLowerCase()));
System.out.println(StringProcessor.process("aBcDeF", s -> s.toUpperCase()));
}
}
((pre-abcdef)-POST) ((PRE-ABCDEF)-POST)
interface Runnable { public void run(); }
new Thread(new Runnable() {
@Override
public void run() {
System.out.prinltn("Hello");
}
});
new Thread(() -> System.out.println("Hello"));
package com.mycompany.lambdaexam;
import java.util.function.ToLongBiFunction;
import java.util.function.ToLongFunction;
public class LambdaRecursive {
public static void main(String[] args) {
ToLongBiFunction<ToLongBiFunction ,Long> fibonacciInner = (me, value) -> {
if (0L == value) return 0L;
if (1L == value) return 1L;
return me.applyAsLong(me, value - 1L) + me.applyAsLong(me, value - 2L);
};
ToLongFunction<Long> fibonacci = (value) -> fibonacciInner.applyAsLong(fibonacciInner, value);
System.out.println(fibonacci.applyAsLong(10L));
}
}
55
package com.mycompany.lambdaexam;
public class ScopeExam {
public static void main(String[] args) {
ScopeExam thisObj = new ScopeExam();
thisObj.scope();
}
public void scope() {
int num = 123;
Runnable runner = () -> {
System.out.println(this.getClass().getName());
System.out.println(num);
};
runner.run();
}
}
com.mycompany.lambdaexam.ScopeExam 123
package com.mycompany.lambdaexam;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
class Person {
private String name;
private int age;
}
public class Main {
public static void main(String[] args) {
List<Person> list = new ArrayList<>();
list.add(new Person("Zach", 18));
list.add(new Person("Albert", 59));
list.add(new Person("Donna", 38));
list.add(new Person("Erich", 40));
list.add(new Person("Brenda", 16));
// Collections#sort(List, Comparator)
Collections.sort(list,
(p1, p2) -> p1.getName().compareTo(p2.getName())
);
// List#forEach(Consumer)
list.forEach(p -> System.out.println(p));
System.out.println("--");
// List#removeIf(Predicate)
list.removeIf(p -> p.getAge() > 20);
list.forEach(p -> System.out.println(p));
System.out.println("--");
// List#removeAll(UnaryOperator)
list.replaceAll(p -> new Person(p.getName(), p.getAge() + 10));
list.forEach(p -> System.out.println(p));
}
}
Person(name=Albert, age=59) Person(name=Brenda, age=16) Person(name=Donna, age=38) Person(name=Erich, age=40) Person(name=Zach, age=18) -- Person(name=Brenda, age=16) Person(name=Zach, age=18) -- Person(name=Brenda, age=26) Person(name=Zach, age=28)
Set にも forEach() removeIf() がある
package com.mycompany.lambdaexam;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
class Person {
private String name;
private int age;
}
public class Main {
public static void main(String[] args) {
Map<String, Integer> personMap = new LinkedHashMap<>();
// lambda 式の返り値を格納する
personMap.compute("Zach", (key, existValue) -> null == existValue ? 18 : existValue);
// 値がない場合にのみ lambda 式の返り値を格納する
personMap.computeIfAbsent("Albert", (key) -> 59);
personMap.put("Donna", 38);
personMap.put("Erich", 40);
personMap.put("Brenda", 16);
// 値がある場合にのみ lambda 式の返り値を上書きする
personMap.computeIfPresent("Donna", (key, existValue) -> existValue + 10);
personMap.computeIfPresent("Alphons", (key, existValue) -> 99);
// 値があったら lambda 式の返り値を格納。なかったら設定値を格納
personMap.merge("Erich", 50, (oldValue, newValue) -> oldValue);
personMap.merge("Sonton", 3, (oldValue, newValue) -> oldValue);
// Map#forEach(Consumer)
personMap.forEach((key, value) -> System.out.println(key + "=" + value));
System.out.println("--");
}
}
Zach=18 Albert=59 Donna=48 Erich=40 Brenda=16 Sonton=3