JCF : Iterator & Iterable

Jamangstangs·2022년 3월 13일
0

Roadmap

목록 보기
6/8
post-thumbnail

Iterator & Iterable

Interface Enumeration < E >

  • element의 집합체를 생산하는 interface이다. nextElement method는 element의 연속물에서 연속적인 element를 반환한다.
  • Enumeration를 통해 구현해야하는 메소드는 다음과 같다 .
    1. hasMoreElements() : 현재 커서 이후에 요소들이 있는지 확인한다. Boolean반환
    2. hasElement() : 커서를 다음 요소로 이동시키고, 현재 커서에 있는 요소를 꺼내 반환한다.
  • Enumeration의 역할은 JCF에 가면 Iterator가 그 역할을 대신한다.

출처 : https://docs.oracle.com/javase/8/docs/api/java/util/Enumeration.html

Interface Iterator< E >

  • E : iterator에 의해 반환되는 element의 타입
  • Collection에 저장된 요소를 읽어오는 행위를 표준화한 인터페이스이다.
  • Iterator를 통해 구현해야하는 메소드는 다음과 같다.
    1. hasNext() : 다음 element가 있으면 true를 반환한다.
    2. next() : 다음 element를 반환한다.
    3. remove() : 해당 collection에서 iterator에 의해 마지막으로 반환된 element를 제거한다.
  • JCF에서 Enumeratino의 지위를 가지며, Enumeration과 다른점은 2가지가 있다.
    1. collection에서 원소를 지울 수 있다.
    2. method가 더 많다.

출처 : https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html

Interface Iterable < T >

  • T : iterator에 의해 반환되는 element의 타입이다.

  • 하위 클래스에서 Iterator() 생성을 강제한다. -> 따라서, JCF에서 Iterator가 Enumeration의 역할을 대신할 수 있다.

  • Iterable이 구현해야하는 메소드는 다음과 같다.

    1. iterator() : 타입 T의 element의 Iterator를 반환한다.
  • Iterable 인터페이스의 subInterface는 아래와 같다.

    1. Collection< E >
    2. List< E >
    3. Set< E >

    주요 인터페이스는 위와 같다.

출처 : https://docs.oracle.com/javase/8/docs/api/java/lang/Iterable.html

profile
자망스탕스

0개의 댓글