Example usage for java.util PriorityQueue remove

List of usage examples for java.util PriorityQueue remove

Introduction

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

Prototype

public boolean remove(Object o) 

Source Link

Document

Removes a single instance of the specified element from this queue, if it is present.

Usage

From source file:Main.java

public static void main(String args[]) {

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

    // insert values in the queue
    for (int i = 3; i < 10; i++) {
        prq.add(i);/* w ww  .  j a  va  2  s  . co m*/
    }

    System.out.println(prq);

    // remove 7 from the queue
    boolean isremoved = prq.remove(7);

    System.out.println("Return value after remove: " + isremoved);

    System.out.println(prq);
}

From source file:Navigation.Vertex.java

public static void computePaths(Vertex source) {
    source.minDistance = 0.;//from  w  w  w .  ja  v a 2s .c  o  m
    PriorityQueue<Vertex> vertexQueue = new PriorityQueue<Vertex>();
    vertexQueue.add(source);

    while (!vertexQueue.isEmpty()) {
        Vertex u = vertexQueue.poll();

        // Visit each edge exiting u
        for (Edge e : u.adjacencies) {
            Vertex v = e.target;
            double weight = e.weight;
            double distanceThroughU = u.minDistance + weight;
            if (distanceThroughU < v.minDistance) {
                vertexQueue.remove(v);

                v.minDistance = distanceThroughU;
                v.previous = u;
                vertexQueue.add(v);

            }

        }
    }
}

From source file:de.tudarmstadt.lt.n2n.annotators.RelationAnnotator.java

protected List<Dependency> find_path_dijkstra(Token start, Token dest, Collection<Token> nodes,
        Map<Token, List<Dependency>> edges) throws IllegalStateException {
    List<Dependency> shortest_path = new ArrayList<Dependency>();

    final Map<Token, Integer> dist = new HashMap<Token, Integer>();
    final Map<Token, Dependency> prev = new HashMap<Token, Dependency>();
    for (Token t : nodes)
        dist.put(t, Integer.MAX_VALUE);
    dist.put(start, 0);/*from  w  w w  .j  ava 2  s . c  o m*/

    PriorityQueue<Token> Q = new PriorityQueue<Token>(edges.size(), new Comparator<Token>() {
        @Override
        public int compare(Token o1, Token o2) {
            return dist.get(o1).compareTo(dist.get(o2));
        }
    });
    Q.addAll(nodes);

    while (!Q.isEmpty()) {
        Token u = Q.poll(); // initially source node
        if (u.equals(dest)) // stop if dest
            break;
        if (dist.get(u) == Integer.MAX_VALUE)
            throw new IllegalStateException(String.format(
                    "Could not find path from token '%s' to token '%s'. Perhaps start or dest is part of a preposition? (%s)",
                    start.getCoveredText(), dest.getCoveredText(),
                    DocumentMetaData.get(u.getCAS()).getDocumentId()));

        List<Dependency> connected_edges = edges.get(u);
        if (connected_edges == null)
            continue;

        for (Dependency d : connected_edges) {
            Token v = null;
            if (u.equals(d.getGovernor()))
                v = d.getDependent();
            else
                v = d.getGovernor();
            if (!Q.contains(v))
                continue;
            int alt = dist.get(u) + 1; // dist(u,v) = 1
            if (alt < dist.get(v)) {
                dist.put(v, alt);
                prev.put(v, d);
                Q.remove(v); // reinsert v so that Q is recomputed
                Q.offer(v);
            }
        }
    }

    Token u = dest;
    Dependency e = prev.get(u);
    while (e != null) {
        shortest_path.add(0, e);
        if (u == e.getGovernor())
            u = e.getDependent();
        else
            u = e.getGovernor();
        e = prev.get(u);
    }

    return shortest_path;
}