티스토리 뷰

Programming/JAVA

Java 8 람다 맛보기

Allg 2018. 8. 6. 23:03

Java 8을 구성하는 핵심 사항 2가지

  1. 간결한 코드
  2. 멀티코어 프로세서의 간단한 활용
  • 스트림 API
  • 메서드에 코드를 전달하는 기법(메서드 래퍼런스, 람다)
  • 인터페이스의 디폴트 메서드

실습!!

Apple

public class Apple {
    private int weight = 0;
    private String color = "";

    public Apple(int weight, String color){
        this.weight = weight;
        this.color = color;
    }

    public Integer getWeight() {
        return weight;
    }

    public void setWeight(Integer weight) {
        this.weight = weight;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    @Override
    public String toString() {
        return "Apple [weight=" + weight + ", color=" + color + "]";
    }
}

Ex1 : Java 8 이전 방법

public class Ex1 {

    public static void main(String ... args) {
        List<Apple> inventory = Arrays.asList(new Apple(80,"green"), new Apple(155, "green"), new Apple(120, "red"));
        System.out.println("inventory : "  + inventory);

        //모든 녹색사과 선택해서 리스트 반환
        List<Apple> greenApples = filterGreenApples(inventory);
        System.out.println("greenApples : " + greenApples);
    }
    public static List<Apple> filterGreenApples(List<Apple> inventory) {
        List<Apple> list = new ArrayList<>();
        for(Apple apple : inventory) {
            if("green".equals(apple.getColor())) { list.add(apple); }
        }
        return list;
    }
}

Ex2 : 메서드 레퍼런스(Method Reference) :: 활용(메서드를 값으로 사용, 코드를 전달가능)

public class Ex2 {

    public static void main(String ... args) {
        List<Apple> inventory = Arrays.asList(new Apple(80,"green"), new Apple(155, "green"), new Apple(120, "red"));
        System.out.println("inventory : "  + inventory);

        //녹색사과
        List<Apple> greenApples = filterApples(inventory, Ex2::isGreenApple);
        System.out.println("greenApples : " + greenApples);

        //빨간사과
        List<Apple> redApples = filterApples(inventory, Ex2::isRedApple);
        System.out.println("redApples : " + redApples);
    }

    public static boolean isGreenApple(Apple apple) {
        return "green".equals(apple.getColor());
    }

    public static boolean isRedApple(Apple apple) {
        return "red".equals(apple.getColor());
    }

    //함수형 인터페이스(하나의 추상 메서드)
    @FunctionalInterface
    public interface Predicate<T> {
        boolean test(T t);
    }

    public static List<Apple> filterApples(List<Apple> inventory, Predicate<Apple> p) {
        List<Apple> list = new ArrayList<>();
        for(Apple apple: inventory) {
            if(p.test(apple)) { list.add(apple);}
        }
        return list;
    }
}

Ex3 - 람다

public class Ex3 {
    public static void main(String ... args) {
        List<Apple> inventory = Arrays.asList(new Apple(80,"green"), new Apple(155, "green"), new Apple(120, "red"));
        System.out.println("inventory : "  + inventory);

        //녹색사과
        List<Apple> greenApples = filterApples(inventory, (Apple a) -> "green".equals(a.getColor()));
        System.out.println("greenApples : " + greenApples);

        //빨간사과
        List<Apple> redApples = filterApples(inventory, (Apple a) -> "red".equals(a.getColor()));
        System.out.println("redApples : " + redApples);

        //100 이상인 사과
        List<Apple> heavyApples = filterApples(inventory, (Apple a) -> a.getWeight() >= 100);
        System.out.println("heavyApples : " + heavyApples);
    }

    public interface Predicate<T> {
        boolean test(T t);
    }

    public static List<Apple> filterApples(List<Apple> inventory, Predicate<Apple> p) {
        List<Apple> list = new ArrayList<>();
        for(Apple apple: inventory) {
            if(p.test(apple)) { list.add(apple);}
        }
        return list;
    }
}
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함