Java JDK 7과 8의 정리
JDK 7
Type Inference
JDK 7 이전
1 | Map<String, List<String>> employeeRecords = new HashMap<String, List<String>>(); |
JDK 7 이후
1 | Map<String, List<String>> employeeRecords = new HashMap<>(); |
String in Switch
1 | switch (day) { |
Automatic Resource Management
JDK 7 이전
1 | FileInputStream fin = null; |
JDK 7 이후
1 | try (FileInputStream fin = new FileInputStream("info.xml"); |
Underscore in Numeric literal
1 | int billion = 1_000_000_000; // 10^9 |
JDK 8
Lambda expressions
람다 표현식은 Anonymous Function라고 할 수 있다
람다를 이용하여 코드를 간결하게 할 수 있다
1 | // Before |
Method Reference
특정 람다 표현식을 축약한 것으로 볼 수 있다
메서드 정의를 활용하여 람다처럼 사용 가능하다
1 | // Before |
Stream
간결하게 컬렉션의 데이터를 처리하는 기능
1 | // Before |
Default Method
인터페이스의 구현체를 인터페이스 자체에서 기본으로 제공 가능하다
구현 클래스에서 인터페이스를 구현하지 않아도 된다
1 | public interface Sized { |
Optional
값을 Optional
값이 존재한다면 Optional 클래스는 값을 감싼다
값이 없다면 Optional.empty메서드로 Optional을 리턴한다
New date / time APIs
- Joda-Time의 많은 기능을 java.time 패키지로 추가했다
- LocalDate, LocalTime, Instant, Duration, Period …