I am trying to initialize a random 5 elements (Object) and I am trying to use the Stream.iterate function , but something is wrong. The first element (object) is always null .
Stream.iterate(new Student(), s -> {
s.setId(new Random().nextInt(5));
s.setUsername(UUID.randomUUID().toString());
s.setPassword(UUID.randomUUID().toString());
return s;
}).limit(5)
.forEach(System.out::println);
Output:
Student{id=null, username=null, password=null}
Student{id=0, username=7d7403e0-89f0-4033-a874-31e0aae1d7c6, password=590baa12-a9f7-4fef-8a47-4bcb7f2174d1}
Student{id=3, username=d75bff67-a1f7-4969-9418-93f3e0eb1cd7, password=9e48d614-a15e-4f2a-9e87-63ed3a81f410}
Student{id=3, username=47943516-e8bd-4ffe-bc47-4e251104994a, password=f2d4d02d-2e37-4346-a51d-b83f40044c68}
Student{id=2, username=1a1e5f9e-7fac-439f-b5f2-2931884e0772, password=9df36248-5e1e-4b28-b1c0-020123dd26b8}
What is wrong here?
source
share