다음이 Iterable이라고 가정해 보겠습니다. −
Iterable<String> i = Arrays.asList("K", "L", "M", "N", "O", "P"); 이제 컬렉션을 만드세요 -
Stream<String> s = convertIterable(i);
위에 사용자 정의 메소드 convertIterable() 이 있습니다. 변환을 위해. 다음은 방법입니다 -
public static <T> Stream<T> convertIterable(Iterable<T> iterable) {
return StreamSupport.stream(iterable.spliterator(), false);
} 예시
다음은 Iterable을 Java에서 Stream으로 변환하는 프로그램입니다 -
import java.util.*;
import java.util.stream.*;
public class Demo {
public static <T> Stream<T> convertIterable(Iterable<T> iterable) {
return StreamSupport.stream(iterable.spliterator(), false);
}
public static void main(String[] args) {
Iterable<String> i = Arrays.asList("K", "L", "M", "N", "O", "P");
Stream<String> s = convertIterable(i);
System.out.println("Iterable to Stream: "+s.collect(Collectors.toList()));
}
} 출력
Iterable to Stream: [K, L, M, N, O, P]