# ๐Ÿ’œ 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


๋ชจ๋˜ ์ž๋ฐ” ์ธ ์•ก์…˜ (ํ•œ๋น›๋ฏธ๋””์–ด)

Last Updated: 3/8/2024, 5:46:31 AM