본문 바로가기

자바8 스트림3

Java - Stream 최종 연산자(reduce) reduce - 스트림의 요소를 하나씩 줄여가며 누적연산 수행 함수 인터페이스 Optional reduce(BinaryOperator accumulator) T reduce(T identity, BinaryOperator accumulator) U reduce(U identity, BiFunction accumulator, BinaryOperator combiner) 매개변수 identity - 초기값 accumulator - 이전 연산결과와 스트림의 요소에 수행할 연산 combiner - 병렬처리된 결과를 합치는데 사용할 연산(병렬 스트림) // OptionalInt reduce(IntBinaryOperator accumulator)// 연산된 결과값과 다음 값을 계속 비교한다. OptionalInt .. 2017. 3. 9.
Java - Stream 최종 연산자 메서드설명반복하면서 작업수행forEach()병렬스트림 시 순서가 보장되지 않음forEachOrdered()순서대로 실행된다.배열로 변환toArray()Object 타입의 배열로 리턴toArray(IntFunction generator)A타입의 배열로 리턴조건 검사boolean allMatch(Predicate predicate)모든 요소가 조건을 만족시키면 trueboolean anyMatch(Predicate predicate)한 요소라도 조건을 만족시키면 trueboolean noneMatch(Predicate predicate)모든 요소가 조건을 만족시키지 않으면 true조건에 일치하는 요소 찾기Optional findFirst()첫 번째 요소를 반환. 순차 스트림에 사용Optional findAn.. 2017. 3. 8.
Java - 스트림(Stream) 생성 빈 스트림 생성 Stream em = Stream.empty(); 컬렉션으로 부터 스트림 생성 List list = Arrays.asList(1,2,3); Stream is= list.stream(); 배열로부터 스트림 생성 Stream ss= Stream.of("1", "2", "3"); 특정 범위 스트림 생성 IntStream is = IntStream.range(1, 3); // 1,2 IntStream is = IntStream.rangeClosed(1, 3); // 1,2,3 난수를 갖는 스트림 IntStream it = new Random().ints(); it.limit(3).forEach(System.out::println); it = new Random().ints(3); it.forEa.. 2017. 3. 4.