Convenient List Generation with

Let's assume you need a List of classes or records containing an index / number for test purposes.

The following snippet generates five ints, instantiates a record instance with the generated number and finally converts the stream into a List<Developer>:

import org.junit.jupiter.api.Test;

public class StreamGenerationTest {

    public void listGeneration() {

        record Developer(int id) {
        var developerList = IntStream.range(0, 5)
        assertEquals(5, developerList.size());


The output: [Developer[id=0], Developer[id=1], Developer[id=2], Developer[id=3], Developer[id=4]]

Big thanks to @ProjectPolly for the IntStream hint.


Post a Comment:
  • HTML Syntax: NOT allowed
Online Workshops
...the last 150 posts
...the last 10 comments