Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[아이템 58] for-each를 사용할 수 없는 상황 세가지 #128

Closed
pci2676 opened this issue Jun 11, 2020 · 1 comment
Closed

[아이템 58] for-each를 사용할 수 없는 상황 세가지 #128

pci2676 opened this issue Jun 11, 2020 · 1 comment
Assignees
Labels
Effective Java 3 이펙티브 자바 3

Comments

@pci2676
Copy link
Member

pci2676 commented Jun 11, 2020

p.349 하지만 안타깝게도 for-each를 사용할 수 없는 상황이 세가지 존재한다.

  • 파괴적인 필터링
  • 변형
  • 병렬 반복

위 세가지 경우 예시 코드 있으면 좋을 것 같아

@pci2676 pci2676 added this to In progress in 이펙티브 자바 via automation Jun 11, 2020
@Gobukgol
Copy link
Contributor

  • 파괴적인 필터링
    image
    위 코드를 그대로 실행하면
    image
    이런 식으로 Exception이 발생하고 추가로 IDE에서도
    image
    이런식으로 removeIf를 사용하라고 유도하고 있어
    image
    image
    removeIf 메소드 내부적으로 iterator를 사용해서 코드에서 명시적으로 순회를 사용하지 않게 되는 것이고.

  • 변형
    image

image

  • 병렬 반복
    병렬 반복 의미가 스레드를 이용해서 동시에 처리하는 건가? 라고 생각했었는데 책 설명 밑에 코드 58-4 언급이나 스택오버플로우에도 비슷한 질문이 있어서 확인해봤는데 중첩 반복문 얘기를 하는 것 같아.

하나의 퍼블리셔에 하나의 클라이언트를 매칭하고 싶은 상황에서 for-each 문을 중첩으로 한다면
image
모든 조합의 경우의 수가 나오니까
반복자를 사용해서 하나씩 매칭해주는 거지
image

image

추가로 스택오버플로우 링크야
https://stackoverflow.com/questions/33208013/parallel-iteration-on-multiple-collections

이펙티브 자바 automation moved this from In progress to Done Jun 13, 2020
@pci2676 pci2676 added the Effective Java 3 이펙티브 자바 3 label Oct 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Effective Java 3 이펙티브 자바 3
Projects
Development

No branches or pull requests

3 participants