CHAP02 - Modern-Java-in-Action/Online-Study GitHub Wiki
λμμ νλΌλ―Έν°ννλ©΄ ν¨μλ₯Ό λ€λ₯Έ λ©μλμ λ§€κ°μΈμλ‘ λκΈΈ μ μλ€. μ’μ μλ Collections.sort()
λ©μλμ Comparator
μ΅λͺ
ν΄λμ€λ‘ κ°μΌ compare()
λ₯Ό μ λ¬νλ κ²½μ°μ΄λ€.
2μ₯μμλ μ΄λ¬ν λμ νλΌλ―Έν°νκ° μ νμνμ§λ₯Ό μ€λͺ νλ€. μλ°8 μ΄νλ‘ λμ νλΌλ―Έν°νλ₯Ό μ΄λ»κ² νλμ§, μ΄λ₯Όν λ©΄ λλ€ννμκ³Ό λ©μλ μ§μ μ°Έμ‘°λ 3μ₯μμ ꡬ체μ μΌλ‘ λ€λ£¬λ€.
λ©μλ : ν΄λμ€μ μ’ μλ λμ
ν¨μ : ν΄λμ€μ μ’ μλμ§ μμ λμ
컬λ μ μ λ€μκ³Ό κ°μ λ©μλλ₯Ό ꡬννλ€κ³ κ°μ ν΄λ³΄μ. μ΄ λ©μλκ° μ΄λ€ λμμ μνν μ§ μ ν΄μ§μ§ μμ μνμ΄λ€. μν©μ λ°λΌ λ€μν λμμ μννκ³ , μμλλλ‘ λ³ννλ μꡬμ¬νμ λ§μΆλ €λ©΄ 볡μ‘ν μ½λλ₯Ό μμ±ν΄μΌ νκ² λ€.
- 리μ€νΈμ λͺ¨λ μμμ λν΄ 'μ΄λ€ λμ'μ μνν μ μμ
- 리μ€νΈμ κ΄λ ¨ μμ μ λλΈ λ€μ 'μ΄λ€ λ€λ₯Έ λμ'μ μνν μ μμ
- μλ¬κ° λ°μνλ©΄ 'μ ν΄μ§ μ΄λ€ λ€λ₯Έ λμ'μ μνν μ μμ
κ·Έ λμ μ μ΄λ€ λμμ μνν μ§ λ©μλμ λ§€κ°μΈμλ‘ λ겨λ°μΌλ©΄ μ΄λ¨κΉ?
κΈ°λ³Έ μμ μ½λλ λ€μκ³Ό κ°λ€.
enum Color {RED, GREEN}
public class Apple {
Color color;
Integer weight;
Apple(Color color, int weight, int no){
this.color = color;
this.weight = Integer.valueOf(weight);
this.no = Integer.valueOf(no);
}
public Color getColor(){return this.color;}
public Integer getWeight(){return this.weight;}
}
List<Apple> inventory = new ArrayList<>();
inventory.add(new Apple(Color.GREEN, 500));
inventory.add(new Apple(Color.RED, 750));
inventory.add(new Apple(Color.RED, 400));
inventory.add(new Apple(Color.GREEN, 600));
inventory.add(new Apple(Color.RED, 100));
inventory.add(new Apple(Color.GREEN, 2000));
λ¬΄κ² λλ μμ ꡬλΆνκΈ° μν΄ if-else
λ¬Έμ κ°κ° μμ±νλ λμ μ ν΄λΉ λμμ ν리λμΌμ΄νΈ ν¨μμ λ΄μ μ λ¬λ°μ μ μλ€.
ν리λμΌμ΄νΈλ μ°Έ λλ κ±°μ§μ λ°ννλ ν¨μμ΄λ€. λ¨ νλμ ν¨μλ§ κ°μ§λ ν리λμΌμ΄νΈ μΈν°νμ΄μ€λ₯Ό μ μνκ³ , ν΄λΉ ν리λμΌμ΄νΈλ₯Ό μν©μ λ§κ² ꡬννμ¬ μ¬μ©νλ€.
μλμ κ°μ΄ μ§μ μ μν΄μ μ¬μ©ν μ μλ€.
public interface ApplePredicate{
boolean test(Apple apple);
}
λλ java.util.function
μλ Predicate<T>
μΈν°νμ΄μ€λ₯Ό ꡬνν΄ μ¬μ©ν΄λ λλ€. boolean
μΌλ‘ μ°Έ/κ±°μ§μ λ°ννλ test()
ν¨μ, ν리λμΌμ΄νΈκ° AND/OR/NOT μ°μ°μ μννλ ν¨μ λ±μ΄ μ μλμ΄ μλ€.
ν리λμΌμ΄νΈ μΈν°νμ΄μ€λ μλμ κ°μ΄ ꡬνν΄μ μ¬μ©νλ€.
public class AppleHeavyWeightPredicate implements ApplePredicate{
public boolean test(Apple apple){
return apple.getWeight() > 150;
}
}
public class AppleGreenColorPredicate implements ApplePredicate{
public boolean test(Apple apple){
return GREEN.equals(apple.getColor());
}
}
if-else
λμ 쑰건μμ ν리λμΌμ΄νΈ ν¨μλ‘ μ λ¬λ°μ μννλ μ½λμ΄λ€. λ³΄λ€ μ μ°νκ² λμνλλ°λ€ κ°λ
μ±λ μ’λ€.
public static List<Apple> filterApples(List<Apple> inventory, ApplePredicate p){
List<Apple> result = new ArrayList<>();
for(Apple apple : inventory){
if(p.test(apple)){
result.add(apple);
}
}
return result;
}
λ€λ§, λ©μλλ₯Ό ν΄λμ€λ‘ κ°μΈ μ λ¬νλ―λ‘ μ½λκ° λΆνμνκ² μ€λ³΅λλ€.
voidλ₯Ό λ°ννκ±°λ Stringμ λ°ννλ ν리λμΌμ΄νΈλ₯Ό ꡬνν μλ μλ€. μ΄λ₯Όν λ©΄ 컬λ μ κ°μ²΄λ₯Ό κ²μ¬ν΄ μΆλ ₯ν λ©μμ§λ₯Ό λ€λ₯΄κ² μ€μ ν μ μκ² λ€.
public interface AppleFormatter{
String accept(Apple a);
}
Javaμ Predicate<T>
λ boolean
λ§μ λ°ννλ―λ‘, λ€μν νμ©μ μν΄μλ μ§μ μ μν΄μ μ¬μ©ν΄μΌ νκ² λ€. λ€λ§, μΈν°νμ΄μ€μ λ κ° μ΄μ ν¨μλ₯Ό μ μνλ©΄ @functionalInterface
μ΄λ
Έν
μ΄μ
μ λ¬μμ λ μλ¬κ° λ¬λ€. ν리λμΌμ΄νΈ μΈν°νμ΄μ€λ λ¨ νλμ ν¨μλ§μ κ°μ ΈμΌ νλ€.
2.1.μ AppleHeavyWeightPredicate
μ AppleGreenColorPredicate
λ ν¨μλ₯Ό μ λ¬νκΈ°λ§ νλ€. μλμ κ°μ΄ μ΅λͺ
ν΄λμ€λ‘ ν리λμΌμ΄νΈλ₯Ό μμ±ν΄ μ λ¬νλ©΄ λ³΄λ€ κ°κ²°νκ² ννν μ μλ€.
List<Apple> redApples = filterApples(inventory, new ApplePredicate(){
public boolean test(Apple a){
return RED.equals(a.getColor());
}
});
κ·Έλ¬λ λ€μκ³Ό κ°μ΄ κ°μ²΄ μ€μ½νκ° λ³΅μ‘ν΄μ§λ λ¬Έμ κ° μλ€. λ€μ μ½λμ κ²°κ³Όκ°μ 무μμΌκΉ?
public class MeaningOfThis{
public final int value = 4;
public void doIt(){
int value = 6;
Runnable r = new Runnable(){
public final int value = 5;
public void run(){
int value = 10;
System.out.println(this.value);
}
};
r.run();
}
public static void main(String...args){
MeaningOfThis m = new MeaningOfThis();
m.doIt();
}
}
μ λ΅
5μΈμ€ν΄μ€ λ©μλ λλ μμ±μ μμμ this ν€μλλ‘ νμ¬ κ°μ²΄λ₯Ό μ°Έμ‘°ν μ μλ€. μ§μ λ©€λ² λλ λ§€κ°λ³μλ‘ μμ λ©€λ²κ° κ°λ €μ§κΈ° λλ¬Έμ΄λ€. this ν€μλλ₯Ό μ¬μ©ν΄ νμ¬ κ°μ²΄μ λͺ¨λ λ©€λ²λ₯Ό μ°Έμ‘°ν μ μλ€.
λν μλμ κ°μ΄ μμ±μ μ½λ μ€λ³΅μ νΌνκΈ° μν΄ μ¬μ©ν μλ μλ€. μ΄ κ²½μ° thisλ μμ±μμ 첫 λ²μ§Έ νμμ νΈμΆλμ΄μΌ νλ€.
public class Coordinate{
public int x;
public int y;
public Coordinate(int x){
this(x, 0);
}
public Coordinate(int y){
this(0, y);
}
public Coordinate(int x, int y){
this.x = x;
this.y = y;
}
}
μ΅λͺ ν΄λμ€λ₯Ό μλμ κ°μ΄ λ κ°κ²°νκ² ννν μ μλ€.
List<Apple> result = filterApples(inventory, (Apple apple) -> RED.equals(apple.getColor()));
μ΄λ¬ν ννμμ λλ€λΌκ³ νλ€. μ’ λ₯λ μλμ κ°μ΄ λ κ°μ§κ° μλλ°, λλ€λ μΈμ, νμ΄ν, λͺΈμ²΄λ‘ ꡬμ±λμ΄ μλ€. ꡬ체μ μΈ μ¬μ©λ²μ 3μ₯μμ λ€λ£¬λ€.
-
(μΈμ) -> expression
: κ΄νΈ μ μΈμλ₯Ό μ λ¬λ°μ νμ΄ν λ€μ ννμμ μννκ³ κ·Έ κ²°κ³Όκ°μ λ°ννλ€λ μλ―Έμ΄λ€. ννμμSystem.out.println
κ³Ό κ°μ΄void
λ₯Ό λ°ννλ κ²½μ°λ μλ€. -
(μΈμ) -> { statements; }
: κ΄νΈ μ μΈμλ₯Ό μ λ¬λ°μ νμ΄ν λ€ μ€κ΄νΈ μμ λ¬Έμ₯μ μννλ€λ μλ―Έμ΄λ€. λ°νκ°μ΄ μλ€λ©΄return
μ μ¬μ©ν΄ λͺ μμ μΌλ‘ νμν΄μ£Όμ΄μΌ νλ€.
ννμ(expression): μμ μ²λ¦¬νλ©΄ 'νλμ κ°'μ λ°ν. μ΄λ₯Όν λ©΄
int variable = 0
κ³Ό κ°μ ν λΉμμ μ±κ³΅μ intλ₯Ό λ°ννλ ννμμ΄λ©°,System.out.println(variable)
μ Voidλ₯Ό λ°ννλ ννμμ΄λ€.λ¬Έμ₯(statements): ν λΉμ, λ³μ μ¦κ°(
++
--
), λ©μλ νΈμΆ, κ°μ²΄ μμ±, μ μ΄λ¬Έ λ± μνν μμ μ ν λ¨μμ΄λ€. μΌλΆ λ¬Έμ₯μ;
μμ΄ ννμμΌλ‘ μ¬μ©ν μλ μλ€.
μ λ€λ¦μ μ¬μ©ν΄ 리μ€νΈ μμ νμμ μΆμνν κ²½μ°μ΄λ€.
public static <T> List<T> filter(List<T> list, Predicate<T> p){
List<T> result = new ArrayList<>();
for(T e : list){
if(p.test(e)){
result.add(e);
}
}
return result;
}
List<Apple> redApples = filter(inventory, (Apple apple)-> RED.equals(apple.getColor()));
List<Integer> evenNumbers = filter(numbers, (Integer i) -> i%2==0);
- ν리λμΌμ΄νΈ μΈν°νμ΄μ€λ₯Ό ꡬνν κ°μ²΄λ₯Ό μ λ¬
- μ΅λͺ ν΄λμ€λ‘ κ°μΈ ν¨μλ₯Ό μ λ¬
- λλ€μμ μ λ¬
- μ λ€λ¦μ μ¬μ©
κ²°λ‘ , 'λμ'μ μΆμνν΄ λ§€κ°μΈμλ‘ 'μ λ¬' λ°μ