I had this piece of code
List<UserNotification> userNotifications = new ArrayList<UserNotification>();
teatreAlertNotifications
.parallelStream()
.forEach(can -> userNotifications.add(new UserNotification(can)));
But since ArrayList is unsynchronized I think it is bad practice and I should use .stream() instead
Or just:
List<UserNotification> userNotifications = teatreAlertNotifications
.parallelStream()
.map(UserNotification::new)
.collect(Collectors.toList());
This is called un-needed side effects, that are generally discouraged in the documentation.
You could keep your original code, but use a synchronized data structure (thread safe), but in this case the order of the elements is not guaranteed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With