Example usage for java.util Queue add

List of usage examples for java.util Queue add

Introduction

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

Prototype

boolean add(E e);

Source Link

Document

Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions, returning true upon success and throwing an IllegalStateException if no space is currently available.

Usage

From source file:MainClass.java

public static void main(String[] args) {
    Queue queue = new LinkedList();
    queue.add("Hello");
    queue.add("World");
    List list = new ArrayList(queue);

    System.out.println(list);/*from  ww  w  .  jav a 2 s .com*/
}

From source file:Main.java

public static void main(String[] args) {
    Queue<String> queue = new LinkedList<String>();
    queue.add("Hello");
    queue.add("World");
    List<String> list = new ArrayList<String>(queue);

    System.out.println(list);/*from   ww w  .j  av a2s .com*/
}

From source file:Main.java

public static void main(String[] args) {
    Queue<String> myQueue = new LinkedList<String>();
    myQueue.add("A");
    myQueue.add("B");
    myQueue.add("C");
    myQueue.add("D");

    List<String> myList = new ArrayList<String>(myQueue);

    for (Object theFruit : myList)
        System.out.println(theFruit);
}

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<>();
    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 av  a2s .  co 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:CircularArrayQueueTest.java

public static void main(String[] args) {
    Queue<String> q = new CircularArrayQueue<String>(5);
    q.add("Amy");
    q.add("Bob");
    q.add("Carl");
    q.add("Deedee");
    q.add("Emile");
    q.remove();//from  w  w w.  j  ava  2s .  com
    q.add("Fifi");
    q.remove();
    for (String s : q)
        System.out.println(s);
}

From source file:ComparablePerson.java

public static void main(String[] args) {
    int initialCapacity = 5;
    Comparator<ComparablePerson> nameComparator = Comparator.comparing(ComparablePerson::getName);

    Queue<ComparablePerson> pq = new PriorityQueue<>(initialCapacity, nameComparator);
    pq.add(new ComparablePerson(1, "Oracle"));
    pq.add(new ComparablePerson(4, "XML"));
    pq.add(new ComparablePerson(2, "HTML"));
    pq.add(new ComparablePerson(3, "CSS"));
    pq.add(new ComparablePerson(4, "Java"));

    System.out.println("Priority  queue: " + pq);

    while (pq.peek() != null) {
        System.out.println("Head  Element: " + pq.peek());
        pq.remove();/* w w  w .  ja va 2s  . c  om*/
        System.out.println("Removed one  element from  Queue");
        System.out.println("Priority  queue: " + pq);
    }
}

From source file:com.ok2c.lightmtp.examples.MailUserAgentExample.java

public static void main(final String[] args) throws Exception {

    String text1 = "From: root\r\n" + "To: testuser1\r\n" + "Subject: test message 1\r\n" + "\r\n"
            + "This is a short test message 1\r\n";
    String text2 = "From: root\r\n" + "To: testuser1, testuser2\r\n" + "Subject: test message 2\r\n" + "\r\n"
            + "This is a short test message 2\r\n";
    String text3 = "From: root\r\n" + "To: testuser1, testuser2, testuser3\r\n" + "Subject: test message 3\r\n"
            + "\r\n" + "This is a short test message 3\r\n";

    List<DeliveryRequest> requests = new ArrayList<DeliveryRequest>();
    requests.add(new BasicDeliveryRequest("root", Arrays.asList("testuser1"),
            new ByteArraySource(text1.getBytes("US-ASCII"))));
    requests.add(new BasicDeliveryRequest("root", Arrays.asList("testuser1", "testuser2"),
            new ByteArraySource(text2.getBytes("US-ASCII"))));
    requests.add(new BasicDeliveryRequest("root", Arrays.asList("testuser1", "testuser2", "testuser3"),
            new ByteArraySource(text3.getBytes("US-ASCII"))));

    MailUserAgent mua = new DefaultMailUserAgent(TransportType.SMTP, IOReactorConfig.DEFAULT);
    mua.start();//from ww w. j a v  a 2s  .  c om

    try {

        InetSocketAddress address = new InetSocketAddress("localhost", 2525);

        Queue<Future<DeliveryResult>> queue = new LinkedList<Future<DeliveryResult>>();
        for (DeliveryRequest request : requests) {
            queue.add(mua.deliver(new SessionEndpoint(address), 0, request, null));
        }

        while (!queue.isEmpty()) {
            Future<DeliveryResult> future = queue.remove();
            DeliveryResult result = future.get();
            System.out.println("Delivery result: " + result);
        }

    } finally {
        mua.shutdown();
    }
}

From source file:com.ok2c.lightmtp.examples.LocalMailClientTransportExample.java

public static void main(final String[] args) throws Exception {

    String text1 = "From: root\r\n" + "To: testuser1\r\n" + "Subject: test message 1\r\n" + "\r\n"
            + "This is a short test message 1\r\n";
    String text2 = "From: root\r\n" + "To: testuser1, testuser2\r\n" + "Subject: test message 2\r\n" + "\r\n"
            + "This is a short test message 2\r\n";
    String text3 = "From: root\r\n" + "To: testuser1, testuser2, testuser3\r\n" + "Subject: test message 3\r\n"
            + "\r\n" + "This is a short test message 3\r\n";

    DeliveryRequest request1 = new BasicDeliveryRequest("root", Arrays.asList("testuser1"),
            new ByteArraySource(text1.getBytes("US-ASCII")));

    DeliveryRequest request2 = new BasicDeliveryRequest("root", Arrays.asList("testuser1", "testuser2"),
            new ByteArraySource(text2.getBytes("US-ASCII")));

    DeliveryRequest request3 = new BasicDeliveryRequest("root",
            Arrays.asList("testuser1", "testuser2", "testuser3"),
            new ByteArraySource(text3.getBytes("US-ASCII")));

    Queue<DeliveryRequest> queue = new ConcurrentLinkedQueue<DeliveryRequest>();
    queue.add(request1);
    queue.add(request2);//w w  w . jav a2 s  .  c om
    queue.add(request3);

    CountDownLatch messageCount = new CountDownLatch(queue.size());

    IOReactorConfig config = IOReactorConfig.custom().setIoThreadCount(1).build();

    MailClientTransport mua = new LocalMailClientTransport(config);
    mua.start(new MyDeliveryRequestHandler(messageCount));

    SessionEndpoint endpoint = new SessionEndpoint(new InetSocketAddress("localhost", 2525));

    SessionRequest sessionRequest = mua.connect(endpoint, queue, null);
    sessionRequest.waitFor();

    IOSession iosession = sessionRequest.getSession();
    if (iosession != null) {
        messageCount.await();
    } else {
        IOException ex = sessionRequest.getException();
        if (ex != null) {
            System.out.println("Connection failed: " + ex.getMessage());
        }
    }

    System.out.println("Shutting down I/O reactor");
    try {
        mua.shutdown();
    } catch (IOException ex) {
        mua.forceShutdown();
    }
    System.out.println("Done");
}

From source file:reactor.logback.DurableLogUtility.java

@SuppressWarnings("unchecked")
public static void main(String... args) throws ParseException, JoranException, IOException {
    Parser parser = new BasicParser();
    CommandLine cl = null;/*w w w.  j ava 2 s.  com*/
    try {
        cl = parser.parse(OPTS, args);
    } catch (ParseException e) {
        HelpFormatter help = new HelpFormatter();
        help.printHelp("dlog", OPTS, true);
        System.exit(-1);
    }

    LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
    loggerContext.reset();

    if (cl.hasOption("config")) {
        // Read Logback configuration
        JoranConfigurator configurator = new JoranConfigurator();
        configurator.setContext(loggerContext);

        configurator.doConfigure(cl.getOptionValue("file", "logback.xml"));

        StatusPrinter.printInCaseOfErrorsOrWarnings(loggerContext);
    } else {
        BasicConfigurator.configure(loggerContext);
    }

    Appender appender = null;
    if (cl.hasOption("output")) {
        String outputAppender = cl.getOptionValue("output", "console");
        appender = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME).getAppender(outputAppender);
    }

    ChronicleTools.warmup();
    Chronicle chronicle = ChronicleQueueBuilder.indexed(cl.getOptionValue("path")).build();
    ExcerptTailer ex = chronicle.createTailer();

    Level level = Level.valueOf(cl.getOptionValue("level", "TRACE"));

    if (cl.hasOption("head")) {
        int lines = Integer.parseInt(cl.getOptionValue("head", "10"));
        for (int i = 0; i < lines; i++) {
            LoggingEvent evt = readLoggingEvent(ex, loggerContext);
            if (evt.getLevel().isGreaterOrEqual(level)) {
                writeEvent(evt, appender);
            }
        }
    } else if (cl.hasOption("tail")) {
        int lines = Integer.parseInt(cl.getOptionValue("tail", "10"));
        Queue<LoggingEvent> tail = new LinkedBlockingQueue<LoggingEvent>(lines);
        while (ex.nextIndex()) {
            LoggingEvent evt = readLoggingEvent(ex, loggerContext);
            if (!tail.offer(evt)) {
                tail.poll();
                tail.add(evt);
            }
        }
        LoggingEvent evt;
        while (null != (evt = tail.poll())) {
            if (evt.getLevel().isGreaterOrEqual(level)) {
                writeEvent(evt, appender);
            }
        }
    } else if (cl.hasOption("search")) {
        String regex = cl.getOptionValue("search");
        Pattern regexPatt = Pattern.compile(regex);
        while (ex.nextIndex()) {
            LoggingEvent evt = readLoggingEvent(ex, loggerContext);
            if (null != evt && evt.getLevel().isGreaterOrEqual(level)) {
                if (regexPatt.matcher(evt.getFormattedMessage()).matches()) {
                    writeEvent(evt, appender);
                }
            }
        }
    }

    loggerContext.stop();
    chronicle.close();
}