Java/Java 기타

JAVA Reducer로 소문자 열 대문자 변환하며 더하는 2가지 방법

kimc 2022. 5. 18. 23:12

```

JAVA Reducer로 소문자 열 대문자 변환하며 더하는 2가지 방법

```

이번 글을 통해 배워갈 내용

  1. JAVA Reducer로 소문자 열 대문자 변환하며 더하는 2가지 방법

 


reduce

reduce를 사용하면 연속된 원소들로부터 하나의 단일 결과를 생성할 수 있습니다.

 

 

아래와 같이 소문자 "hello"의 각 문자들을 받아서

문자열을 대문자로 만들면서 더해서

대문자 문자열인 HELLO를 출력할수도 있습니다.

String retStr = Arrays.asList("h", "e", "l", "l", "o")
        .stream()
        .reduce("", (partialStr, element) -> partialStr + element.toUpperCase());
System.out.print(retStr);

 

또는 맵과 섞어서 아래와 같이 더한다음

HELLO를 동일하게 출력할 수도 있습니다.

        String retStr = Arrays.asList("h", "e", "l", "l", "o").stream()
                .map(str->str.toUpperCase(Locale.ROOT)).reduce("", String::concat);
        System.out.print(retStr);

 

참조

https://docs.oracle.com/javase/tutorial/collections/streams/reduction.html

 

Reduction (The Java™ Tutorials > Collections > Aggregate Operations)

The Java Tutorials have been written for JDK 8. Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available. See Java Language Changes for a summary of updated

docs.oracle.com

 

읽어주셔서 감사합니다

 

무엇인가 얻어가셨기를 바라며

 

오늘도 즐거운 코딩 하시길 바랍니다 ~ :)

 


 

728x90