Java - Add value to List in constructor declaration

Description

Add value to List in constructor declaration

Demo

import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

public class Main {
  public static void main(String[] args) {
    List<String> lst = new CopyOnWriteArrayList<String>() {
      {// w  ww .  j a  v a  2 s  .c o  m
        add("a");
        add("b");
        add("c");
      }
    };
    Iterator<String> iterator = lst.iterator();
    while (iterator.hasNext()) {
      String string = (String) iterator.next();
      System.out.println(string);
      if (string.equals("b")) {
        lst.add("d");
      }
    }
  }
}

Related Topic