Java 8 在ArrayList中添加时stream()与parallelStream()的比较

Java 8 在ArrayList中添加时stream()与parallelStream()的比较,java-8,parallel-processing,functional-programming,java-stream,Java 8,Parallel Processing,Functional Programming,Java Stream,我有这段代码 List<UserNotification> userNotifications = new ArrayList<UserNotification>(); teatreAlertNotifications .parallelStream() .forEach(can -> userNotifications.add(new UserNotification(can))); List userNotif

我有这段代码

List<UserNotification> userNotifications = new ArrayList<UserNotification>();

teatreAlertNotifications
            .parallelStream()
            .forEach(can -> userNotifications.add(new UserNotification(can)));
List userNotifications=new ArrayList();
教师须知
.parallelStream()
.forEach(can->userNotifications.add(newusernotification(can)));
但是由于ArrayList是不同步的,我认为这是一种不好的做法,我应该使用.stream()来代替它,或者只是:

List<UserNotification> userNotifications = teatreAlertNotifications
           .parallelStream()
           .map(UserNotification::new)
           .collect(Collectors.toList());
List userNotifications=teatreAlertNotifications
.parallelStream()
.map(UserNotification::new)
.collect(Collectors.toList());
这被称为不必要的副作用,在文档中通常不鼓励这样做


您可以保留原始代码,但使用同步数据结构(线程安全),但在这种情况下,无法保证元素的顺序

请注意,UserNotification需要argument@carlesxuriguera它将使用这个论点。。。我不明白你的意思,这相当于
can->newusernotification(can)