무지성 메모

■ char배열의 stream은 왜 안되는걸까?

배게 2022. 3. 9. 19:30
728x90

Arrays.stream( char[] )를 했는데 오류가 났음..

 

이게 Array.stream()안의 parameter type은

 

static DoubleStream	stream​(double[] array)	
Returns a sequential DoubleStream with the specified array as its source.
static DoubleStream	stream​(double[] array, int startInclusive, int endExclusive)	
Returns a sequential DoubleStream with the specified range of the specified array as its source.
static IntStream	stream​(int[] array)	
Returns a sequential IntStream with the specified array as its source.
static IntStream	stream​(int[] array, int startInclusive, int endExclusive)	
Returns a sequential IntStream with the specified range of the specified array as its source.
static LongStream	stream​(long[] array)	
Returns a sequential LongStream with the specified array as its source.
static LongStream	stream​(long[] array, int startInclusive, int endExclusive)	
Returns a sequential LongStream with the specified range of the specified array as its source.
static <T> Stream<T>	stream​(T[] array)	
Returns a sequential Stream with the specified array as its source.

 

이 정도인데 stream으로 다루는 데이터의 타입들 중

자주 쓰이는 실수나 숫자형은 아예 DoubleStream, IntStream, LongStream이 존재함

 

char는 자주 안쓰이고

CharStream이라는 클래스가 존재를 하지 않아서 Arrays.stream()안의 패러미터로 넣을 수 없는 것임..

 

또 자주 쓰이는 String형은 primitive가 아니라 Reference클래스기 때문에

Arrays.stream(T[] array)의 Stream형으로 반환함

 

char형은 원시타입이라 제네릭타입 받는 방식으로 받을 수도 없고,

애초에 받는 paremeter 범위 내에 없기 때문에 불가능..

Wrapper클래스로 바꿔주거나 잠시 int형으로 바꾸던가.. 아니면

내가 풀던 문제는 String에서 toCharArray()활용해 char[]로 쪼갠거니까

String.chars() 메소드를 사용, IntStream을 땡겨와서 그걸로 문제 해결해야함