Java 8을 구성하는 핵심 사항 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);
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;
}
}