728x90
๋ฐ์ํ
๐ฏ ์์ดํ 44. ํ์ค ํจ์ํ ์ธํฐํ์ด์ค๋ฅผ ์ฌ์ฉํ๋ผ.
ํ์ํ ์ฉ๋์ ๋ง๋ ๊ฒ ์๋ค๋ฉด, ์ง์ ๊ตฌํํ์ง ๋ง๊ณ ํ์ค ํจ์ํ ์ธํฐํ์ด์ค๋ฅผ ํ์ฉํ๋ผ.
public class Item44 {
public static void main(String[] args) {
/**
* 1 Supplier
*/
Supplier<String> supplier = () -> "hello world";
System.out.println("supplier = " + supplier.get());
Supplier<Double> doubleSupplier = () -> Math.random();
System.out.println("doubleSupplier = " + doubleSupplier.get());
printRandomDoubles(doubleSupplier, 5);
System.out.println();
/**
* 2 Consumer
*/
Consumer<String> stringConsumer = str -> System.out.println(str);
stringConsumer.accept("Hell World");
Consumer<Integer> integerConsumer = x -> System.out.println("Processing Integer " + x);
List<Integer> integerList = Arrays.asList(4,2,3);
process(integerList, integerConsumer);
System.out.println();
/**
* 3 BiConsumer
*/
BiConsumer<Integer, Double> doubleBiConsumer = (index, input) -> System.out.println("Processing " + input+" at index "+index);
List<Double> doubleList = Arrays.asList(4.1,2.2,3.3);
process(doubleList, doubleBiConsumer);
System.out.println();
/**
* 4 Predicate
*/
Predicate<Integer> integerPredicate = x -> x>0;
System.out.println(integerPredicate.test(2));
List<Integer> inputs = Arrays.asList(10,-5,-2,0,3);
System.out.println("filter(inputs, integerPredicate) = " + filter(inputs, integerPredicate));
System.out.println("filter(inputs, integerPredicate.negate()) = " + filter(inputs, integerPredicate.negate()));
System.out.println("filter(inputs, integerPredicate.or(x-> x==0)) = " + filter(inputs, integerPredicate.or(x-> x==0)));
System.out.println("filter(inputs, integerPredicate.and(x-> x%2 == 0)) = " + filter(inputs, integerPredicate.and(x-> x%2 == 0)));
System.out.println();
/**
* 5 Comparator
*/
List<User> users = new ArrayList<>();
users.add(new User(3, "Alice"));
users.add(new User(1, "Charlie"));
users.add(new User(5, "Bob"));
System.out.println("users = " + users);
Comparator<User> idComparator = Comparator.comparingInt(User::getId);
Collections.sort(users, idComparator);
System.out.println("users = " + users);
Collections.sort(users, Comparator.comparing(User::getName));
System.out.println("users = " + users);
}
public static void printRandomDoubles(Supplier<Double> randomSupplier, int count){
for (int i = 0; i < count; i++){
System.out.println(randomSupplier.get());
}
}
public static <T> void process(List<T> inputs, Consumer<T> processor){
for (T input : inputs){
processor.accept(input);
}
}
public static <T> void process(List<T> inputs, BiConsumer<Integer, T> processor){
for (int i = 0; i < inputs.size(); i++) {
processor.accept(i, inputs.get(i));
}
}
public static <T> List<T> filter(List<T> inputs, Predicate<T> condition){
List<T> output = new ArrayList<>();
for (T t : inputs) {
if(condition.test(t)){
output.add(t);
}
}
return output;
}
static class User {
private int id;
private String name;
public User(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {return id;}
public void setId(int id) {this.id = id;}
public String getName() {return name;}
public void setName(String name) {this.name = name;}
@Override
public String toString() {return "User{" + "id=" + id + ", name='" + name + '\'' + '}';}
}
}
์๋ ํ์ค ํจ์ํ ์ธํฐํ์ด์ค๋ฅผ ํ์ฉํ ๊ฐ๋จํ ์์ ์ด๋ค.
ํ์ง๋ง, ํ์ค ํจ์ํ ์ธํฐํ์ด์ค๊ฐ ์ง์ํ์ง ์๋ ๊ธฐ๋ฅ์ ๋ง๋ค๊ณ ์ ํ๋ค๋ฉด ๋ฐ๋์ @FuntionalInterface
๋ฅผ ์ฌ์ฉํ์.
์ด ์ ๋ํ
์ด์
์ ์ฌ์ฉํ๋ ์ด์ ๋ @Override
๋ฅผ ์ฌ์ฉํ๋ ์ด์ ์ ๋น์ทํ๋ค.
- ํด๋น ํด๋์ค์ ์ฝ๋๋ ์ค๋ช ๋ฌธ์๋ฅผ ์ฝ์ ์ด์๊ฒ ๊ทธ ์ธํฐํ์ด์ค๊ฐ ๋๋ค์ฉ์ผ๋ก ์ค๊ณ๋ ๊ฒ์์ ์๋ ค์ค๋ค.
- ํด๋น ์ธํฐํ์ด์ค๊ฐ ์ถ์ ๋ฉ์๋๋ฅผ ์ค์ง ํ๋๋ง ๊ฐ์ง๊ณ ์์ด์ผ ์ปดํ์ผ๋๊ฒ ํด์ค๋ค.
- ๊ทธ ๊ฒฐ๊ณผ ์ ์ง๋ณด์ ๊ณผ์ ์์ ๋๊ตฐ๊ฐ ์ค์๋ก ๋ฉ์๋๋ฅผ ์ถ๊ฐํ์ง ๋ชปํ๊ฒ ๋ง์์ค๋ค.
์ฐธ๊ณ ์๋ฃ
Joshua Bloch, ใEffective Java 3/Eใ, ๊ฐ์๋งต์ ์ฎ๊น, ํ๋ก๊ทธ๋๋ฐ์ธ์ฌ์ดํธ(2018)
http://www.kyobobook.co.kr/product/detailViewKor.laf?ejkGb=KOR&mallGb=KOR&barcode=9788966262281&orderClick=LEa&Kc=
728x90
๋ฐ์ํ