I want to create an ArrayList of objects that have an id and an empty list using streams. I tried different ways but none seams to work. If someone can give me some hits will help me a lot. Here is the code that I want to convert to Java 8:
this.registers = new ArrayList<Supplier>();
for (int i = 0; i < this.numberOfSuppliers; i++) {
Supplier supplier = new Supplier();
supplier.setSupplierNumber(i);
supplier.setMaterials(new ArrayList<Warehouse>());
this.registers.add(supplier);
}
Thank you in advance.
forloop is good as it is. You will gain nothing by converting to Streams, not even clarity or simplicity of code. In my opinion, it'll actually be less clean/simple (see answer).