How to use streams with interfering methods and constructors, and why not use .peek()?

I'll show a simplified example of my problem to help demonstrate it.

let's say I'm trying to convert an old java code to java 8 code for style and parallelization purposes (Which i do).

this is the code:

    public static boolean deleteTester(List<Integer> keys) {
        DHeap dHeap = new DHeap(d, keys.size());
        DHeap_Item[] DHeap_ItemArray = keysListToDHeap_ItemArray(keys);
        dHeap.arrayToHeap(DHeap_ItemArray);

        for (int i = 0; i < keys.size(); i++) {
            keys.set(i, null);
            dHeap.delete(DHeap_ItemArray[i]);
            if (!someTest(keys, dHeap.getList()))
                return false;
        }
        return true;
    }

There are several issues in the conversion:

  1. I need to check every iteration, so i can't reduce/collect and then check.
  2. I am accessing an array and a data structure object (which the tester is for), meaning it isn't non-interfering, and isn't stateless operation.

Here is my attempt at replacing the for loop:

    return IntStream.range(0, keys.size())
            //.parallel()
            .peek(idx -> keys.set(idx, null))
            .peek(idx -> dHeap.delete(DHeap_ItemArray[idx]))
            .allMatch(e -> someTest(keys, dHeap.getList()));

which is much shorter and readable, however "breaks the rules" and in result unable to parallel.

So my questions are:

  1. Why am I unable to use .parallal()? (I get a null pointer ex.)
  2. Why shouldn't we use peek()? because in this code i see no other way around it.
  3. What should I do when accessing outside elements?
  4. How should I "transfer" the code to java 8 streams?
  5. What am I missing?

I guess that the answers are intertwined. I just started to learn java 8 so thanks a lot for all the help because I'm really stuck.

Answer:1

I have a bunch of constraints, each consisting of two symbols and a comparison operator: <, <=, !=, ==, >=, or >. So, for example: A <= B C >= B A != C C == D D > E I want to be ...

I've been googling around for a solution to this problem, I probably found it but I'm pretty new to programming so my level of understanding is mediocre at best. I have a calculation taking place in ...

Hi is there any cache or settings of the jvm to speed up methods call? e.g: I do have a web service and when I call it once per 10minutes or so it's quite slow processing takes around 8-10s in ...

I am building a java application and i have implemented custom listeners and events. The problem now is that there are many events and listeners and the application GUI thread is being blocked while ...