-2

I'm trying to convert traditional for loop into Java Stream, but some problems are occurring. Basically, my Method execution taking too much time.When I describe reducing the method execution time. When converting the traditional for loop into JavaStream then the List size is 0, otherwise using the traditional for loop list size is not 0 and taking too much time. Please, Anyone, resolve this problem.

1.

 for (ProductProjection p : products) {
     if (p != null && p.getCommodityId() != 0) {
           batchIdList.add(p.getCommodityId());
     }
 }

The above code segment converts into the Java stream is correct or not, please edit me.

products.parallelStream().filter(product -> Objects.nonNull(product)  && (product.getCommodityId() != 0))
                .forEach(pro -> {
                  batchIdList.add(pro.getCommodityId());
                });


2.

for (ProductProjection p : products) {
    for (CourseBatch cb : batches) {
        if (cb.getId() == p.getCommodityId()) {
            CourseProductResponse cpr = new CourseProductResponse();
            if (cb.getCourse() != null) {
                cpr.setCourseName(cb.getCourse().getCourseTitle());
                cpr.setBatchName(cb.getBatchName());
            }
            cpr.setProduct(p);
            response.add(cpr);
         }
    }
}

The above code segment converts into the Java stream is correct or not, please edit me.

products.parallelStream()
          .forEach(product -> {
          batches.parallelStream().peek(e -> System.out.println("Batches : " + e))
            .filter(cb -> cb.getId() == product.getCommodityId())
            .forEach(cb -> {
                CourseProductResponse cpr = new CourseProductResponse();
                if (Objects.nonNull(cb.getCourse())) {
                  cpr.setCourseName(cb.getCourse().getCourseTitle());
                  cpr.setBatchName(cb.getBatchName());
                }
                cpr.setProduct(product);
                response.add(cpr);
          });
        });

second loop. List of ProductProjection size: 1238 and List of CourseBatch size: 1124

Ng Sharma
  • 2,072
  • 8
  • 27
  • 49
  • 1
    Be careful using parallelStream(). In your first example you need a thread safe list or you should use just stream(). And why do you want to switch to streams? Your second snippet is less readable with streams. – Milgo Sep 10 '20 at 08:59
  • second option taking too much time. that way switch to the streams – Ng Sharma Sep 10 '20 at 09:02
  • Then you need to make response thread safe. – Milgo Sep 10 '20 at 09:20
  • From a global point of view, using parallel streams often makes your application *slower* (unless the task at hand is alone on your machine). From the available computing power, you consume the same amount of resources, only from distributed over multiple cores instead of one single core. Plus you introduce the overhead for the coordination of the threads. Your current task might finish earlier, but other tasks suffer more. – Ralf Kleberhoff Sep 10 '20 at 09:24
  • Generally, if something is too slow, first measure which part it is that's consuming too much time (using a decent profiler), then find an improvement that tackles that problem. – Ralf Kleberhoff Sep 10 '20 at 09:29

1 Answers1

0

Do not modify the collection in the stream, use method collect()

1.

List<??> addToBatchIdList = products.parallelStream()
        .filter(Objects::nonNull)
        .map(product::getCommodityId)
        .filter(Objects::nonNull)
        .collect(Collectors.toList());
batchIdList.addAll(addToBatchIdList);
Ilya
  • 720
  • 6
  • 14