1

I have a Queue which I want to convert into long[] and pass it to my method which calculate percentiles.

private final ConcurrentLinkedQueue<Long> holder = new ConcurrentLinkedQueue<>();

I am using ConcurrentLinkedQueue because I am inserting latencies which are in milliseconds from multithread application into my above holder queue so I wanted to be thread safe.

Now my question is how can I convert holder queue into long[] long array so that I can pass it to my below method? Is there any way to do that?

  public static long[] percentiles(long[] latencies, double... percentiles) {
    Arrays.sort(latencies, 0, latencies.length);
    long[] values = new long[percentiles.length];
    for (int i = 0; i < percentiles.length; i++) {
      int index = (int) (percentiles[i] * latencies.length);
      values[i] = latencies[index];
    }
    return values;
  }
  • Have you tried anything? What's the problem you're facing? – JB Nizet Dec 14 '16 at 22:50
  • May this question be a duplicate of this? [Convert a Queue to ArrayList](http://stackoverflow.com/questions/20708358/convert-from-queue-to-arraylist) –  Dec 14 '16 at 23:01

1 Answers1

0

It looks like ConcurrentLinkedQueue#toArray(T[] a) will get you 90% of the way there:

Long[] longs = holder.toArray(new Long[0]);

Converting Long[] to long[] left as exercise to student. ;-)

AJNeufeld
  • 8,526
  • 1
  • 25
  • 44