본문 바로가기

전체 글

(272)
스트림9 최종연산 reduce() 최종연산 reduce() 최종연산중에서 가장 중요 스트림의 요소를 하나씩 줄여가며 누적연산 수행 Optional reduce(BinaryOperator accumulator) T reduce(T identity , BinaryOperator accumulator) U reduce(T identity , BiFunction accumulator),BinaryOperator Combiner) identity = 초기값, accumulator = 이전 연산결과와 스트림의 요소에 수행할 연산 combiner = 병렬처리된 결과를 합치는데 사용할 연산(병렬스트림) ex) int reduce(int identity , IntBinaryOperator op) int Count = intStream.reduce(0,(..
스트림8 최종연산 forEach() 최종연산 forEach() 스트림의 모든 요소에 지정된 작업을 수행 , forEach(), forEachOrdered() void forEach(Consumer
Optional<T> 타입의 래퍼 클래스 - optional 래퍼클래스 = Integer 나 Long 등 public final class Optional{ private final T value; // T타입의 참조변수를 가지고 있다. } 어떤 타입이든가 전부 저장할수 있다. null도 저장가능 Optional의 필드 이유 1.null을 직접 다루는것은 위험하다. nullpointException이 발생한다. 그래서 간접적으로 null을 다루이 위함이다. 2.null체크작업을 안해도 된다. 3.result null 을 하던것을 optional 객체에 null을 넣어서 반환. optional 객체 생성하기 String str = "abc"; Optional optval = optional.of(str); Optional op..
스트림7 flatMap() 스트림의 스트림을 스트림으로 변환 flatMap() 스트림의 스트림을 스트림으로 변환 Stream strArrStream = stream.of(new String[] {"abc" , "def" ,"ghi"} , new String[] {"ABC" , "DEF" , "GHI"}) 스트림의 요소 하나하나가 배열인 경우 map을 이용하여 요소를 Stream 화 Stream strstrStrm = strArrStrm.map(Arrays::Stream); 이 경우 strArrStream 이 가진 배열을 각각 stream으로 만들어서 가지고 있게 된다. 문자열 배열을 합쳐 하나의 stream 으로 만들기 Stream strstrStream = strArrStream.flatMap(Arrays::stream); //Arrays.stream(T[]) ..
스트림6 peek() 스트림소모 없이 요소 조회 peek() 스트림소모 없이 요소 조회 Stream peek(consumer
스트림5 map() 스트림의 내용 변환 map() 스트림의 내용 변환 stream map(function
스트림4 중간연산 예제 스트링의 중간연산 예제 Stream skip (long n) //앞에서부터 n개 뛰어넘기 Stream limit (long maxsize) //maxsize 이후에 요소는 잘라냄 IntStream intStream = IntStream.rangeClosed(1,10) //12345678910 intStream.skip(3).limit(5).forEach(system.out.println("")) //45678 //.skip(3) 앞에서 3개 건너 뛰기 45678910 //.limit(5) 앞에서부터 5개 만 가져오기 45678 IntStream intStream = IntStream.of(1,2,2,3,3,3,4,5,5,6) intStream.distinct().forEach(system.out.prin..
스트림3 스트림의 최종연산 스트림의 최종연산 void forEach(consumer