Example usage for java.util Deque isEmpty

List of usage examples for java.util Deque isEmpty

Introduction

In this page you can find the example usage for java.util Deque isEmpty.

Prototype

boolean isEmpty();

Source Link

Document

Returns true if this collection contains no elements.

Usage

From source file:Main.java

public static void main(String args[]) {
    Deque<String> stack = new ArrayDeque<String>();
    Deque<String> queue = new ArrayDeque<String>();

    stack.push("A");
    stack.push("B");
    stack.push("C");
    stack.push("D");

    while (!stack.isEmpty())
        System.out.print(stack.pop() + " ");

    queue.add("A");
    queue.add("B");
    queue.add("C");
    queue.add("D");
    while (!queue.isEmpty())
        System.out.print(queue.remove() + " ");
}

From source file:Main.java

public static void main(String[] args) {
    // Create a Deque and use it as stack
    Deque<String> deque = new ArrayDeque<>();
    deque.push("Oracle");
    deque.push("HTML");
    deque.push("CSS");
    deque.push("XML");

    System.out.println("Stack: " + deque);

    // remove all elements from the Deque
    while (deque.peek() != null) {
        System.out.println("Element at  top:  " + deque.peek());
        System.out.println("Popped: " + deque.pop());
        System.out.println("Stack: " + deque);
    }/*from ww w.j ava 2  s . co m*/

    System.out.println("Stack is  empty:  " + deque.isEmpty());
}

From source file:Main.java

public static void main(String[] args) {
    Deque<String> deque = new LinkedList<>();
    deque.addLast("Oracle");
    deque.offerLast("Java");
    deque.offerLast("CSS");
    deque.offerLast("XML");

    System.out.println("Deque: " + deque);

    // remove elements from the Deque until it is empty
    while (deque.peekFirst() != null) {
        System.out.println("Head  Element: " + deque.peekFirst());
        deque.removeFirst();/*from w w w. ja  v a 2  s. c o m*/
        System.out.println("Removed one  element from  Deque");
        System.out.println("Deque: " + deque);
    }

    // the Deque is empty. Try to call its peekFirst(),
    // getFirst(), pollFirst() and removeFirst() methods
    System.out.println("deque.isEmpty(): " + deque.isEmpty());

    System.out.println("deque.peekFirst(): " + deque.peekFirst());
    System.out.println("deque.pollFirst(): " + deque.pollFirst());

    String str = deque.getFirst();
    System.out.println("deque.getFirst(): " + str);
    str = deque.removeFirst();
    System.out.println("deque.removeFirst(): " + str);

}

From source file:cz.cuni.mff.ksi.jinfer.autoeditor.automatonvisualizer.layouts.graphviz.AutomatonToDot.java

public static <T> String convertToDot(final Automaton<T> automaton,
        final Transformer<Step<T>, String> edgeLabelTransformer) {
    final StringBuilder sb = new StringBuilder();
    sb.append("digraph finite_state_machine {\n");
    sb.append("\trankdir=LR;\n");
    sb.append("\tnodesep=\"50\";");
    sb.append("\tsplines=\"line\";");
    sb.append("\tranksep=\"100\";");
    sb.append("\tedge [label = \"\", dir = none, arrowhead=none, arrowtail=none];");
    sb.append("\tnode [shape = none, label = \"\", width = 0, height = 0];\n");
    final Deque<State<T>> queue = new ArrayDeque<State<T>>();
    queue.addAll(automaton.getDelta().keySet());

    while (!queue.isEmpty()) {
        final State<T> actual = queue.removeFirst();
        sb.append(actual.getName());/*from w w  w . j ava2 s  .  c o m*/
        sb.append(";\n");
        for (Step<T> step : automaton.getDelta().get(actual)) {
            sb.append("\t");
            sb.append(step.getSource().getName());
            sb.append(" -> ");
            sb.append(step.getDestination().getName());
            sb.append("];\n");
        }
    }
    sb.append("\n}");
    return sb.toString();
}

From source file:com.frostwire.platform.DefaultFileSystem.java

public static void walkFiles(FileSystem fs, File file, FileFilter filter) {
    File[] arr = fs.listFiles(file, filter);
    if (arr == null) {
        return;/*  ww  w. j  a  v  a 2 s  .  co  m*/
    }
    Deque<File> q = new LinkedList<>(Arrays.asList(arr));

    while (!q.isEmpty()) {
        File child = q.pollFirst();
        filter.file(child);
        if (fs.isDirectory(child)) {
            arr = fs.listFiles(child, filter);
            if (arr != null) {
                for (int i = arr.length - 1; i >= 0; i--) {
                    q.addFirst(arr[i]);
                }
            }
        }
    }
}

From source file:org.apache.sling.etcd.testing.tree.Node.java

@Nonnull
public static String name(@Nonnull String path) {
    Deque<String> names = names(path);
    return names.isEmpty() ? "/" : names.getLast();
}

From source file:org.apache.sling.etcd.testing.tree.Node.java

@Nonnull
public static String parent(@Nonnull String path) {
    Deque<String> names = names(path);
    if (!names.isEmpty()) {
        names.removeLast();/*w ww.  ja  v  a2s .  c o  m*/
    }
    return path(names);
}

From source file:org.apache.sling.etcd.testing.tree.Node.java

@Nonnull
public static Deque<String> names(@Nonnull String key) {
    Deque<String> names = new ArrayDeque<String>(Arrays.asList(key.split("/")));
    if (!names.isEmpty()) {
        names.removeFirst();/*  w w  w. j  av a  2 s. c  o  m*/
    }
    return names;
}

From source file:net.myrrix.common.io.IOUtils.java

/**
 * Attempts to recursively delete a directory. This may not work across symlinks.
 *
 * @param dir directory to delete along with contents
 * @return {@code true} if all files and dirs were deleted successfully
 *//*from ww w . jav  a2s .c o m*/
public static boolean deleteRecursively(File dir) {
    if (dir == null) {
        return false;
    }
    Deque<File> stack = new ArrayDeque<File>();
    stack.push(dir);
    boolean result = true;
    while (!stack.isEmpty()) {
        File topElement = stack.peek();
        if (topElement.isDirectory()) {
            File[] directoryContents = topElement.listFiles();
            if (directoryContents != null && directoryContents.length > 0) {
                for (File fileOrSubDirectory : directoryContents) {
                    stack.push(fileOrSubDirectory);
                }
            } else {
                result = result && stack.pop().delete();
            }
        } else {
            result = result && stack.pop().delete();
        }
    }
    return result;
}

From source file:org.roda.core.util.ZipUtility.java

public static void zip(File directory, ZipOutputStream zout) throws IOException {
    URI base = directory.toURI();
    Deque<File> queue = new LinkedList<>();
    queue.push(directory);//w ww.ja  v  a 2  s  . c o m
    try {
        while (!queue.isEmpty()) {
            File dir = queue.pop();
            for (File kid : dir.listFiles()) {
                String name = base.relativize(kid.toURI()).getPath();
                if (kid.isDirectory()) {
                    queue.push(kid);
                    name = name.endsWith("/") ? name : name + "/";
                    zout.putNextEntry(new ZipEntry(name));
                } else {
                    zout.putNextEntry(new ZipEntry(name));
                    copy(kid, zout);
                    zout.closeEntry();
                }
            }
        }
    } finally {
        zout.close();
    }
}