๊ณต์๋ฌธ์ :
https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/stream/Stream.html
Stream (Java SE 11 & JDK 11 )
A sequence of elements supporting sequential and parallel aggregate operations. The following example illustrates an aggregate operation using Stream and IntStream: int sum = widgets.stream() .filter(w -> w.getColor() == RED) .mapToInt(w -> w.getWeight())
docs.oracle.com
Java์ stream()ํจ์๋
โ
Collection(ex. List, Set)์ ๋ฐ์์
Stream<T>๊ฐ์ฒด๋ฅผ ๋ฆฌํดํ๋ ํจ์์.
import java.util.Arrays;
import java.util.List;
public class StreamExample {
public static void main(String[] args) {
List<String> names = Arrays.asList("Alice", "Bob", "Angela", "Charlie");
names.stream() // ์คํธ๋ฆผ ์์
.filter(name -> name.startsWith("A")) // 'A'๋ก ์์ํ๋ ์ด๋ฆ๋ง ๊ณจ๋ผ
.map(String::toUpperCase) // ๋๋ฌธ์๋ก ๋ฐ๊ฟ
.forEach(System.out::println); // ํ๋์ฉ ์ถ๋ ฅ
}
}
>> ์ถ๋ ฅ ๊ฒฐ๊ณผ
ALICE
ANGELA
์งํผํฐ๊ฐ ์จ์ค ์์ ์ฝ๋.
โ
์ ์์ ์์๋ name๋ผ๋ ๋ฆฌ์คํธ์์ ๊ฐ ํ๋์ฉ ์จ๋ด์
ํํฐ๋ง, ๋งคํ, ์ ๋ ฌ ๋ฑ๋ฑํด์ for๋ฌธ์ฒ๋ผ ๋ฐ๋ณตํด์ ์ถ๋ ฅํจ.
โ
โ
โ
์์ฃผ ์ฐ๋ Stream๋ฉ์๋๋

์ฒจ์๋ ์ด Stream() ํจ์์ ์ญํ ์ด ์ดํด๊ฐ ์ ์๊ฐ์
โ
๊ฑ ๋์ถฉ for๋ฌธ์ด๋ ๋น์ทํ๊ฑด๊ฐ ํ๋๋ฐ
Collectionํ์ ๋ค์ ๋ ๊น๋ํ๊ฒ ์ฒ๋ฆฌํ ์ ์๋ค๋ ์ฅ์ ์ด ์์.
โ
for๋ฌธ๋ณด๋ค ๋น์ฐํ ๊ฐ๋ ์ฑ๋ ์ข๊ณ
์๋๋ ๋ด๊ฐ ์ ์ฉํ ์์
@GetMapping("/api/articles")
public ResponseEntity<List<ArticleResponseDto>> findAllArticles() {
List<ArticleResponseDto> articles = blogService.findAll()
.stream()
.map(ArticleResponseDto::new)
.toList();
return ResponseEntity.ok()
.body(articles);
}
// findAll()ํจ์๋ list<Article>์ return
public List<Article> findAll() {
return blogRepository.findAll();
}
controller์์ streamํจ์ ์ฌ์ฉ์ค์
โ
โ
์์๋๋ก ๋ณด๋ฉด,
1. blogService.findAll()ํจ์ ์จ์ List<Article>์ ๋ฆฌํด ๋ฐ์.
2. .stream() : stream()ํจ์ ์จ์ stream<Article>๋ก ๋ณํ
3. .map() : mapํจ์ ์จ์ Article์ new ArticleResponseDto๋ก ๋ณํ
= stream<ArticleResponseDto> ๊ฐ ์์ฑ๋จ
4. toList() : ๋ค์ ๋ฆฌ์คํธ๋ก ๋ณํ
= List<ArticleResponseDto>๊ฐ ์์ฌ
โ
---> List<Article>์ ๋ฐ์๋ค์, List<ArticleResponseDto>๋ก ๋ณํํ๋ ๊ณผ์
โ
Article์ ArticleResponseDto๋ก ๋ณํํ๋ ๊ณผ์ ์์
โ
๊ฐ์๊ธฐ ํ๋ค๋ณด๋๊น entity๋ dto์ ๋ํด์ ํท๊ฐ๋ฆฌ๋๊ฒ ์๊ฒจ์
๋ด๊ฐ ๊ธฐ์ตํ ๊ฒธ ์์ธํ ์ ์ด๋ณด์๋ฉด
โ
dto๋ ๋จ์ ๋ฐ์ดํฐ ์ด๋ฐ์ฉ๋์. (๋ฐ์ฐฌํต๊ฐ์ ๋๋)
--> ๋น์ฐํ dto์ ์๋ ํ๋๋ entity์ ์๋ ์ปฌ๋ผ์ผ๊ฒ์ด๊ณ ,
๊ทธ๊ฑธ ๋งคํํ๋๊ฑด
โ
.map(ArticleResponseDto::new)
์ด ๋ถ๋ถ์ธ๋ฐ, ์ด๊ฒ
@Getter
public class ArticleResponseDto {
private final String title;
private final String content;
public ArticleResponseDto(Article article) {
this.title = article.getTitle();
this.content = article.getContent();
}
}
์ฌ๊ธฐ ์๋ ์์ฑ์ ํธ์ถํด์
ArticleResponseDto ๊ฐ์ฒด ์์ฑ, ์ด๊ธฐํ ํ๊ณ ์๋๊ฑฐ์!!!
โ
Article article์ ๋ฐ์์ ์ด๊ธฐํ์ค
โ
.map(ArticleResponseDto::new)๊ฐ ์๊ฑธ ํด์ฃผ๊ณ ์๋๊ฒ..
โ
โ
โ
์ฐธ๊ณ ๋ก .map(ArticleResponseDto::new)๋ ๋๋ค์์ ๋๋ค.
๋ฉ์๋ ์ฐธ์กฐ(method refernce)๋ผ๊ณ ๋ถ๋ฅด๋ ๋๋ค์์ ํ ์ข ๋ฅ
.map(article -> new ArticleResponseDto(article))
โ์ด๊ฒ ์ผ๋ฐ ๋๋ค์
์ด๊ฑธ ์ค์ธ๊ฒ ์๋์ ๊ฐ์.
.map(ArticleResponseDto::new)
'JAVA' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [Java] StringBuffer๊ณผ BufferedWriter์ ์ฐจ์ด์ (2) | 2025.07.11 |
|---|---|
| [Java] ์๋ฐ ์ง๋ ฌํ, ์ญ์ง๋ ฌํ๊ฐ ๋ญ๊น?ObjectMapper ํด๋์ค (0) | 2025.05.04 |