2

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.

2
  • Unless this is just an exercise in Streams, don't convert to Streams. The for loop 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). Commented Mar 4, 2017 at 23:01
  • Yes, it's just an exercise. But thank you for the advice :) Commented Mar 4, 2017 at 23:10

2 Answers 2

3

You might consider adding a constructor to Supplier that takes the id as a parameter, and initializes the list of materials, so you don't have to expose a setter. Then the solution using either the loop or streams becomes much simpler.

public void buildSuppliersWithLoop()
{
    ArrayList<Supplier> registers = new ArrayList<>()
    int numberOfSuppliers = 100;
    for (int i = 0; i < numberOfSuppliers; i++)
    {
        registers.add(new Supplier(i));
    }
}

public void buildSuppliersWithStream()
{
    int numberOfSuppliers = 100;
    List<Supplier> registers = IntStream.range(0, numberOfSuppliers)
            .mapToObj(Supplier::new)
            .collect(Collectors.toList());
}

public class Supplier
{
    private int number;
    private List<Warehouse> materials;

    public Supplier(int number)
    {
        this.number = number;
        this.materials = new ArrayList<>();
    }
}

public class Warehouse
{
}
Sign up to request clarification or add additional context in comments.

Comments

2

It is not my preferred approach (I think the for loop is fine here), but in streams you can do this:

this.registers = IntStream.range(0, this.numberOfSuppliers)
         .map(i -> {
                       Supplier supplier = new Supplier();
                       supplier.setSupplierNumber(i);
                       supplier.setMaterials(new ArrayList<Warehouse>());
                       return supplier;
         })
         .collect(Collectors.toList());

3 Comments

Caveat: That creates a List<Supplier>, not an ArrayList<Supplier>. If an ArrayList is required, replace Collectors.toList() with Collectors.toCollection(ArrayList<Supplier>::new).
I believe you need to use mapToObj here, instead of map, as map returns an IntStream, not a Stream.
You can also extract the creation of the Supplier object to a different method to improve readability.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.