Example usage for java.util Queue element

List of usage examples for java.util Queue element

Introduction

In this page you can find the example usage for java.util Queue element.

Prototype

E element();

Source Link

Document

Retrieves, but does not remove, the head of this queue.

Usage

From source file:Main.java

public static void main(String[] args) {
    Queue<String> queue = new LinkedList<String>();

    queue.add("A");
    queue.add("B");

    queue.offer("C");
    queue.offer("D");

    System.out.println("remove: " + queue.remove());

    System.out.println("element: " + queue.element());

    System.out.println("poll: " + queue.poll());

    System.out.println("peek: " + queue.peek());
}

From source file:Main.java

public static void main(String[] args) {
    Queue<String> queue = new LinkedList<String>();
    queue.offer("First");
    queue.offer("Second");
    queue.offer("Third");
    queue.offer("Fourth");

    System.out.println("Size: " + queue.size());

    System.out.println("Queue head using peek   : " + queue.peek());
    System.out.println("Queue head using element: " + queue.element());

    Object data;/*  w w  w .  jav  a  2  s  .com*/
    while ((data = queue.poll()) != null) {
        System.out.println(data);
    }
}

From source file:Main.java

public static void main(String[] args) {
    Queue<String> queue = new LinkedList<>();
    queue.add("Java");
    // offer() will work the same as add()
    queue.offer("SQL");
    queue.offer("CSS");
    queue.offer("XML");

    System.out.println("Queue: " + queue);

    // Let's remove elements until the queue is empty
    while (queue.peek() != null) {
        System.out.println("Head  Element: " + queue.peek());
        queue.remove();//from ww  w .  j a  v a 2 s.  c  o m
        System.out.println("Removed one  element from  Queue");
        System.out.println("Queue: " + queue);
    }
    System.out.println("queue.isEmpty(): " + queue.isEmpty());
    System.out.println("queue.peek(): " + queue.peek());
    System.out.println("queue.poll(): " + queue.poll());
    try {
        String str = queue.element();
        System.out.println("queue.element(): " + str);
        str = queue.remove();
        System.out.println("queue.remove(): " + str);
    } catch (NoSuchElementException e) {
        System.out.println("queue.remove(): Queue is  empty.");
    }
}

From source file:playground.sergioo.ptsim2013.qnetsimengine.PTQLink.java

void makeVehicleAvailableToNextDriver(QVehicle veh, double now) {

    /*//from w  ww  .ja  v  a2  s.c om
     * Insert waiting passengers into vehicle.
     */
    Id<Vehicle> vehicleId = veh.getId();
    Set<MobsimAgent> passengers = this.passengersWaitingForCars.get(vehicleId);
    if (passengers != null) {
        // Copy set of passengers since otherwise we would modify it concurrently.
        List<MobsimAgent> passengersToHandle = new ArrayList<MobsimAgent>(passengers);
        for (MobsimAgent passenger : passengersToHandle) {
            this.unregisterPassengerAgentWaitingForCar(passenger, vehicleId);
            this.insertPassengerIntoVehicle(passenger, vehicleId, now);
        }
    }

    /*
     * If the next driver is already waiting for the vehicle, check whether
     * all passengers are also there. If not, the driver is not inserted
     * into the vehicle and the vehicle does not depart.
     */
    final Queue<MobsimDriverAgent> driversWaitingForCar = driversWaitingForCars.get(veh.getId());
    final boolean thereIsDriverWaiting = driversWaitingForCar != null && !driversWaitingForCar.isEmpty();
    if (thereIsDriverWaiting) {
        MobsimDriverAgent driverWaitingForPassengers = driversWaitingForPassengers
                .get(driversWaitingForCar.element().getId());
        if (driverWaitingForPassengers != null)
            return;
    }

    /*
     * If there is a driver waiting for its vehicle, and this car is not currently already leaving again with the
     * same vehicle, put the new driver into the vehicle and let it depart.
     */
    if (thereIsDriverWaiting && veh.getDriver() == null) {
        // set agent as driver and then let the vehicle depart
        veh.setDriver(driversWaitingForCar.remove());
        if (driversWaitingForCar.isEmpty()) {
            final Queue<MobsimDriverAgent> r = driversWaitingForCars.remove(veh.getId());
            assert r == driversWaitingForCar;
        }
        removeParkedVehicle(veh.getId());
        this.letVehicleDepart(veh, now);
    }
}