preface

ParallelStream parallelStream parallelStream parallelStream parallelStream parallelStream parallelStream parallelStream

@Test
public void testStream(a) {
    List<Integer> list = new ArrayList<>();
    for (int i = 0; i < 10000; i++) {
        list.add(i);
    }
    System.out.println(list.size());
    List<Integer> streamList = new ArrayList<>();
    list.parallelStream().forEach(streamList::add);
    System.out.println(streamList.size());
}
Copy the code

such

List<Integer> integers = new ArrayList<>();
for (int i = 0; i < 1000; i++ ) {
    integers.add(i);
}
List<String> strings = new ArrayList<>();
integers.parallelStream().forEach(i -> strings.add(i.toString()));
System.out.println(strings);
Copy the code

, of course, these are some of the wrong wording, they give the solution is to use some thread-safe Collections such as Collections. SynchronizedList or CopyOnWriteArrayList actually these are multithreaded operations Shared variables have no locking problems, It’s not the Parallelstream itself, so if we look at their code, they create new collections and then they use parallel streams to add elements to new collections, and the recommended way to do this mapping is to use the map function, In fact, through this code, you can see that they have only a half-understanding of lambda and stream. They just use it for the sake of use. They don’t understand it at all

// This is the original
@Test
public void testStream(a) {
    List<Integer> list = new ArrayList<>();
    for (int i = 0; i < 10000; i++) {
        list.add(i);
    }
    System.out.println(list.size());/ / print 10000
    List<Integer> streamList = new ArrayList<>();
    list.parallelStream().forEach(streamList::add);
    System.out.println(streamList.size());// Data may be lost or subscripts may be out of bounds
}
/ / modified
@Test
public void testStream(a) {
    List<Integer> list = new ArrayList<>();
    for (int i = 0; i < 10000; i++) {
        list.add(i);
    }
   System.out.println(list.size());/ / print 10000
   List<Integer> streamList = list.parallelStream()
   .map(Integer::new).collect(Collectors.toList());
   System.out.println(streamList.size());/ / print 10000
}
Copy the code

Second code

/ / the original
List<Integer> integers = new ArrayList<>();
for (int i = 0; i < 1000; i++ ) {
    integers.add(i);
}

List<String> strings = new ArrayList<>();
integers.parallelStream().forEach(i -> strings.add(i.toString()));
System.out.println(strings);// Data may be lost or subscripts may be out of bounds
/ / modified
List<Integer> integers = new ArrayList<>();
for (int i = 0; i < 1000; i++ ) {
    integers.add(i);
}
List<String> strings = integers.parallelStream()
Map (s-> s.tostring ())
.map(Objects::toString)
.collect(Collectors.toList());
System.out.println(strings);// Print the result [0, 1, 2...998, 999]
Copy the code

conclusion

When we write code with lambda, we should think a little bit more about why we’re doing it and not just for the sake of it, but that’s all we have to say and finally to all the programmers out there, write code without any bugs, okay