Convert queue into long array?

白昼怎懂夜的黑 提交于 2019-12-11 04:24:52

问题


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;
  }

回答1:


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. ;-)



来源:https://stackoverflow.com/questions/41153502/convert-queue-into-long-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!