Use an empty list iterator - Java Collection Framework

Java examples for Collection Framework:ListIterator

Description

Use an empty list iterator

Demo Code

import java.util.ArrayList;
import java.util.Collections;
import java.util.ListIterator;

public class Main {
  public static ListIterator<String> returnEmptyListIterator() {
    boolean someConditionMet = false;
    if (someConditionMet) {
      ArrayList<String> list = new ArrayList<>();
      // Add elements
      ListIterator<String> listIterator = list.listIterator();
      return listIterator;
    } else {/*from ww  w .  j av a2s . c o m*/
      return Collections.emptyListIterator();
    }
  }

  public static void main(String[] args) {
    ListIterator<String> list = returnEmptyListIterator();
    while (list.hasNext()) {
      String item = list.next();
      System.out.println(item);
    }
  }

}

Related Tutorials