본문 바로가기

JAVA

(19)
스트림12 스트림의 그룹화 분할 스트림의 그룹화 분활 Collectors partitioningBy()는 스트림을 2분활 한다. Collector partitioningBy(Predicate predicate) Collector partitioningBy(Predicate predicate , Collector downStream) groupingBy() 는 스트립을 n분할 한다. Collector groupingBy (Function classifier) Collector groupingBy (Function classifier , Collector downStream) Collector groupingBy (Function classifier , Supplier mapFactory , Collector downStream) Part..
스트림11 스트림의 collector로 reducing 그룹별 리듀싱 스트림의 reducing Collector reducing (BinaryOperator op) Collector reducing (T Identity, BinaryOperator op) Collecotr reducing (U Identity , Function mapper , BinaryOperator op) //map + reduce IntStream intStream = new Random().ints(1,46).distinct().init(6); OptionalInt max = intStream.reduce(integer::max); //전체 리듀싱 OptionalInt max = intStream.boxed().collect(reducing(Integer::max)) // 그룹별 Long sum ..
스트림10 최종연산 collect 와 colltors 최종연산 collect 와 colltors collect는 인터페이스 collect()는 collectors와 매개변수로 하는 스트림의 최종여산 Object Collect (collect Collector) //collector를 구현한 클래스의 객체를 매개변수로 사용 Object Collect(Supplier supplier , BiConcumer , accmulator , BiConsumer Combine) 잘 쓰지않음. reduce와 collect의 차이 , 사실 거의 같다. reduce는 stream 전체요소에 대한 리듀스 collect는 stream 전체요소를 그룹화로 나누어 리듀싱 Colletor는 수집(collect)에 필요한 메서드를 정의해 놓은 인터페이스 public interface C..
스트림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