반응형

reduce

    Java8 스트림(stream) 연산

    리덕션 연산 Stream의 reduce 메소드는 2개의 인자를 받아 인자와 같은 값을 리턴하는 (T,T) -> T 메소드이다. 첫 번째 인자는 누적되는 값이 되고, str2는 foreach처럼 순서대로 요소가 들어오는 방식이다. public static void main(String args[]) { List list = Arrays.asList(new String[] {"a", "b", "c"}); Stream stream = list.stream(); Optional opt = stream.reduce((str1, str2) -> str1 + str2); opt.ifPresent(System.out::println); } =>> 리턴 값 "abc" 또 다른 형태의 reduce 메소드 위에서 소개한 r..

반응형