Java

[Effective Java] ์•„์ดํ…œ 44. ํ‘œ์ค€ ํ•จ์ˆ˜ํ˜• ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋ผ.

quedevel 2023. 3. 22. 11:45
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๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ ์™€ ๋น„์Šทํ•˜๋‹ค.

  1. ํ•ด๋‹น ํด๋ž˜์Šค์˜ ์ฝ”๋“œ๋‚˜ ์„ค๋ช… ๋ฌธ์„œ๋ฅผ ์ฝ์„ ์ด์—๊ฒŒ ๊ทธ ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ๋žŒ๋‹ค์šฉ์œผ๋กœ ์„ค๊ณ„๋œ ๊ฒƒ์ž„์„ ์•Œ๋ ค์ค€๋‹ค.
  2. ํ•ด๋‹น ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ์ถ”์ƒ ๋ฉ”์„œ๋“œ๋ฅผ ์˜ค์ง ํ•˜๋‚˜๋งŒ ๊ฐ€์ง€๊ณ  ์žˆ์–ด์•ผ ์ปดํŒŒ์ผ๋˜๊ฒŒ ํ•ด์ค€๋‹ค.
  3. ๊ทธ ๊ฒฐ๊ณผ ์œ ์ง€๋ณด์ˆ˜ ๊ณผ์ •์—์„œ ๋ˆ„๊ตฐ๊ฐ€ ์‹ค์ˆ˜๋กœ ๋ฉ”์„œ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜์ง€ ๋ชปํ•˜๊ฒŒ ๋ง‰์•„์ค€๋‹ค.

์ฐธ๊ณ  ์ž๋ฃŒ

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
๋ฐ˜์‘ํ˜•