Example usage for java.util PriorityQueue toArray

List of usage examples for java.util PriorityQueue toArray

Introduction

In this page you can find the example usage for java.util PriorityQueue toArray.

Prototype

public Object[] toArray() 

Source Link

Document

Returns an array containing all of the elements in this queue.

Usage

From source file:Main.java

public static void main(String args[]) {

    PriorityQueue<Integer> prq = new PriorityQueue<Integer>();

    // insert values in the queue
    prq.add(6);// w  w  w .jav a 2s .  c o m
    prq.add(9);
    prq.add(5);
    prq.add(64);
    prq.add(6);

    System.out.println(prq);

    // get objects from the queue
    Object[] arr = prq.toArray();

    for (int i = 0; i < arr.length; i++) {
        System.out.println(arr[i]);
    }
}

From source file:io.warp10.script.functions.OPTDTW.java

@Override
public Object apply(WarpScriptStack stack) throws WarpScriptException {
    Object o = stack.pop();/* w  ww. ja  v  a  2s .  c om*/

    if (!(o instanceof Number)) {
        throw new WarpScriptException(
                getName() + " expects a count of best restults to return on top of the stack.");
    }

    int count = ((Number) o).intValue();

    o = stack.pop();

    if (!(o instanceof List)) {
        throw new WarpScriptException(getName() + " expects a numeric list to use as query below the count.");
    }

    double[] query = new double[((List) o).size()];
    int i = 0;
    for (Object oo : (List) o) {
        query[i++] = ((Number) oo).doubleValue();
    }

    // Z-Normalize query
    double[] musigma = DoubleUtils.musigma(query, true);
    for (i = 0; i < query.length; i++) {
        query[i] = (query[i] - musigma[0]) / musigma[1];
    }

    o = stack.pop();

    if (!(o instanceof List)) {
        throw new WarpScriptException(getName()
                + " expects a numeric list as the sequence in which to find best matches below the 'query' list.");
    }

    double[] sequence = new double[((List) o).size()];
    i = 0;
    for (Object oo : (List) o) {
        sequence[i++] = ((Number) oo).doubleValue();
    }

    if (sequence.length <= query.length) {
        throw new WarpScriptException(
                getName() + " expects the query list to be shorter than the sequence list.");
    }

    double mindist = 0.0;

    PriorityQueue<Pair<Integer, Double>> distances = new PriorityQueue<Pair<Integer, Double>>(
            new Comparator<Pair<Integer, Double>>() {
                @Override
                public int compare(Pair<Integer, Double> o1, Pair<Integer, Double> o2) {
                    return o1.getValue().compareTo(o2.getValue());
                }
            });

    double[] subsequence = new double[query.length];

    for (i = 0; i <= sequence.length - query.length; i++) {
        System.arraycopy(sequence, i, subsequence, 0, query.length);
        // Z-Normalize the subsequence
        musigma = DoubleUtils.musigma(subsequence, true);
        for (int j = 0; j < subsequence.length; j++) {
            subsequence[j] = (subsequence[j] - musigma[0]) / musigma[1];
        }
        double dist = dtw.compute(query, 0, query.length, subsequence, 0, query.length, mindist);

        if (dist < 0) {
            continue;
        }

        distances.add(new Pair<Integer, Double>(i, dist));

        //
        // If the priority queue is of 'count' size, retrieve the largest distance and
        // use it as the threshold for the DTW computation
        //

        if (count > 0 && distances.size() >= count) {
            Object adist[] = distances.toArray();
            mindist = ((Pair<Integer, Double>) adist[count - 1]).getValue();
        }
    }

    List<List<Object>> results = new ArrayList<List<Object>>();

    while (!distances.isEmpty()) {

        Pair<Integer, Double> entry = distances.poll();

        List<Object> result = new ArrayList<Object>();
        result.add(entry.getKey());
        result.add(entry.getValue());
        results.add(result);

        if (count > 0 && count == results.size()) {
            break;
        }
    }

    stack.push(results);

    return stack;
}