java8

Stream API는 최종 처리 단계 에서 요소들이 특정 조건에 만족하는지 조사할 수 있도록 세가지 매칭 메소드를 제공한다. allMatch() 모든 요소들이 매개값으로 주어진 Predicate의 조건을 만족하는지 조사 anyMatch() 최소한 한 개의 요소가 매개값으로 주어진 조건을 만족하는지 조사 noneMatch() 모든 요소들이 매개값으로 주어진 조건을 만족하지 않는지 조사 public static void main(String[] args){ int[] intArr = {2, 4, 6}; boolean result = Arrays.stream(intArr) .allMatch(a -> a%2 == 0); System.out.println("2의 배수? " + result); result = Ar..
자바8에는 추상 메서드 하나짜리 인터페이스는 특별한 의미를 인정받아 특별한 대우를 받게 되었다. 함수형 인터페이스라 부르는 이 인터페이스들의 인스턴스를 람다식(lamda expression)을 사용해 만들 수 있게 된 것이다. Collections.sort(words, new Comparator(){ public int compare(String s1, String s2){ return Integer.compare(s1.length, s2.length()); } }); Collections.sort(words, (s1, s2) -> Integer.compare(s1.length(), s2.length()); 컴파일러가 문맥을 살펴 타입을 추론해준다. 타입을 명시해야 코드가 더 명확할 때만 제외하고는, 람..
Optional을 어떻게 하면 효율적으로 사용할 수 있을까? 전통적인 null check 코드를 살펴보자 NullPointException 방어패턴 중첩 null 체크 if(a != null){ B b = a.getMember(); if(b != null){ C c = b.getAddress()); if(c != null){ ... } } } return "incheon"; return 하기 if(a == null) return "incheon"; B b = a.getMember(); if(b == null) return "incheon"; C c = b.getAddress(); ... 어떻게 하면 null처리를 효과적으로 어떻게 할 수 있을까? Optional 자바8에는 Optional이라는 것이 생겼..
깡냉쓰
'java8' 태그의 글 목록