Skip to content

[Chapter 01] 스트림의 내부 반복 #2

@shkisme

Description

@shkisme

아래는 P. 55에 나와있는 스트림을 활용한 코드입니다.

List<Transaction> transactions = new ArrayList<>();
Map<Currency, List<Transaction>> transactionsByCurrencies = transactions.stream()
                .filter((Transaction t) -> t.getPrice() > 1000)
                .collect(groupingBy(Transaction::getCurrency));

Q. 스트림은 컬렉션과 달리, 외부 반복이 아닌 라이브러리에서 내부 반복 과정을 통해서 연산된다고 합니다. 하지만 코드를 보면 filter 연산을 하고, 그 다음 collect 연산을 하고 있는 것처럼 보입니다. 이는 마치, for 문으로 filter 연산을 하고, collect 연산을 하는 과정과 비슷해 보이는데, 책에서 나와 있는 외부반복과의 차이가 무엇인지 잘 모르겠습니다.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions