0

I am trying to simulate a median of infinite nos.This is my code

package hard;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Random;
import java.util.stream.Stream;

public class Median
{
static Queue<Integer> minHeap;

static Queue<Integer> maxHeap;

{
    minHeap = new PriorityQueue<>();

    maxHeap = new PriorityQueue<>();
}

private static void add(Stream<Integer> randomNos)
{

    randomNos.forEach(no ->
    {
        if (minHeap.size() == 0 || no < minHeap.peek())
        {
            minHeap.add(no);
        }
            else
            {
                maxHeap.add(no);
            }

            int diff = minHeap.size() - maxHeap.size();

            if (Math.abs(diff) > 1)
            {
                if (diff < 0)
                {
                    minHeap.add(maxHeap.peek());
                }
                else
                {
                    maxHeap.add(minHeap.peek());
                }
            }
        });

}
private static int median()
{
    int median = 0;

    if ((minHeap.size() + maxHeap.size()) % 2 == 0)
    {
        median = (minHeap.peek() + maxHeap.peek()) / 2;
    }
    else
    {
        if (minHeap.size() < maxHeap.size())
        {
            median = maxHeap.peek();
        }
        else
        {
            median = minHeap.peek();
        }
    }

    return median;
}

public static void main(String[] args)
{
    add(new Random().ints(1, 100000).boxed());
    System.out.println(median());
}

}

I get the following exception :

Exception in thread "main" java.lang.NullPointerException
at hard.Median.lambda$0(Median.java:25)
at hard.Median$$Lambda$2/1929600551.accept(Unknown Source)
at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183)
at java.util.stream.IntPipeline$4$1.accept(IntPipeline.java:250)
at java.util.Random$RandomIntsSpliterator.forEachRemaining(Random.java:1044)
at java.util.Spliterator$OfInt.forEachRemaining(Spliterator.java:693)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:512)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:502)
at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150)
at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:418)
at hard.Median.add(Median.java:23)
at hard.Median.main(Median.java:75)

I ran the debugger and found that there are 2 values of the same integer repeated after which this exception is flushed out

1
  • You might want to point out what line 25 is. (BTW: .peek() might return null). Besides: I dont think this is a correct implementation. You need to sort the values. Commented Jul 29, 2014 at 2:55

1 Answer 1

2

This

{
    minHeap = new PriorityQueue<>();

    maxHeap = new PriorityQueue<>();
}

is an instance initializer. You may want

static {
    minHeap = new PriorityQueue<>();

    maxHeap = new PriorityQueue<>();
}

so that it is executed when the class is initialized.

Note however that the PriorityQueue objects will be empty. peek will return null anyway and the < comparison will fail. You may want to add some objects to the PriorityQueue objects before you use them.

Sign up to request clarification or add additional context in comments.

1 Comment

Oops !a simple error .Thanks for catching it.I was fooled by the stack trace

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.