2014년 3월 release
forEach() method in Iterable interface
default and static methods in Interfaces
default method와 static method의 차이
default method는 라이브러리의 인터페이스에 새 기능을 추가하고 해당 인터페이스의 이전 버전용으로 작성된 코드와 바이너리 호환성을 보장.
static method는 개체가 아니라 정의된 클래스와 연결된 메서드. 클래스의 모든 인스턴스는 static method를 공유.
인터페이스의 static method는 인터페이스 클래스의 일부이며 이를 구현하거나 재정의할 수 없는 반면 class는 default method를 재정의할 수 있다.
public interface DefaultStaticExampleInterface {
default void show() {
System.out.println("In Java 8- default method - DefaultStaticExampleInterface");
}
static void display() {
System.out.println("In DefaultStaticExampleInterface I");
}
}
public class DefaultStaticExampleClass implements DefaultStaticExampleInterface {
}
public class Main {
static void main(String args[]) {
// Call interface static method on Interface
DefaultStaticExampleInterface.display();
DefaultStaticExampleClass defaultStaticExampleClass = new DefaultStaticExampleClass();
// Call default method on Class
defaultStaticExampleClass.show();
}
}
Functional Interfaces and Lambda Expressions
하나의 추상 메소드가 있는 인터페이스는 기능 인터페이스가 되고 단일 추상 메소드 run()이 있는 java.lang.Runnable은 기능적 인터페이스의 좋은 예이다.
Runnable r = new Runnable(){
@Override
public void run() {
System.out.println("My Runnable");
}};
다음과 같이 구현
Runnable r1 = () -> {
System.out.println("My Runnable");
};
Java Stream API for Bulk Data Operations on Collections
큰 데이터를 처리하기에 유용.
package com.journaldev.java8.stream;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
public class StreamExample {
public static void main(String[] args) {
List<Integer> myList = new ArrayList<>();
for(int i=0; i<100; i++) myList.add(i);
//sequential stream
Stream<Integer> sequentialStream = myList.stream();
//parallel stream
Stream<Integer> parallelStream = myList.parallelStream();
//using lambda with Stream API, filter example
Stream<Integer> highNums = parallelStream.filter(p -> p > 90);
//using lambda in forEach
highNums.forEach(p -> System.out.println("High Nums parallel="+p));
Stream<Integer> highNumsSeq = sequentialStream.filter(p -> p > 90);
highNumsSeq.forEach(p -> System.out.println("High Nums sequential="+p));
}
}
아웃풋
High Nums parallel=91
High Nums parallel=96
High Nums parallel=93
High Nums parallel=98
High Nums parallel=94
High Nums parallel=95
High Nums parallel=97
High Nums parallel=92
High Nums parallel=99
High Nums sequential=91
High Nums sequential=92
High Nums sequential=93
High Nums sequential=94
High Nums sequential=95
High Nums sequential=96
High Nums sequential=97
High Nums sequential=98
High Nums sequential=99
Java Time API
Date, Time, Time Zones를 다루기 유용하다.
Collection API improvements
- Iterator forEachRemaining(Consumer action) 모든 요소가 처리되거나 작업이 예외를 throw할 때까지 나머지 각 요소에 대해 지정된 작업을 수행하는 기본 메서드
- Collection removeIf(Predicate filter) 주어진 술어를 만족하는 이 컬렉션의 모든 요소를 제거하는 기본 방법.
- Collection spliterator() 요소를 순차적으로 또는 병렬로 순회하는데 사용할 수 있는 Spliterator 인스턴스를 반환하는 메서드
- Map replaceAll(), compute(), merge() 메서드
- HashMap 키 충돌의 개선
Concurrency API improvements
- ConcurrentHashMap compute(), forEach(), forEachEntry(), forEachKey(), forEachValue(), merge(), reduce() and search() 메서드
- CompletableFuture 명시적으로 완료될 수 있다.
- Executors newWorkStealingPool() 사용 가능한 모든 프로세서를 대상 병렬 처리 수준으로 사용하여 work-stealing pool을 만드는 방법.
Java IO improvements
- Files.list(Path dir) 느리게 채워진 Stream을 반환.
- Files.lines(Path path) Stream으로 파일의 모든 라인을 읽는다.
- Files.find() 주어진 시작 파일에 파일을 검색하여 느리게 채워진 Stream을 반환.
- BufferedReaders.lines() Stream을 반환하는 요소는 이 BufferedReader에서 읽은 행의 요소.
출처
https://www.journaldev.com/2389/java-8-features-with-examples https://www.tutorialspoint.com/difference-between-default-and-static-interface-method-in-java-8