# ๐ 01. ๋์ ํ๋ผ๋ฏธํฐํ ์ฝ๋ | Predicate | ์ต๋ช ํด๋์ค | lambda (Thread) | ExecutorService
๐คฅ JAVA 8 ์ ์์ฐ๊ณ , ๊ณ ์ ์ ์ธ ๋ฐฉ์๋ง ์จ์... ์ด๋ฒ์ ์คํฐ๋ ํด๋ณด๋๋ก..
์ด ์นดํ ๊ณ ๋ฆฌ๋ ๋ง์ด ๋ง๊ธฐ ๋ณด๋ค๋.. ์ฝ๋๋ก ๋ณด์ฌ์ฃผ๋๋ก ํ ๊ฑฐ๋ค. ๋ด๊ฐ ๋ค์ ๋ณด๊ณ ๊ธฐ์ตํ๊ธฐ ์ฌ์ด ์ฉ์ผ๋ก..
# ๐ ์ฌ๊ณผ ํด๋์ค
๋ฌด๊ฒ์ ์์ ์ ๋ณด๊ฐ ๋ด๊ธด ์ฌ๊ณผ ํด๋์ค ์์ฑ
package javaStudy.mda02;
/**
* ์ฌ๊ณผ
*/
public class Apple {
public enum Color {
RED,
GREEN
}
Color color;
Integer weight;
public Apple(Color color, int weight) {
this.color = color;
this.weight = weight;
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
public Integer getWeight() {
return weight;
}
public void setWeight(Integer weight) {
this.weight = weight;
}
@Override
public String toString() {
return "Apple{" +
"color=" + color +
", weight=" + weight +
'}';
}
}
# ๐ Predicate ์์ฑ
Predicate
์ฐธ/๊ฑฐ์ง์ ๋ฐํํ๋ ํจ์
# ์ธํฐํ์ด์ค
package javaStudy.mda02;
public interface ApplePredicate {
boolean test(Apple apple);
}
# ์ด๋ก์ ์ฌ๊ณผ๋ง ์ ํ
package javaStudy.mda02;
public class AppleGreenColorPredicate implements ApplePredicate{
@Override
public boolean test(Apple apple) {
return Apple.Color.GREEN.equals(apple.getColor());
}
}
# ๋ฌด๊ฑฐ์ด ์ฌ๊ณผ๋ง ์ ํ
package javaStudy.mda02;
public class AppleHeavyWeightPredicate implements ApplePredicate{
@Override
public boolean test(Apple apple) {
return apple.getWeight() > 150;
}
}
# ๐ Main Method
package javaStudy.mda02;
import java.util.*;
import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.toList;
public class MainMethod {
public static void main(String[] args) {
List<Apple> appleList = Arrays.asList(
new Apple(Apple.Color.RED, 180),
new Apple(Apple.Color.GREEN, 100),
new Apple(Apple.Color.GREEN, 350),
new Apple(Apple.Color.RED, 400)
);
/**
* ๋ฌด๊ฑฐ์ด ์ฌ๊ณผ๋ง
* [Apple{color=RED, weight=180}, Apple{color=GREEN, weight=350}, Apple{color=RED, weight=400}]
*/
filterApples(appleList, new AppleHeavyWeightPredicate());
/**
* ์ต๋ช
ํด๋์ค
* Apple{color=GREEN, weight=100}, Apple{color=GREEN, weight=350}]
*/
filterApples(appleList, new ApplePredicate() {
@Override
public boolean test(Apple apple) {
return Apple.Color.GREEN.equals(apple.getColor());
}
});
/**
* ๋๋ค์ (๋นจ๊ฐ ์ฌ๊ณผ๋ง)
* [Apple{color=RED, weight=180}, Apple{color=RED, weight=400}]
*/
filterApples(appleList, (Apple apple) -> Apple.Color.RED.equals(apple.getColor()));
/**
* compareTo (๊ฐ๋ฒผ์ด ์ฌ๊ณผ ์)
* [Apple{color=GREEN, weight=100}, Apple{color=RED, weight=180}, Apple{color=GREEN, weight=350}, Apple{color=RED, weight=400}]
*/
appleList.sort((Apple a1, Apple a2) -> a1.getWeight().compareTo(a2.getWeight()));
/**
* stream lambda (๋ฌด๊ฒ 150 ๋๋ ์ฌ๊ณผ๋ง ๋ฌด๊ฒ์์ผ๋ก ์ ๋ ฌ)
* [180, 350, 400]
*/
List<Integer> lambdaTest = appleList.stream().filter(apple -> apple.getWeight() > 150).sorted(Comparator.comparing(Apple::getWeight)).map(Apple::getWeight).collect(toList());
/**
* groupingBy (์์๋ณ ์ ๋ฆฌ)
* {GREEN=[Apple{color=GREEN, weight=100}, Apple{color=GREEN, weight=350}], RED=[Apple{color=RED, weight=180}, Apple{color=RED, weight=400}]}
*/
Map<Apple.Color, List<Apple>> groupingTest = appleList.stream().collect(groupingBy(Apple::getColor));
}
/**
* ์ฌ๊ณผ ํํฐ
*
* @param inventory
* @param p
* @return
*/
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;
}
}
# ๐ Main Method2
package javaStudy.mda02;
import java.util.Arrays;
import java.util.List;
public class MainMethod2 {
public static void main(String[] args) {
List<Apple> appleList = Arrays.asList(
new Apple(Apple.Color.RED, 180),
new Apple(Apple.Color.GREEN, 100),
new Apple(Apple.Color.GREEN, 350),
new Apple(Apple.Color.RED, 400)
);
/**
* ๋ฌด๊ฒ์ ์ ๋ ฌ (List sort ๋ฐ Comparator ํ์ฉ
* [Apple{color=GREEN, weight=100}, Apple{color=RED, weight=180}, Apple{color=GREEN, weight=350}, Apple{color=RED, weight=400}]
*/
appleList.sort((Apple a1, Apple a2) -> a1.getWeight().compareTo(a2.getWeight()));
/**
* Runnable lambda ์
*/
Thread t = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("Hello world");
}
});
/**
* Runnable lambda ํ
*/
Thread t_lambda = new Thread(() -> System.out.println("Hello World"));
}
}
# Reference
๋ชจ๋ ์๋ฐ ์ธ ์ก์ (ํ๋น๋ฏธ๋์ด)